1

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 ?

Julien
  • 13
  • 2

1 Answers1

0

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.

olendvcook
  • 86
  • 4
  • It does not seem to work whith the property com.ibm.websphere.ejbcontainer.initializeEJBsAtStartup to 'true'. Maybe because EJBs are MDB but the onEjbCreate() method is still not called at the application startup. – Julien Dec 13 '18 at 09:40
  • Sorry should have noticed, the lifecycle method an ejb calls when created is called `ejbCreate` not `onEjbCreate` so it's probably calling an empty method that was defined in the super class. – olendvcook Dec 13 '18 at 16:01
  • Yes the _ejbCreate_ method on the superclass call the _onEjbCreate_ that i redifined, see [link](https://github.com/fywxin/spring-source-read/blob/master/spring-framework/spring-context/src/main/java/org/springframework/ejb/support/AbstractMessageDrivenBean.java). But the _ejbCreate_ should be called and this is not the case. – Julien Dec 13 '18 at 21:02
  • MDB instances are not created until a relational resource adapter (RAR) asks for one; typically when a message is delivered. Also, ejbCreate runs for every instance, so if the RAR asks for say 50, then ejbCreate will be called 50 times. It sounds like you want something to be called only once per app start, so ejbCreate doesn't seem like a good fit. For WAS v7, I'd recommend a startup bean. If you could move to a later version, then an `@Singleton` `@Startup` bean. – Tracy Dec 13 '18 at 23:40
  • Yeah Tracy is right, initialization != creation and I messed that distinction up. – olendvcook Dec 13 '18 at 23:52
  • That answer my question. Thanks a lot for your help :) – Julien Dec 14 '18 at 09:25