@PreDestroy method not called in @Singleton on JBoss EAP 6.3.0

此生再无相见时 提交于 2021-01-27 21:10:45

问题


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.

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");
    }
}

During startup of the server, the @PostConstruct message appears in the log. But when shutting down the server, no log message appears.

How can I make the server call the @PreDestroy method?

EDIT: Since the @PreDestroy method is not the appropriate location to clean up the database, this question is obsolete.


回答1:


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,

But for your help i attach this which i took from a book (Mastering EJB 3.0),



来源:https://stackoverflow.com/questions/42994279/predestroy-method-not-called-in-singleton-on-jboss-eap-6-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!