4 Replies Latest reply on Nov 27, 2014 4:02 AM by oldwhy

    EJB 3.1 Singleton Session Bean @PreDestroy method not called

    oldwhy

      Hello, All

       

      I have a Singleton Session Bean executing background tasks:

       

       

      @Singleton

      @ConcurrencyManagement(ConcurrencyManagementType.BEAN)

      @TransactionManagement(TransactionManagementType.BEAN)

      @Startup

      public class TaskQueue {

       

          private static final Logger LOGGER = Logger.getLogger("TaskQueue");

       

          @Resource

          private SessionContext sessionContext;

       

          private final ArrayList<Runnable> tasks = new ArrayList<Runnable>();

          private boolean running = false;

       

          @PostConstruct

          public void postConstruct() {

              LOGGER.info("postConstruct");

       

              running = true;

              sessionContext.getBusinessObject(TaskQueue.class).taskLoop();

          }

       

          @PreDestroy

          public void preDestroy() {

              LOGGER.info("preDestroy");

              running = false;

              synchronized (tasks) {

                  tasks.notifyAll();

              }

          }

       

          public void addTask(Runnable r) {

              synchronized (tasks) {

                  tasks.add(r);

                  tasks.notifyAll();

              }

          }

       

          @Asynchronous

          public void taskLoop() {

              while (running) {

                  Runnable task;

                  synchronized (tasks) {

                      LOGGER.info("Fetching next task...");

                      if (tasks.isEmpty()) {

                          try {

                              LOGGER.info("Task queue is empty. Waiting...");

                              tasks.wait();

                              LOGGER.info("Resumed");

                              continue;

                          } catch (InterruptedException e) {

                              break;

                          }

                      }

       

                      task = tasks.remove(0);

                  }

       

                  LOGGER.info("Executing task...");

                  task.run();

              }

       

              running = false;

              LOGGER.info("Task queue exited");

          }

      }

       

      When I tried to stop the module, undeploy the module, or stop the server, the preDestroy() method did not get called, and the stopping/undeployment process won't proceed. The only way to stop the server is to kill the Java process.

      I am using Jboss EAP 6.0.

      What's going wrong with my code? How to fix it, or what's the alternative way to do background task queue processing with EJB 3.1?

      Thanks!

      -

        老y