0
我有兩個類在java ee中調用asynchronus方法調用。我正在關注鏈接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()方法invcation完成,你可以從結果asyncMethod()方法invcation沒有被另一個線程處理看到的。 使用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;
}
}
嘗試使用@EJB註釋注入'BusinessBean'。 –