@Singleton @Startup @PostConstruct 方法保证在 EJB 可用于客户端调用之前 return?

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

WebSphere 8.0 上的 Java EE 6 应用程序 运行 的上下文中,我需要先执行一些启动任务,然后才能执行任何业务方法。为此目的使用 @Startup@Singleton bean 似乎是一个很有前途的解决方案。但是,我并不完全清楚应用程序生命周期究竟是什么样子。 EJB 3.1 spec 声明如下:

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. 最后一句,初始化到底是什么?在使企业 bean 可用于客户端请求之前,容器是否会等待 @Startup bean 的 @PostConstruct 方法到 return?

  2. 说到 客户端请求,在这种情况下,使用 @Scheduled 注释的 EJB 方法的计划执行算作一次吗?

我需要保证在应用程序的任何 EJB 中的任何业务方法可以 运行 之前在应用程序启动时执行某些代码,无论是通过客户端调用还是计划执行。 运行在 @Singleton@Startup bean 的 @PostConstruct 方法中启动代码是否提供这样的保证?如果不是,是否有任何其他方法可以保证这种行为?

  1. 是的,容器等待模块("EJB application")中所有 @Startup 个 bean 的 @PostConstruct 方法到 return 之后才允许任何客户端请求。
  2. 是的,正如知识中心的 Developing singleton session beans 主题所暗示的那样,WebSphere Application Server 就是这种情况,它说 "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"。换句话说,计时器回调调用将等待 @PostConstruct 方法完成,因此 @PostConstruct 方法不能等待计时器回调调用完成。