Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

Asynchronous Methods invocation

I have two classes to make asynchronus method call in java ee. I am following the link http://satishgopal.wordpress.com/2011/04/24/ejb-3-1-asynchronous-methods to learn how to do. When I have invocate the asynrnous method , I am having following result.

Result:

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

What I am expecting is not to wait asyncMethod() method invcation completed however as you can see from the result that asyncMethod() method invcation is not handled by another thread. Using Glassfish 3.1.1 as Application container.

BusinessBean class

@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 class

@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;
    }


}

Upvotes: 0

Views: 265

Answers (1)

perissf
perissf

Reputation: 16273

Get rid of the ManagedBean annotation for BusinessBean: it should be an Enterprise Java Bean, and it is thanks to the @Stateless annotation.

Also inject it by means of @EJB:

@EJB
private BusinessBean businessBean;

See this link for further reference.

Upvotes: 1

Related Questions