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.
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?
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?
@Startup Marks a singleton bean for eager initialization during the application startup sequence. @DependsOn Used to express an initialization dependency between singleton components.
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.
@PostConstruct
method of all @Startup
beans in the module ("EJB application") to return before allowing any client requests.@PostConstruct
methods to complete, so @PostConstruct
methods must not wait for timer callback invocations to complete.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With