java Eager / 自动加载 EJB / 在启动时加载 EJB(在 JBoss 上)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2707733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:26:21  来源:igfitidea点击:

Eager / auto loading of EJB / load EJB on startup (on JBoss)

javajbossjakarta-eeejb-3.0

提问by Bozho

EJBs seem to be loaded lazily - whenever accessed.

EJB 似乎是惰性加载的——无论何时访问。

However, I want to initialize them eagerly - i.e. whenever the container starts-up. How is this achieved (in JBoss in particular)

但是,我想急切地初始化它们 - 即每当容器启动时。这是如何实现的(特别是在 JBoss 中)

This topicgives some hints, but isn't quite satisfactory.

这个话题给出了一些提示,但不是很令人满意。

回答by Brett Kail

As of EJB 3.1, singleton beans can be notified of module start and stop:

从 EJB 3.1 开始,可以通知单例 bean 模块启动和停止:

@Singleton
@Startup
public class StartupBean {
    @PostConstruct
    private void postConstruct() { /* ... */ }

    @PreDestroy
    private void preDestroy() { /* ... */ }
}

Prior to EJB 3.1, there is no standard, EJB-only solution. I'd suggest adding a WAR to your EAR and using a servlet-context-listener.

在 EJB 3.1 之前,没有标准的、仅限 EJB 的解决方案。我建议向您的 EAR 添加 WAR 并使用 servlet-context-listener。

回答by Jakub Holy

According to Adam Bien's Real World Java EE Patterns - Rethinking Best Practices (see a summary of the patterns) and the Service Starter pattern, it is indeed as bkail suggests

根据 Adam Bien 的 Real World Java EE Patterns - Rethinking Best Practices(请参阅模式摘要)和 Service Starter 模式,确实如 bkail 所建议的那样

  • with Java EE 6 = EJB 3.1 use @Singleton with @Startup (and perhaps also with @DependsOn)
  • prior to that the only standard and portable way is to use the Servlet API, e.g. a HttpServlet starting the EJBs in its init() method and load-on-startup set to 1 in web.xml.
  • 使用 Java EE 6 = EJB 3.1 将 @Singleton 与 @Startup 一起使用(也可能与 @DependsOn 一起使用)
  • 在此之前,唯一的标准和可移植方式是使用 Servlet API,例如 HttpServlet 在其 init() 方法中启动 EJB,并在 web.xml 中将 load-on-startup 设置为 1。