javajakarta-eewebsphereejb-2.x

ejbCreate() method of MDB not called at the application startup on Websphere Application Server


I try to deploy an EJB 2.1 based application on a Websphere Application Server v7.0.0.23 with some Spring initialization code into the onEjbCreate method of the MDB:

@Override
protected void onEjbCreate() {      
    getBeanFactory().getBean("myBean");     
}

But this method is called on the reception of the message and not at the application startup. How can i force WAS to instanciate my MDB before the message reception ?


Solution

  • Edited my response to be more correct and highlight the information Tracy mentioned:

    By default, the EJBContainer defers initializing beans until they are first called. Furthermore, ejbCreate() is called each time a new bean is created. MDB instances are not created until a relational resource adapter (RAR) asks for one; typically when a message is delivered. Since, ejbCreate runs for every instance, so if the RAR asks for say 50, then ejbCreate will be called 50 times.

    You could programmatically look up the bean and create it somehow before it receives a message to trigger your initialization code, but you probably don't want to run your initialization code every time a bean is created anyway, so the best option is to use a startup bean. Since you are using EJB2.1 beans and Was7 EJB Container has a 'legacy' startup bean

    In EJB3.0 Startup Singleton beans were introduced which can be added either by annotating the class with @Singleton @Startup or configured on individual beans with ejb-jar.xml file:

    <session name="[bean-name]">
        <start-at-app-start value="true"/>
    </session>
    

    Moving up WAS/EJB versions and using this is your best bet.