3 Replies Latest reply on Feb 28, 2005 10:07 PM by Steven Peh

    Long-lived MDBs and application server shutdown

    twalsh2 Newbie

      I've noticed that while an MDB is executing, JBoss AS is unable to undeploy the EAR containing the MDB. Since the deployed EAR cannot be undeployed until the MDB completes execution of its onMessage method, a single running MDB can delay the (clean) shutdown of the application server.

      I have some MDBs which will be implementing long-running tasks (in some cases several hours). Does anyone have any strategies for dealing with clean server shutdowns in these cases? Any way for the MDB to be notified of the shutdown so it could cooperatively shut itself down and allow the app server to shut down?

        • 1. Re: Long-lived MDBs and application server shutdown
          Steven Peh Newbie

          umm... just a suggestion, how bout a shutdown MDB that sets a static flag somewhere or better yet execute an interface callback method which your long running MDBs implement to notify them of the shutdown even... of course you'd have to register a shutdown hook with the container to trigger the shutdown MDB (shutdown hooks would be app container specific).

          If you're using the interface callbacks, you can probably have a singleton register the long running MDBs as listeners and the shutdown hook use the singleton to notify registered listeners of shutdown event.

          Just a suggestion, havent really tried it myself... dont really believe in long running MDBs ;-)

          • 2. Re: Long-lived MDBs and application server shutdown
            twalsh2 Newbie

            A couple of questions on your suggestions:

            >just a suggestion, how bout a shutdown MDB that sets a static flag
            >somewhere or better yet execute an interface callback method which your
            >long running MDBs implement to notify them of the shutdown even

            1) How would you go about getting a reference to all currently executing MDBs so you could call a method they implement? I don't see any obvious reference to them in the JNDI tree.



            >Just a suggestion, havent really tried it myself... dont really believe
            >in long running MDBs ;-)

            2) Within a J2EE context, what component would you use to represent a potentially long-lived and asynchronous background task? I suppose a JMX MBean could be used, but I am trying to limit the app-server specific components as much as possible.

            • 3. Re: Long-lived MDBs and application server shutdown
              Steven Peh Newbie

               


              >just a suggestion, how bout a shutdown MDB that sets a static flag
              >somewhere or better yet execute an interface callback method which your
              >long running MDBs implement to notify them of the shutdown even

              1) How would you go about getting a reference to all currently executing MDBs so you could call a method they implement? I don't see any obvious reference to them in the JNDI tree.


              Well, like i said, you'd use a singleton. Then for in each MDB's onMessage method, there will be something like this to register and deregister itself...

              public void MyMDB implements MessageDrivenBean, MessageListener, MyShutdownEventListener
              
              public void onMessage(Message msg){
               // register myself
               MyShutdownSingleton.getInstance().addShutdownListener(this);
              
               // do my long running task
               ....
              
               // if i complete my long running task and no shutdown event yet, then
               // deregister myself
               myShutdownSingleton.getInstance().removeShutdownListener(this);
              }
              
              // implement my shutdown event code here
              public void myShutdownEvent(){
              ...
              }
              


              In your shutdown hook, you'd just call the MyShutdownSingleton.fireShutdownEvent() method which basically iterates through all registered listeners and call their myShutdownEvent method.

              Anyway like i said, havent tried this in a J2EE context. Interface callbacks are quite common when developing Swing apps. Just an idea thrown in for you to try if you want, use at your own risk. Good luck.


              Cheers.