mercredi 8 juillet 2015

@Singleton @Startup depends on @Stateless EJB

I have the following setup:

@Singleton
@Startup
@DependsOn(value="DataSourceHandler")
public class TimerTask {

    @EJB(name = "DataSourceHandler")
    DataSourceHandler dataSourceHandler;
}

@Stateless(name = "DataSourceHandler")
public class DataSourceHandler {
    ... database operations
}

The timertask runs once every 30 minutes and performs database operations with the help of the DataSourceHandler EJB.

The problem here is that I'm unable to inject the EJB into the Singleton Timertask, because a singleton can only depend on other singletons. The solutions proposed in other questions don't work for me however:

  • I can't make the DataSourceHandler a Singleton because it is also used in other parts of the application and not multithreading-save.
  • I can't remove the Singleton from the TimerTask because it is required for the @Startup annotation

How can I inject a stateless into a singleton?

Aucun commentaire:

Enregistrer un commentaire