Java 如何将具有异步方法调用行为的bean添加到liferay portlet?
我运行Liferay 6.2 CE。我试图声明一个bean,该bean的方法可以异步调用。但当我调用该方法时,它会同步执行。所以我不明白,我错过了什么 这是我的豆子:Java 如何将具有异步方法调用行为的bean添加到liferay portlet?,java,spring,asynchronous,liferay,liferay-6,Java,Spring,Asynchronous,Liferay,Liferay 6,我运行Liferay 6.2 CE。我试图声明一个bean,该bean的方法可以异步调用。但当我调用该方法时,它会同步执行。所以我不明白,我错过了什么 这是我的豆子: public class MyBean { @Async public void process(){ try { Thread.sleep(10000L); } catch (InterruptedException e) { e.
public class MyBean {
@Async
public void process(){
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(new Date());
}
}
我已将Spring注释添加到我的项目依赖项中:
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
...
您可以共享调用process()方法的代码段吗?@premkumar,我已经共享了代码段。很可能,我应该直接使用
ThreadPoolTaskExecutor
。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- activate the @Async annotation -->
<task:annotation-driven executor="taskExecutor"/>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean>
<bean id="processor" class="mypackage.MyBean"/>
</beans>
...
MyBean myBean = (MyBean) PortletBeanLocatorUtil.locate(my_plugin_context_name, "processor");
myBean.process();
...