How to execute code at the start/stop of an Application that includes EJB 3.0 projects

Technote (FAQ)


Question

How do you execute code at the start and stop of an Application that includes EJB 3.0 projects?

Cause

The @Startup annotation is new in the EJB 3.1 specification and it is not supported in the EJB 3.0 specification.

Answer

The information in the remainder of the technote applies to Websphere Application Server 7.0.0.x.

Startup Beans

Among the WebSphere Application Server extensions, you can find Startup beans. Startup beans expose a start and a stop method, which are invoked by the server if the Startup beans service in enabled (see Related URL: "Startup Beans Service setup").

Startup beans are configured by creating one Stateless Session bean that uses a specific Home and Remote interface, provided by WebSphere Application Server.

There are two names for these interfaces, depending on whether the startup Bean must execute at Application Startup or Module Startup:

  1. Application Startup
    1. com.ibm.websphere.startupservice.AppStartUpHome (Home)
    2. com.ibm.websphere.startupservice.AppStartUp (Remote)
  2. Module Startup
    1. com.ibm.websphere.startupservice.ModStartUpHome (Home)
    2. com.ibm.websphere.startupservice.ModStartUp (Remote)
The bean class must implement the start and stop methods provided by the Remote Interface.

The nature of these Remote interfaces is such that they cannot be used in an EJB 3.0, since the Remote interface extends: javax.ejb.EJBObject.

You can, however, include one EJB 2.1 project in an EAR 5.0 project, and add a Startup Bean in this EJB 2.1 project, which will execute at Application Startup.

Example:


package ejbs;

/**
 * Bean implementation class for Enterprise Bean: StartupCode
 */
public class StartupCodeBean implements javax.ejb.SessionBean {
 public boolean start() throws java.rmi.RemoteException{
 System.out.println("Application Start Code "+this);

 return true;
 };
 
 
 public void stop() throws java.rmi.RemoteException{
 
 System.out.println("Application Stop Code "+this);

 };
static final long serialVersionUID = 3206093459760846163L;
private javax.ejb.SessionContext mySessionCtx;
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
}

Snippet from ejb-jar.xml


 <enterprise-beans>
 <session id="StartupCode">
  <ejb-name>StartupCode</ejb-name>
  <home>com.ibm.websphere.startupservice.AppStartUpHome</home>
  <remote>com.ibm.websphere.startupservice.AppStartUp</remote>
  <ejb-class>ejbs.StartupCodeBean</ejb-class>
  <session-type>Stateless</session-type>
  <transaction-type>Container</transaction-type>
  </session>
  </enterprise-beans>


Servlets

You can add to the EAR 5.0 a Dynamic Web project containing a Servlet class, that extends javax.servlet.http.HttpServlet. For the Servlet, you can implement thet "init" and "destroy" methods.

Additionally, to ensure that the servlet is loaded at startup, you must use the Deployment Descriptor (web.xml) and configure the parameter "load-on-startup", which takes a numerical parameter.

Example:

Servlet code:


package com.ibm.rational.support;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
 * Servlet implementation class LifeCycleServlet
 */
public class LifeCycleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LifeCycleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
System.out.println("Servlet init "+this);
}

/**
* @see Servlet#destroy()
*/
public void destroy() {
System.out.println("Servlet destroy "+this);
}

}

Snippet from web.xml:


<servlet>
  <description />
  <display-name>LifeCycleServlet</display-name>
  <servlet-name>LifeCycleServlet</servlet-name>
  <servlet-class>com.ibm.rational.support.LifeCycleServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>


Stateless EJB 3.0 lifecycle methods (@PostConstruct and @PreDestroy)

For a stateless EJB 3.0, you can implement two methods annotated with @PostContruct and @PreDestroy. These methods should be defined in the bean class and they should return "void", for the rest they can have an arbitrary name. These callback or lifecycle methods will be invoked by the container after an instance is created and before it is destroyed. You have however no direct control over when the container will create an instance or destroy a given instance. After an instance is created to serve a client request, it is returned to the pool, in order to be given to other clients. It will eventually be destroyed when the application is stopped.

You can tune some parameters related to the size of the pool (see Related URL: Managing EJB Containers)

Example:

//Stateless Session Bean Class with callback Lifecycle methods
package com.ibm.rational.support;

import java.rmi.RemoteException;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Remote;
import javax.ejb.RemoteHome;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class LifeCycleDemoBean
 */
@Stateless

public class LifeCycleDemoBean implements LifeCycleDemoBeanLocal {

    /**
     * Default constructor.
     */
    public LifeCycleDemoBean() {
    System.out.println("In default Constructor "+this);
    }
    @PostConstruct
    public void method1(){
    System.out.println("In PostConstruct "+this);
   
    }
    @PreDestroy
    public void method2(){
    System.out.println("In PreDestroy "+this);
   
    }
    public String hello(){
    return "Hello "+this;
    }


}

