我有两个类可以在 java ee 中进行异步方法调用。我正在关注链接http://satishgopal.wordpress.com/2011/04/24/ejb-3-1-asynchronous-methods来学习如何做。当我调用异步方法时,我得到以下结果。
结果:
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;
}
}