0

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?

Lonzak
  • 9,334
  • 5
  • 57
  • 88
  • 1
    Is your `@Singleton` an EJB annotation? Because there is `@javax.ejb.Singleton` (this is what you want) and `@javax.inject.Singleton` (not what you want). – Siliarus Apr 05 '18 at 09:58
  • thanks for pointing that out however that was not the case... – Lonzak Apr 05 '18 at 10:56
  • 1
    that configuration should work. when you check logs of jboss when creating the ejb beans in jndi namespace, is the bean logged with its global, module and local lookupnames? – maress Apr 05 '18 at 11:18
  • There are no such log entries which made me realize the class is not an EJB. See update of the post... – Lonzak Apr 06 '18 at 12:50

0 Answers0