Need help with a particular scenario.
Suppose I have a stateless bean class BeanA where in its static block, I perform some logic which needs BeanB.
One way I can do this is to create a static variable of BeanB and lookup for it in the static block. Like the following:
However, the question here is, how can i make sure that BeanB is actually loaded before BeanA. If not, if BeanA gets loaded first, the static block runs but the lookup for BeanB fails. Any ideas on how to achieve this?
Also, I hope I cannot use injection here since @EJB works only for instance variables and not for statics. So, any other options here?
BeanB will be provided by the container for you, so it shouldn't really matter.
Only after class of BeanB is loaded, the container will parse the @Stateless etc. annotations on it, do the appropriate JNDI bindings for it. And from then onwards as you commented, the container will provide instances of BeanB to any clients that need them. However, here I am talking about a scenario where BeanB class is not yet loaded at all.
And, I have seen this practically in one of my applications where I face a NameNotFound exception in the lookupBeanB() because the BeanB was not yet loaded at that time. However, after the JBoss start up completes, I can clearly see the BeanB binded properly in the JNDIview.
Any further information is greatly appreciated.
Thanks for the suggestion with the annotations from EJB3.1. I think that should help. Will the identical approach(though not the annotations) work with EJB3.0 or is there any other alternative?
Well, as far as I remember in EJB 3.0 there was no @Startup & @Singleton and the only way to achieve such functionality is rather cumbersome - create a ServletContextInitializer which used the EJB - therefore you was sure that it's instance is created. There are a lot of drawbacks of such approach - it requires to use web component, you're dependent on this web component initialization and the last one - it just feels wrong :-)
I am not aware of any other ways of achieving such result in EJB 3.0.
One more thing - I don't remember but was the @PostConstruct annotation available in EJB 3.0? Though you might want to use it if you could do the initialization for each instance, not for whole EJB.
Yes, @Startup and @Singleton are not part of EJB3.0 . And the @PostConstruct runs for each instance not just once for the whole bean.
I think i can go ahead with the singleton concept itself and implement my own singleton class(POJO) just to do the processing that I was planning to do in the static block of my BeanA. The processing logic will be done in the constructor of that singleton so that it is called only once. I do not see any other better approach.
In the PostConstruct of BeanA, I will place the code to get Instance of the singleton. Even if the PostConstruct runs for each instance, the processing logic in the constructor of the singleton will be called only once since its a singleton.
( SCBCD 5, CCENT, SCJP 5 )
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad: