我有两个类在java ee中进行asynchronus方法调用。我正在关注http://satishgopal.wordpress.com/2011/04/24/ejb-3-1-asynchronous-methods链接以了解如何操作。 当我调用asynrnous方法时,我得到了以下结果。
结果:
INFO: caller method running in thread http-thread-pool-8083(5) date:1373866669346
INFO: start running asyncMethod in thread http-thread-pool-8083(5)
INFO: finished running asyncMethod in thread http-thread-pool-8083(5)
INFO: caller method running in thread http-thread-pool-8083(5) date:1373866672348
我期待的是不要等待asyncMethod()方法调用完成,但是从结果中可以看出asyncMethod()方法调用不是由另一个线程处理的。 使用Glassfish 3.1.1作为应用程序容器。
BusinessBean类
@ManagedBean
@Stateless
public class BusinessBean {
@Asynchronous
public void asyncMethod() {
System.out.println("start running asyncMethod in thread "+ Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished running asyncMethod in thread "+ Thread.currentThread().getName());
}
}
LoginBean类
@ManagedBean
@RequestScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
@ManagedProperty(value="#{businessBean}")
BusinessBean businessBean;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void login(){
System.out.println("caller method running in thread "+Thread.currentThread().getName()+" date:"+System.currentTimeMillis());
businessBean.asyncMethod();
System.out.println("caller method running in thread "+Thread.currentThread().getName()+" date:"+System.currentTimeMillis());
}
public BusinessBean getBusinessBean() {
return businessBean;
}
public void setBusinessBean(BusinessBean businessBean) {
this.businessBean = businessBean;
}
}
答案 0 :(得分:1)
删除BusinessBean的ManagedBean
注释:它应该是Enterprise Java Bean,这要归功于@Stateless
注释。
也可以通过@EJB
:
@EJB
private BusinessBean businessBean;
有关详细信息,请参阅this link。