4 Replies Latest reply on Sep 22, 2008 11:01 AM by jaikiran pai

    @PostConstruct and @PreDestroy in JBoss AS 5.0.0CR2

    Anders Jensen Newbie

      Hi,

      I have started deploying an internal Java EE ejb application of ours in the new JBoss AS 5.0.0CR2, due to the fact that -- as far as I can see -- JBoss 4.x does not support the complete set of features in EJB 3.0. The application was previously developed for Glassfish, but we've decided moving to JBoss for several other technical reasons.

      This question might be a bit complex .Please tell me if you want snippets of the source codes and the beans that we've produced -- I can put it up to a pastie.

      I am developing a simple mail daemon/service) that looks up new entries in a table called mail_events (mapped to an EJB entity called MailEvent). The service runs as a while loop in a separate thread that loads new MailEvents from the database and processes each message according to a template (e.g. sends out a mail to a newly registrated user with login information). I considered using JMS, but this is not possible due to several environmental issues (we depend on other legacy applications that do not speak JMS -- hence we just use a relational database).

      Currently I have made a EJB called QueueExecuterBean that is a stateless session bean which implements QueueExecuterLocal. This bean loads a singleton class, QueueRunner which extends java.lang.Thread. Hence, QueueRunner is the aforementioned thread that polls for new entries in the MailEvents table (using a proper Facade).

      QueueExecuterBean which holds the reference to QueueRunner should be started AS SOON AS the application is deployed. Therefore I've added two methods:

      @PostConstruct
      public void initialize()

      and

      @PreDestroy
      public void exit()

      -- according to the EJB 3.0 specification.

      Nevertheless, when deploying the bean, none of the methods were invoked. Subsequently I tried deploying a servlet that is loaded upon start-up. This servlet refers the QueueExecuterLocal using @EJB annotations -- and invokes a dummy method on the bean in the servlet's init() method. This causes the @PostConstruct annotated method to be called.

      My problem is that the exit() method in the bean is never called. I cannot control nor find out when the bean is shut down. Tihs is a big problem, since I need to ensure that the queue is properly cleaned up and that the QueueRunner thread is properly stopped before undeploying the bean.

      How come this is such a big problem? Is it due to lack of conformance with the EJB 3.0 specification?

      As such, I am open to other solutions regarding the service/daemon design and problem. I am not sure if this is the right solution to the problem.

      Thanks in advance,

      Anders

        • 1. Re: @PostConstruct and @PreDestroy in JBoss AS 5.0.0CR2
          jaikiran pai Master

           

          QueueExecuterBean which holds the reference to QueueRunner should be started AS SOON AS the application is deployed.


          @PostConstruct will not be called when the application is deployed. What you are trying to do, can be achieved through JBoss specific @Service http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/jboss_extensions.html. Unfortunately, one of the users has reported that the @Service feature seems to be having some issues with respect to lifecycle management methods in JBoss-5 CR2. Give it a try and see if it works for you.


          • 3. Re: @PostConstruct and @PreDestroy in JBoss AS 5.0.0CR2
            Anders Jensen Newbie

            @jakiran: I already tried the @Service extension for EJB 3.0 in JBoss but it didn't work out properly.

            @wolfc: Will this bug be fixed in the next release of JBoss?

            As such, I am open to any other design solution to this problem. I am not saying that my @PostConstruct/@PreDestroy solution with an integrated singleton thread is the optimal solution.

            Could you give any other architectural advises on the event queue application?

            • 4. Re: @PostConstruct and @PreDestroy in JBoss AS 5.0.0CR2
              jaikiran pai Master

               

              "aojensen" wrote:
              @jakiran: I already tried the @Service extension for EJB 3.0 in JBoss but it didn't work out properly.


              When you say, it doesn't work, what exactly happens? Any exceptions?

              "aojensen" wrote:

              I am developing a simple mail daemon/service) that looks up new entries in a table called mail_events (mapped to an EJB entity called MailEvent). The service runs as a while loop in a separate thread that loads new MailEvents from the database and processes each message according to a template (e.g. sends out a mail to a newly registrated user with login information). I considered using JMS, but this is not possible due to several environmental issues (we depend on other legacy applications that do not speak JMS -- hence we just use a relational database).

              Currently I have made a EJB called QueueExecuterBean that is a stateless session bean which implements QueueExecuterLocal. This bean loads a singleton class, QueueRunner which extends java.lang.Thread. Hence, QueueRunner is the aforementioned thread that polls for new entries in the MailEvents table (using a proper Facade).

              As such, I am open to any other design solution to this problem. I am not saying that my @PostConstruct/@PreDestroy solution with an integrated singleton thread is the optimal solution.

              Could you give any other architectural advises on the event queue application?


              Creating threads in the EJB is not recommended. Instead of polling through a thread, you could have a scheduler which has a job scheduled every x seconds/minutes. The job will then look for any new MailEvents in the database. Maybe you can use this http://wiki.jboss.org/wiki/QuartzSchedulerIntegration