ejb3 @PostConstruct


EJB design: ejb3 @PostConstruct

  1. ejb3 @PostConstruct (1 messages)

    Hello there, I am experimenting with EJB3 on JBoss, developing a stateless bean. Basically once the module has been deployed I need to perform some actions related to load application settings (XML). To do this I've annotated a method as @PostConstruct, which as far as I know from the API instructs the container to invoke it once the bean has been deployed and before get in service. (correct?) Now, I am confused, because from the log on that method looks like is not simply invoked once, but right before each exposed method is called. I only need to call that method once the bean has been deployed, not every time it receives a call. What would be the best approach? And moreover should it be this the expected behave from the container. Thanks in advance Alessandro Ilardo
  2. Hi from there then! :o) Recall EJB concepts, Stateless session beans are actually pooled. Beans are allocated to client requests, according to the container decision. The container decides, depending on the current state of the pool if: - it has to instantiate a new bean that will handle your method call - it gives you an existing stateless session bean from the pool that will handle your method call So if you see that the @PostConstruct method is invoked each time your client calls a method on the bean, well you are actually facing a normal behavior: the container is filling the Stateless session beans pool. Just try to set the pool size to one (which is obviously ridiculous...) and you won't see more than one call to your annotated method. The @PostConstruct is not connected to the deployment but to the bean instantiation. So you basically say to the container "hey, just tell me when a Stateless session bean is instantiated, huh? And after the instantiation, execute the code in the annotated method" As you might already guessed, the @PostConstruct annotation is useful for initialization, like opening a JDBC connection etc. for each bean instance. If you want to load an application setting (so here i assume a common setting that concerns the whole application) you should use DI (using the @Resource annotation) and specify your setting in the deployment descriptor (no worries, it's meant to be used this way). HTH, and good luck Alessendro!