Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Singleton @Startup @PostConstruct method guaranteed to return before EJBs made available for client calls?

In the context of a Java EE 6 application run on WebSphere 8.0, I need to execute a number of startup tasks before any business method can be executed. Using a @Startup, @Singleton bean for this purpose seems like a promising solution. However, it is not entirely clear to me how exactly the application lifecycle would look like. The EJB 3.1 spec states the following:

By default, the container is responsible for deciding when to initialize a Singleton bean instance. However, the bean developer can optionally configure the Singleton for eager initialization. If the Startup annotation appears on the Singleton bean class or if the Singleton has been designated via the deployment descriptor as requiring eager initialization, the container must initialize the Singleton bean instance during the application startup sequence. The container must initialize all such startup-time Singletons before any client requests are delivered to any enterprise bean components in the application.

  1. In the last sentence, what exactly constitutes initialization? Will the container wait for the @PostConstruct method of the @Startup bean to return before making enterprise beans available to client requests?

  2. Speaking of client requests, do scheduled executions of an EJB method with the @Scheduled annotation count as one in this context?

I need to guarantee that some code is executed on application startup before any of the business methods in any of the application's various EJBs can be run, be it through client calls or scheduled executions. Does running the startup code inside the @PostConstruct method of a @Singleton, @Startup bean provide such a guarantee? If not, is there any other way to guarantee this behavior?

like image 623
Lord Zuthulu Avatar asked Sep 22 '16 16:09

Lord Zuthulu


People also ask

What does @startup mean on a @singleton Bean?

@Startup Marks a singleton bean for eager initialization during the application startup sequence. @DependsOn Used to express an initialization dependency between singleton components.

What is a singleton EJB?

Ad. If you need to make sure you are exposing an Enterprise Java Bean as a Singleton, there is a simple approach which requires adding the @Singleton annotation on your EJB. As the name implies a javax. ejb. Singleton is a session bean with a guarantee that there is at most one instance in the application.


1 Answers

  1. Yes, the container waits for the @PostConstruct method of all @Startup beans in the module ("EJB application") to return before allowing any client requests.
  2. Yes, this is the case in WebSphere Application Server as implied by the Developing singleton session beans topic in the Knowledge Center, which says "A PostConstruct method in a singleton bean can create an EJB timer [...] However, to avoid a deadlock, the PostConstruct method must not wait for an EJB timer to run". In other words, timer callback invocations will wait for @PostConstruct methods to complete, so @PostConstruct methods must not wait for timer callback invocations to complete.
like image 179
Brett Kail Avatar answered Nov 06 '22 15:11

Brett Kail