gpt4 book ai didi

java - 启动单例 bean 可以延迟 J2EE 容器初始化吗?

转载 作者:行者123 更新时间:2023-12-02 09:57:23 26 4
gpt4 key购买 nike

根据Java EE 6 Tutorial :

The EJB container is responsible for determining when to initialize a singleton session bean instance unless the singleton session bean implementation class is annotated with the javax.ejb.Startup annotation. In this case, sometimes called eager initialization, the EJB container must initialize the singleton session bean upon application startup. The singleton session bean is initialized before the EJB container delivers client requests to any enterprise beans in the application.

我的RESTEasy应用程序由 Thorntail 提供服务不使用任何EJB,但它使用@Startup、@Singleton和@PostConstruct注释在应用程序服务器初始化期间运行长数据库更新任务。它是这样的:

@Startup
@Singleton
public class StartupTask {

@PostConstruct
void init {
// database update task
}
}

在此任务完成之前我的应用程序能够处理 HTTP 请求吗?

我的问题类似于this one .

最佳答案

任何带有javax.ejb.Singleton注释的bean是一个 EJB,并遵守您在问题中引用的条件。

您可以通过将“数据库更新任务”逻辑移至第二个 EJB 来避免延迟:

@Stateless
public class DatabaseUpdater {

@Resource
private Datasource dataSource;

// OR

@PersistenceContext
private EntityManager em;

@javax.ejb.Asynchronous
public void updateDatabase() {
// beware of long running transaction timeouts in here!
...
}

}

然后从 @Startup bean 调用它:

@Startup
@Singleton
public class StartupTask {

@EJB
private DatabaseUpdater databaseUpdater;

@PostConstruct
void init {
databaseUpdater.updateDatabase();
}

}

关于java - 启动单例 bean 可以延迟 J2EE 容器初始化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com