Discussions

EJB programming & troubleshooting: EJB startup and shutdown

  1. EJB startup and shutdown (2 messages)

    Hi I am having a little trouble ensuring that my J2EE application is started and stopped correctly.

    When the app is deployed I use a servlet to ensure that
    a 'startup' method on a session bean is invoked. (are there
    better solutions that are not dependent on the app server
    being used?)

    Now I also need to clean some stuff up if the application
    is undeployed. The problem here is that by the time my
    'shutdown' method is invoked some of the EJB's have already
    been undeployed. Is there a way to get some code to execute whenever the application is being undeployed but before any
    EJB's have actually been undeployed?


    Thanks,
    Hani

    Threaded Messages (2)

  2. EJB startup and shutdown[ Go to top ]

    How about putting the logic in the ejbRemove() methods of your session beans, or the unsetEntityContext() of your entity beans? That is the only platform-independent thing I can think of, although I admit it is pretty limiting.

    If that won't work, I suggest you try the features of your server. Most servers have some kind of startup/shutdown hooks for applications.
  3. EJB startup and shutdown[ Go to top ]

    Hi!

    So I'm not the only one missing the <load-on-startup> in the EJB specification. (And I also miss <on-shutdown> in the Servlet spec.)

    If you use WebLogic Server, you can use the sartup and shutdown classes from verison 6. In newer version, I think there is an application listener with a lot of nice hooks to put your own code in. But then you are outside the specifications!

    If you want to follow the spec., there is one quite nice way to ensure code is executed at start (or at least before you execute any EJB code): Make a Singeleton (I will call it LoadOnStartup). Synchronize getIntance() and create the instance on the first call.

    Add the following line in every (!) EJB bean class:

        private static LoadOnStartup neverUsed = LoadOnStartup.getInstance();

    Then you are guaranteed the instance of LoadOnStartup is created (and you init logic is executed) before you execute any of you application code.

    This is not nice, but it is the best way I have found...

    When it comes to write code you are guaranteed to execute on start, I don't have any solution to that problem. The ejb methods are not guaranteed to execute on application stop, and therefore you cannot put you code in them.

    This is difficult :-)

    /Tomas