//Local Interface

package com.ibm.rational.support;
import javax.ejb.Local;

@Local
public interface LifeCycleDemoBeanLocal {

String hello();

}


Start at Application Start

You can configure EJBs so that they are initialized at application start time by changing a setting in the Administrative Console (see Related URL: Changing enterprise bean types to initialize at application start time using the administrative console).

You can obtain the same result by adding to the EJB Module the file ibm-ejb-jar-ext.xml (in RAD, right click on the EJB 3.0 Project and choose: Java EE->Generate WebSphere Extensions Deployment Descriptor). In this file, add a Session Bean (which you have previously added to the EJB Deployment Descriptor, ejb-jar.xml) and select the check box: Start At Application Start (See Related URL: EJB 3.0 application bindings overview).

Comparing the SystemOut with and without this setting configured, you can see that one default contructor is invoked for one bean if this setting is enabled, which would not be invoked otherwise. In general, this setting is related to loading the EJB class and processing the (deployment descriptor) metadata. This is not one way of controlling the instanciation of the EJB at startup: this setting does not trigger the invocation of the bean lifecyscle method marked with @PostConstruct annotation). This setting offers no control over the behavior when the application is stopped.

Example:

Snippet from ibm-jar.xml



<enterprise-beans>
<session>
<ejb-name>LifeCycleDemoBean</ejb-name>
<business-local>com.ibm.rational.support.LifeCycleDemoBeanLocal
</business-local> <ejb-class>com.ibm.rational.support.LifeCycleDemoBean</ejb-class> <session-type>Stateless</session-type>
</session>
</enterprise-beans>

Snippet from ibm-ejb-jar-ext.xml



<session name="LifeCycleDemoBean">
<start-at-app-start value="true"/>
</session>

SystemOut produced when starting and Stopping the Application:


[11/30/09 15:12:38:279 CET] 0000000e CompositionUn A   WSVR0190I: Starting composition unit WebSphere:cuname=LifecycleDEmo in BLA WebSphere:blaname=LifecycleDEmo.
[11/30/09 15:12:38:373 CET] 0000000e ApplicationMg A   WSVR0200I: Starting application: LifecycleDEmo
[11/30/09 15:12:38:373 CET] 0000000e ApplicationMg A   WSVR0204I: Application: LifecycleDEmo  Application build level: Unknown
[11/30/09 15:12:38:436 CET] 0000000e EJBContainerI I   WSVR0037I: Starting EJB jar: StartApplication.jar
[11/30/09 15:12:38:451 CET] 0000000e EJBContainerI I   CNTR0167I: The server is binding the com.ibm.websphere.startupservice.AppStartUpHome interface of the StartupCode enterprise bean in the StartApplication.jar module of the LifecycleDEmo application.  The binding location is: ejb/com/ibm/websphere/startupservice/ModStartUpHome
[11/30/09 15:12:38:467 CET] 0000000e EJBContainerI I   WSVR0057I: EJB jar started: StartApplication.jar
[11/30/09 15:12:38:482 CET] 0000000e EJBContainerI I   WSVR0037I: Starting EJB jar: LifecycleDEmoEJB.jar
[11/30/09 15:12:38:529 CET] 0000000e SystemOut     O In default Constructor com.ibm.rational.support.LifeCycleDemoBean@45564556
[11/30/09 15:12:38:529 CET] 0000000e EJBContainerI I   CNTR0167I: The server is binding the com.ibm.rational.support.LifeCycleDemoBeanLocal interface of the LifeCycleDemoBean enterprise bean in the LifecycleDEmoEJB.jar module of the LifecycleDEmo application.  The binding location is: ejblocal:LifecycleDEmo/LifecycleDEmoEJB.jar/LifeCycleDemoBean#com.ibm.rational.support.LifeCycleDemoBeanLocal
[11/30/09 15:12:38:529 CET] 0000000e EJBContainerI I   CNTR0167I: The server is binding the com.ibm.rational.support.LifeCycleDemoBeanLocal interface of the LifeCycleDemoBean enterprise bean in the LifecycleDEmoEJB.jar module of the LifecycleDEmo application.  The binding location is: ejblocal:com.ibm.rational.support.LifeCycleDemoBeanLocal
[11/30/09 15:12:38:545 CET] 0000000e EJBContainerI I   WSVR0057I: EJB jar started: LifecycleDEmoEJB.jar
[11/30/09 15:12:38:576 CET] 0000000e webapp        I com.ibm.ws.webcontainer.webapp.WebGroupImpl WebGroup SRVE0169I: Loading Web Module: LifeCycleTestServlet.
[11/30/09 15:12:38:607 CET] 0000000e WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_host/LifeCycleTestServlet
[11/30/09 15:12:38:623 CET] 0000000e SystemOut     O Servlet init com.ibm.rational.support.LifeCycleServlet@2fe82fe8
[11/30/09 15:12:38:623 CET] 0000000e servlet       I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [LifecycleDEmo] [/LifeCycleTestServlet] [LifeCycleServlet]: Initialization successful.
[11/30/09 15:12:38:623 CET] 0000000e webcontainer  I com.ibm.ws.wswebcontainer.VirtualHost addWebApplication SRVE0250I: Web Module LifeCycleTestServlet has been bound to default_host[*:9081,*:80,*:9444,*:5063,*:5062,*:443].
[11/30/09 15:12:38:639 CET] 0000000e SystemOut     O Application Start Code ejbs.StartupCodeBean@13c613c6
[11/30/09 15:12:38:639 CET] 0000000e ApplicationMg A   WSVR0221I: Application started: LifecycleDEmo
[11/30/09 15:12:38:639 CET] 0000000e CompositionUn A   WSVR0191I: Composition unit WebSphere:cuname=LifecycleDEmo in BLA WebSphere:blaname=LifecycleDEmo started.
[11/30/09 15:14:49:889 CET] 0000001a AdminHelper   A   ADMN1021I: An attempt is made to stop the server1 server.
[11/30/09 15:14:49:951 CET] 00000018 SystemOut     O Application Stop Code ejbs.StartupCodeBean@13c613c6

