I want to force a singleton bean to be created eagerly when the container starts. I combined it with @PostContruct (for some initialization logic) and @PreDestroy (for some clean up). However whatever I tried it does not work in my JBOSS 7 EAP (which is JEE 7).
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Startup
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class ProvisioningDataForApplicationLifecycle {
@PostConstruct
private void init() {
// when app is deployed
LOG.info("called init")
}
@PreDestroy
private void cleanUp() {
// when app is undeployed
LOG.info("called destroy")
}
}
public void someOtherClass(){
@Inject
@EJB
private ProvisioningDataForApplicationLifecycle pdal;
...
}
I searched a lot 1, 2, 3 ... but nothing helped. The only thing which I got working is using
public void initEnvironment(@Observes @Initialized(@ApplicationScoped.class))
However the @Startup should be working. So any idea why it is not?
Update:
I think I realized the problem: The class is part of an (eclipse) utility project (facet) which is added to the final EAR as a jar file under the lib
folder. So it is not an an EJB. Thus the @Startup
and @Singleton
annotation will probably not work. Or is there the possibility to make it work for non EJB classes?