@Singleton, @Startup, @PostConstruct doesn't work on EJB3.1 and Glassfishv3.0.1

11,053

You have invalid type of bean as an attribute of @DependsOn. DependsOn is for expressing dependency between two Singleton session beans, not between Singleton and Stateless. You should change SchedulerEJB to be Singleton or remove dependency.

If you decide change SchedulerEJB to Singleton, then @DepensOn is also not needed, because (from EJB 3.1 specification):

Note that if one Singleton merely needs to invoke another Singleton from its PostConstruct method, no explicit ordering metadata is required. In that case, the first Singleton would merely use an ejb reference to invoke the target Singleton. There, the acquisition of the ejb reference (either through injection or lookup) does not necessarily imply the actual creation of the corresponding Singleton bean instance.

Share:
11,053
Mark Joseph Del Rosario
Author by

Mark Joseph Del Rosario

Updated on June 04, 2022

Comments

  • Mark Joseph Del Rosario
    Mark Joseph Del Rosario almost 2 years

    I have a problem with this setup and I can't even view the logs.

    This is my @Singleton @Startup EJB:

    @Singleton
    @Startup
    @DependsOn("SchedulerEJB")
    public class SchedulerStartUp {
    
        private static Logger log = Logger.getLogger(SchedulerStartUp.class);
    
        @EJB
        SchedulerEJB schedEJB;  
    
        @PostConstruct
        public void atStartup() {
           log.info("startUp")
           System.out.println("startUp");
    
           schedEJB.cancelTimer("EBlastScheduler");
           schedEJB.createTimer("*/1", "*", "*");
        }
    }
    

    The SchedulerEJB:

     @Stateless
     public class SchedulerEJB {
    
        @Resource
        TimerService timerService;         
    
        public cancelTimer(String timerInfo){/*...*/}
    
        public createTimer(String sec, String min, String hour) {/*...*/}
    
        @Timeout
        public void execute(Timer timer) {/*...*/}
     }
    

    Maven pom:

     //Been Using Glassfishv3.0.1 and EJB3.1 with a dependency of:
     <dependency>
         <groupId>javax</groupId>
         <artifactId>javaee-api</artifactId>
         <version>6.0</version>
         <scope>provided</scope>
     </dependency>
    
  • Mark Joseph Del Rosario
    Mark Joseph Del Rosario over 12 years
    But I need it to be @Stateless, any workaround? maybe I should remove '@DependsOn' totally? but will my '@EJB' work? :(
  • Mark Joseph Del Rosario
    Mark Joseph Del Rosario over 12 years
    '@EJB' doesn't work, any way to initialize bean named 'SchedulerEJB' above?
  • Mikko Maunu
    Mikko Maunu over 12 years
    I haven't have such a situation where I would call stateless bean from singleton, but I assume removing @DependsOn should work, assuming that there is not any hidden need in other parts of code why SchedulerEJB should be initialized before. Container should guarantee that there is instance available with your current code without any additions.
  • Mark Joseph Del Rosario
    Mark Joseph Del Rosario over 12 years
    Thanks, its now clear! to other guyz reading this: "never import the wrong library just like what i did, i imported com.google.inject.Singleton instead of javax.ejb.Singleton" Thanks again!
  • zerologiko
    zerologiko over 10 years
    I know it's an old question, On Jboss 7.x a I just used a @DependsOn to reference a stateless bean from a singleton bean: I don't think it's EJB3 compliant, but it works. Source this ticket: bugzilla.redhat.com/show_bug.cgi?id=958217