Question

I have a singleton bean for which the @PostConstruct method needs to call an @Asynchronous method within itself. It cannot do so directly using this because that makes the call synchronous. I cannot @Inject itself because it is circular.

Était-ce utile?

La solution

You can use such type of wrapper:

@Singleton
public class SingletonBean {



@Stateless
public static class AsynchronousMethodLauncher{
    @EJB
    private SingletonBean singletonBean;

    public void launch(){
        singletonBean.asynchronousMethod();
    }
}

    @EJB
    AsynchronousMethodLauncher launcher;

    @Asynchronous
    public void asynchronousMethod(){
        //Place your code here
    }

    public void yourMethod(){
        launcher.launch();
    }
}

Autres conseils

I would suggest natural Java EE way:

@Singleton
public class AsyncSingletonBeanBean {

    @Resource
    private SessionContext sessionContext;

    @PostConstruct
    public void init() {
        AsyncSingletonBeanBean myBean = sessionContext.getBusinessObject(this.getClass());
        myBean.foo();
    }

    @Asynchronous
    public Future<String> foo() {
        return new AsyncResult<String>("Hello");
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top