在JBoss EAP 6.3.0上的@Singleton中未调用@PreDestroy方法 [英] @PreDestroy method not called in @Singleton on JBoss EAP 6.3.0
问题描述
在我的JavaEE应用程序中,我有一个@Singleton
类,其中包含一些@Scheduled
方法.此外,还有使用@PostConstruct
和@PreDestroy
的方法来设置和清理数据库. (为简单起见,我只登录了示例,因为它可以重现该问题.)该应用程序必须在JBoss EAP 6.3.0.GA服务器上运行.
In my JavaEE application, I have a @Singleton
class containing some @Scheduled
methods. Furthermore there are methods with @PostConstruct
and @PreDestroy
to set up and clean up the database. (For the sake of simplicity, I just have logging in the example, since that reproduces the problem.) The application has to run on a JBoss EAP 6.3.0.GA server.
虽然@PostConstruct
方法可以正常工作,但是当我关闭服务器时(在eclipse中按下 stop the server 按钮时,或者从shutdown
命令使用shutdown
命令时,都不会调用@PreDestroy
jboss-cli).这是一些代码,它重现了这个问题:
While the @PostConstruct
method works fine, @PreDestroy
is not called when I shutdown the server (neither when pressing the stop the server button in eclipse nor when using a shutdown
command from jboss-cli). Here is some code, which reproduces the problem:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
@Startup
public class TimerBean {
private static final Logger log = LoggerFactory.getLogger(TimerBean.class);
@PostConstruct
private void postConstruct() {
log.info("PostConstruct called");
}
@PreDestroy
private void preDestroy() {
log.info("PreDestroy called");
}
}
在服务器启动期间,@PostConstruct
消息出现在日志中.但是当关闭服务器时,没有日志消息出现.
During startup of the server, the @PostConstruct
message appears in the log. But when shutting down the server, no log message appears.
如何使服务器调用@PreDestroy
方法?
How can I make the server call the @PreDestroy
method?
编辑:由于@PreDestroy
方法不是清理数据库的适当位置,因此此问题已过时.
EDIT: Since the @PreDestroy
method is not the appropriate location to clean up the database, this question is obsolete.
推荐答案
这不是您的问题的答案,这是您在上一条评论中提出的问题的答案. 其实我想不到一个合适的地方去做.其他人可能会帮助您解决这个问题. 但是无论如何,@ PostContruct和@PreDestory可能不是事务的一部分,这就是为什么在这些方法中执行数据库操作不好的原因,
This is not the answer for your question, this is the answer for the question you raised in the last comment. actually i can not think about a right place to do it.someone else might help you to figure it out. but anyway @PostContruct and @PreDestory might not be a part of transaction, that's why it is not good to do DB operations in those methods,
但是,为您的帮助,我附上了我从一本书(精通EJB 3.0)中学到的内容,
But for your help i attach this which i took from a book (Mastering EJB 3.0),
这篇关于在JBoss EAP 6.3.0上的@Singleton中未调用@PreDestroy方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!