@Singleton @Startup fully Asynchronous?

2019-06-01 08:17发布

问题:

I am using the @Singleton and @Startup annotations together to create a long running process. By using these annotation can I safely assume that my process will be a single thread running fully asynchronously?

回答1:

You can use these annotations just at the class-level, so I'm not sure what 'process' do you refer to (@PostConstruct method?)

I don't remember any part of the EJB 3.1 specification which talks about the required asynchronous singleton initialization.

4.8.1 Singleton Initialization:

(...)the bean developer can optionally configure the Singleton for eager initialization. If the Startup annotation appears on the Singleton bean class (...) 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.

It rather depends on the Application Server vendor what technique of initialization he choose. I would not assume that the loading will occur in a separate process but rather just that it will occur during server startup and before serving clients.

ENTERED FROM COMMENTS: what about providing a separate asynchronous EJB method for your time-consuming operation which will be invoked from your singleton initializer EJB?