SystemOut produced when starting the application, invoking the EJB and Stopping the Application


11/30/09 15:24:48:889 CET] 0000001b CompositionUn A   WSVR0190I: Starting composition unit WebSphere:cuname=LifecycleDEmo in BLA WebSphere:blaname=LifecycleDEmo.
[11/30/09 15:24:48:982 CET] 0000001b ApplicationMg A   WSVR0200I: Starting application: LifecycleDEmo
[11/30/09 15:24:48:982 CET] 0000001b ApplicationMg A   WSVR0204I: Application: LifecycleDEmo  Application build level: Unknown
[11/30/09 15:24:49:029 CET] 0000001b EJBContainerI I   WSVR0037I: Starting EJB jar: StartApplication.jar
[11/30/09 15:24:49:061 CET] 0000001b EJBContainerI I   CNTR0167I: The server is binding the com.ibm.websphere.startupservice.AppStartUpHome interface of the StartupCode enterprise bean in the StartApplication.jar module of the LifecycleDEmo application.  The binding location is: ejb/com/ibm/websphere/startupservice/ModStartUpHome
[11/30/09 15:24:49:061 CET] 0000001b EJBContainerI I   WSVR0057I: EJB jar started: StartApplication.jar
[11/30/09 15:24:49:092 CET] 0000001b EJBContainerI I   WSVR0037I: Starting EJB jar: LifecycleDEmoEJB.jar
[11/30/09 15:24:49:107 CET] 0000001b SystemOut     O In default Constructor com.ibm.rational.support.LifeCycleDemoBean@66f466f4
[11/30/09 15:24:49:107 CET] 0000001b EJBContainerI I   CNTR0167I: The server is binding the com.ibm.rational.support.LifeCycleDemoBeanLocal interface of the LifeCycleDemoBean enterprise bean in the LifecycleDEmoEJB.jar module of the LifecycleDEmo application.  The binding location is: ejblocal:LifecycleDEmo/LifecycleDEmoEJB.jar/LifeCycleDemoBean#com.ibm.rational.support.LifeCycleDemoBeanLocal
[11/30/09 15:24:49:123 CET] 0000001b EJBContainerI I   CNTR0167I: The server is binding the com.ibm.rational.support.LifeCycleDemoBeanLocal interface of the LifeCycleDemoBean enterprise bean in the LifecycleDEmoEJB.jar module of the LifecycleDEmo application.  The binding location is: ejblocal:com.ibm.rational.support.LifeCycleDemoBeanLocal
[11/30/09 15:24:49:123 CET] 0000001b EJBContainerI I   WSVR0057I: EJB jar started: LifecycleDEmoEJB.jar
[11/30/09 15:24:49:154 CET] 0000001b webapp        I com.ibm.ws.webcontainer.webapp.WebGroupImpl WebGroup SRVE0169I: Loading Web Module: LifeCycleTestServlet.
[11/30/09 15:24:49:170 CET] 0000001b WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_host/LifeCycleTestServlet
[11/30/09 15:24:49:186 CET] 0000001b SystemOut     O Servlet init com.ibm.rational.support.LifeCycleServlet@344c344c
[11/30/09 15:24:49:186 CET] 0000001b servlet       I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [LifecycleDEmo] [/LifeCycleTestServlet] [LifeCycleServlet]: Initialization successful.
[11/30/09 15:24:49:201 CET] 0000001b webcontainer  I com.ibm.ws.wswebcontainer.VirtualHost addWebApplication SRVE0250I: Web Module LifeCycleTestServlet has been bound to default_host[*:9081,*:80,*:9444,*:5063,*:5062,*:443].
[11/30/09 15:24:49:217 CET] 0000001b SystemOut     O Application Start Code ejbs.StartupCodeBean@73f473f4
[11/30/09 15:24:49:248 CET] 0000001b ApplicationMg A   WSVR0221I: Application started: LifecycleDEmo
[11/30/09 15:24:49:248 CET] 0000001b CompositionUn A   WSVR0191I: Composition unit WebSphere:cuname=LifecycleDEmo in BLA WebSphere:blaname=LifecycleDEmo started.
[11/30/09 15:24:54:357 CET] 0000001f servlet       I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [LifecycleDEmo] [/LifeCycleTestServlet] [TestServlet]: Initialization successful.
[11/30/09 15:24:54:357 CET] 0000001f SystemOut     O In default Constructor com.ibm.rational.support.LifeCycleDemoBean@6b3e6b3e
[11/30/09 15:24:54:357 CET] 0000001f SystemOut     O In PostConstruct com.ibm.rational.support.LifeCycleDemoBean@6b3e6b3e
[11/30/09 15:25:34:826 CET] 0000001b CompositionUn A   WSVR0192I: Stopping composition unit WebSphere:cuname=LifecycleDEmo in BLA WebSphere:blaname=LifecycleDEmo.
[11/30/09 15:25:34:826 CET] 0000001b ApplicationMg A   WSVR0217I: Stopping application: LifecycleDEmo
[11/30/09 15:25:34:826 CET] 0000001b SystemOut     O Application Stop Code ejbs.StartupCodeBean@73f473f4
[11/30/09 15:25:34:842 CET] 0000001b servlet       I com.ibm.ws.webcontainer.servlet.ServletWrapper doDestroy SRVE0253I: [LifecycleDEmo] [/LifeCycleTestServlet] [TestServlet]: Destroy successful.
[11/30/09 15:25:34:842 CET] 0000001b SystemOut     O Servlet destroy com.ibm.rational.support.LifeCycleServlet@344c344c
[11/30/09 15:25:34:842 CET] 0000001b servlet       I com.ibm.ws.webcontainer.servlet.ServletWrapper doDestroy SRVE0253I: [LifecycleDEmo] [/LifeCycleTestServlet] [LifeCycleServlet]: Destroy successful.
[11/30/09 15:25:34:842 CET] 0000001b EJBContainerI I   WSVR0041I: Stopping EJB jar: LifecycleDEmoEJB.jar
[11/30/09 15:25:34:842 CET] 0000001b SystemOut     O In PreDestroy com.ibm.rational.support.LifeCycleDemoBean@6b3e6b3e
[11/30/09 15:25:34:842 CET] 0000001b EJBContainerI I   WSVR0059I: EJB jar stopped: LifecycleDEmoEJB.jar
[11/30/09 15:25:34:857 CET] 0000001b EJBContainerI I   WSVR0041I: Stopping EJB jar: StartApplication.jar
[11/30/09 15:25:34:857 CET] 0000001b EJBContainerI I   WSVR0059I: EJB jar stopped: StartApplication.jar
[11/30/09 15:25:34:857 CET] 0000001b ApplicationMg A   WSVR0220I: Application stopped: LifecycleDEmo
[11/30/09 15:25:35:342 CET] 0000001b CompositionUn A   WSVR0193I: Composition unit WebSphere:cuname=LifecycleDEmo in BLA WebSphere:blaname=LifecycleDEmo stopped.


Related information

Changing enterprise bean types to initialize at applica
Using startup beans
Startup beans service settings
Managing EJB Containers
EJB 3.0 application bindings overview

 DISCLAIMER:

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


LifeCycleDemo.zip


Cross Reference information
Segment Product Component Platform Version Edition
Software Development Rational Software Architect for WebSphere Software EJB Development Linux, Windows 7.5, 7.5.1, 7.5.2, 7.5.3, 7.5.4, 7.5.5

Rate this page:

(0 users)Average rating

Document information


More support for:

Rational Application Developer for WebSphere Software
EJB Development

Software version:

7.5, 7.5.1, 7.5.2, 7.5.3, 7.5.4, 7.5.5

Operating system(s):

Linux, Windows

Reference #:

1412991

Modified date:

2011-04-03

Translate my page

Machine Translation

Content navigation