You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 44 Next »



Article donated by: Henry Isidro, Leonard Flournoy, Hernan Cunico

Overview

A typical J2EE application may contain Enterprise JavaBeans or EJBs. These beans contain the application's business logic and live business data. Although it is possible to use standard Java objects to contain your business logic and business data, using EJBs addresses many of the issues of using simple Java objects, such as scalability, lifecycle management and state management.

This document discusses one type of EJB, the Session EJB. This type of EJB is useful for mapping business process flow (or equivalent application concepts). There are two types of Session EJB, stateless and stateful.

EJBs hold conversations with clients. A conversation is basically an interaction between the EJB and the client and these interactions are composed of method calls by the clients to the EJBs. Stateful session beans retain state on behalf of a client. This means that if the state of the bean changes during a client's method call, this state is retained for subsequent calls by the same client. A stateless session bean on the other hand retains no conversational state from method to method. In other words, it is expected to hold its conversational state for only a single method call.

This article is organized in the following sections:

EJB implementation analysis

EJB implementation may vary from one vendor to another. The purpose of this section is to provide a session bean specific feature-to-feature comparison between JBoss and Apache Geronimo so you can clearly identify the differences and plan accordingly before migration.

Feature

JBoss v4

Apache Geronimo (OpenEJB)

Stateful and stateless Session Beans

supported

supported

BMP (Bean Managed Persistence) Entity Beans

supported

supported

CMP (Container Managed Persistence) Entity Beans

supported

supported

Message driven beans (MDBs)

supported

supported

Interoperability using RMI-IIOP or JAXRPC

supported

supported

Ability to expose stateless session beans and MDBs as Web Services

supported

supported

Support for sending and receiving messages via Web Services

supported

supported

Easy provisioning and hot deployment of EJB and JMX-based Web Services

supported

supported

Access to EJBs from external CORBA objects

supported

supported

Back to Top

Sample application

This sample application shows how session beans are used and deployed in a container. There are two clients, one for stateless session beans and another for stateful session beans. Both client applications use the same database which contains a table that stores loan application details. An entity bean is used to connect to and operate on the loan details table. A session bean is then used by the client application to get specific details from the database using the entity bean. The first client creates a stateless session bean and displays a list of denied loan applications via a method defined in that EJB. The second client creates a stateful session bean and adds loan applications to the database.

The following figure illustrates the application flow:

Application Beans

The session bean sample application consists of the following packages:

  • com.ibm.demo.entity
    • CustomerBean - contains the methods that connect to the database and operations to manipulate the data.
    • CustomerHomeRemote - the remote interface for the EJB.
    • CustomerHomeRemote - EJBHome Interface.
  • com.ibm.demo.session.stateful
    • StatefulLoanManagerBean - it has the submitLoanApplication() method that saves new customer loan applications to the database as well as the getSubmitCount() method that returns the number of loans submitted; since this bean is a stateful session bean, it can remember connection details from a session so the loan application submit count is retained for every call to the submitLoanApplication() method.
    • StatefulLoanManagerRemote - the remote interface for the EJB.
    • StatefulLoanManagerHomeRemote - EJBHome Interface.
  • com.ibm.demo.session.stateless
    • LoanManagerBean -it has the deniedLoans() method that is used to get a list of all customers whose loan applications are denied based on the condition that their Annual Salary to Loan Amount ratio is less than 0.1
    • LoanManagerRemote - the remote interface for the EJB.
    • LoanManagerHomeRemote - EJBHome Interface.

Back to Top

Tools used

The tools used for developing and building all the applications are:

Eclipse with JBoss IDE

The Eclipse with JBoss IDE plug-ins was used for development of Java-source code of the sample applications. Eclipse is a very powerful and popular open source development tool. Eclipse can be downloaded from the following URL:

http://www.eclipse.org

Apache Maven

Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM). Maven can manage a project's build, reporting and documentation from a central piece of information.

For this migration example Maven 1.0.2 was used. Maven can be downloaded from the followinf URL:

http://maven.apache.org

Back to Top

Sample database

The sample database for the MDB sample application has only one table. This is an in-memory table. The MEMORY storage engine creates tables with contents that are stored in just in memory. These were formerly known as HEAP tables.

The following table describes the fields of the CUSTOMER table.

Field

data type

id

INTEGER

name

VARCHAR(45)

birthdate

DATE

sss_no

VARCHAR(25)

address

VARCHAR(60)

annual_salary

DOUBLE

loan_amount

DOUBLE

Back to Top

The JBoss environment

This section shows you how and where the sample JBoss reference environment was installed so you can map this scenario to your own implementation. Note that for this migration example JBoss v4.0.2 was used.

Detailed instructions for installing, configuring, and managing JBoss are provided in the product documentation. Check the product Web site for the most updated documents.

The following list highlights the general tasks you will need to complete to install and configure the initial environment as the starting point for deploying the sample application.

  1. Download and install JBoss v4 as explained in the product documentation guides. From now on the installation directory will be referred as <jboss_home>
  2. Create a copy of the default JBoss v4 application server. Copy recursively <jboss_home>\server\default to <jboss_home>\server\<your_server_name>
  3. Start the new server by running the run.sh -c <your_server_name> command from the <jboss_home>\bin directory.
  4. Once the server is started, you can verify that it is running by opening a Web browser and pointing it to this URL: http://localhost:8080. You should see the JBoss Welcome window and be able to access the JBoss console.
  5. Once the application server is up and running, the next step is to install and configure all the remaining prerequisite software required by the sample application. This step is described in the following section.

Back to Top

Install and configure prerequisite software

In order to build and run the Session Bean sample application included in this article, you need to install and configure the build tool and the database that is used by the application.

Modify database settings

This application is using the HSQL database that comes as part of the JBoss bundle. You need to modify the script for creating the database. Edit the localDB.script file located in the following directory:

<jboss_home>\server\<your_server_name>\data\hypersonic

Add at the top of the localDB.script file the content of the following example in order to create the sample HSQL database.

Make sure JBoss is not running at the time of modifying this file.

CREATE MEMORY TABLE CUSTOMER(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR(45),BIRTHDATE DATE,SSS_NO VARCHAR(25),ADDRESS VARCHAR(60),ANNUAL_SALARY DOUBLE,LOAN_AMOUNT DOUBLE)

Configure Maven

As mentioned before, Apache Maven is used to build the binaries for the Session Bean sample application. If you do not have Maven installed this is a good time for doing it.

Apache Maven can be downloaded from the following URL:

http://maven.apache.org

Back to Top

Download the sample application

Download the Session Bean sample application from the following URL:

http://opensource2.atlassian.com/confluence/oss/download/attachments/1155/session.zip

After extracting the zip file, a session directory will be created. From now on, this directory will be referred as <session_home>.

Back to Top

Set the JNDI properties

Open the jndi.properties file located in the <session_home>/jndi directory. Make sure that every property under the JBoss Settings section are uncommented and every property under the Geronimo Settings are commented out.

jndi.properties
####################################################################
### JBoss Settings
####################################################################
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost


####################################################################
### Geronimo Settings
####################################################################
#java.naming.factory.initial=org.openejb.client.RemoteInitialContextFactory
#java.naming.provider.url=localhost:4201
#java.naming.security.principal=username
#java.naming.security.credentials=passwd

Back to Top

Verify the WAR files

Open the web.xml file located in the <session_home>/src/webapp/WEB-INF directory and make sure the following taglib element is not commented out.

Excerpt from the web.xml file
...
	 <taglib>
	 	<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
		<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
	</taglib>
...

Open the two jsp files, stateless.jsp and stateful.jsp located in the <session_home>/src/webapp directory and make sure the following line is present:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

Back to Top

Build the sample application

In order to build the Session Bean sample application a Maven build script has been provided. Open a command line and change directory to <session_home>, in that directory open the project.properties file. Edit the maven.jboss.home property to match your environment.

build.properties
maven.ejb.includes=com/ibm/demo/entity/**, com/ibm/demo/session/**
maven.ejb.excludes=com/ibm/demo/client/**
maven.ejb.src=dd

maven.jboss.home=<jboss_home>
maven.geronimo.home=<geronimo_home>

This sample application requires some jars provided by Geronimo, if you do not have Geronimo installed, refer to The Geronimo environment section for installation guidance.

From a command prompt or shell go to the <session_home> directory and run the following command:

maven ejb war

This will compile the source files and package the EJB jar file. You can see the results in the <session_home>/target directory. The following list shows the dependency jars needed in building and running the Session Bean sample application.

  • geronimo-spec-ejb-2.1-rc4.jar - the EJB spec; needed for compiling source.
  • openejb-core-2.0-G1M5.jar - needed by Geronimo during runtime.
  • geronimo-spec-j2ee-1.4-rc4.jar - needed by Geronimo during runtime.
  • geronimo-security-1.0-M5.jar - needed by Geronimo during runtime.

Note that JBoss doesn't need the last three jars to run the application.

Back to Top

Deploy the sample application

To deploy the Session Bean sample application you just built with Maven, copy the session-ejb-SNAPSHOT.jar and session-ejb.war files from the <session_home>/target directory to the following directory:

<jboss_home>\server\<your_server_name>\deploy

If JBoss is already started, it will automatically deploy and start the applications; otherwise, the applications will be deployed and started at the next startup.

Back to Top

Test the sample application

To test the Session Bean sample application run the following commands from the <session_home> directory:

maven run:stateless - for the stateless client
maven run:stateful - for the stateful client

The following example shows the results of the stateless session client.

E:\session>maven run:stateless
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

build:start:

run:stateless:
    [java] Customers with denied loan applications:
BUILD SUCCESSFUL
Total time: 4 seconds
Finished at: Wed Nov 02 10:08:29 EST 2005

The following example shows the results of the stateful session client.

E:\session>maven run:stateful
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

build:start:

run:stateful:
    [java] Submitting new loan application...
    [java] -> submit count is now = 1
    [java] Submitting new loan application...
    [java] -> submit count is now = 2
    [java] Submitting new loan application...
    [java] -> submit count is now = 3
    [java] Submitting new loan application...
    [java] -> submit count is now = 4
    [java] Submitting new loan application...
    [java] -> submit count is now = 5
    [java] 5 loan applications were submitted.
BUILD SUCCESSFUL
Total time: 2 seconds
Finished at: Wed Nov 02 10:11:51 EST 2005

If you run the stateless client again you will see now some customers with a denied loan application.

E:\session>maven run:stateless
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

build:start:

run:stateless:
    [java] Customers with denied loan applications:
    [java] - Customer1
BUILD SUCCESSFUL
Total time: 2 seconds
Finished at: Wed Nov 02 10:12:21 EST 2005

You can also run the webapp client by opening a browser window and going to the following URL:

http://localhost:8080/session

The following figures illustrate the home page for the Web Application as well as the testing for the stateless and stateful beans depending on your selection on the Web Application home page.

Back to Top

The Geronimo environment

Download and install Geronimo from the following URL:

http://geronimo.apache.org/downloads.html

The release notes available there provide clear instructions on system requirements and how to install and start Geronimo. Throughout the rest of this article we will refer to the Geronimo installation directory as <geronimo_home>.

TCP/IP ports conflict

If you are planning to run JBoss and Geronimo on the same machine consider to change the default service ports on, at least, one of these servers.

Back to Top

Configure resources

For this scenario the Session Bean application will use directly the SystemDatabase from Geronimo. In this case there is no need to set up a new connector for the SystemDatabase since it is already configured as the DefaultDatasource.

Start the Geronimo server

Ensure that Geronimo is up and running. If the server has not been started yet, do so by typing the following command:

<geronimo_home>/bin/startup.sh

Once the server is started you should see a screen similar as the one illustrated in the following example:

E:\geronimo\bin>startup
Booting Geronimo Kernel (in Java 1.4.2_09)...
Starting Geronimo Application Server
[*************] 100%  32s Startup complete
  Listening on Ports:
    1099 0.0.0.0 RMI Naming
    1527 0.0.0.0 Derby Connector
    4201 0.0.0.0 ActiveIO Connector EJB
    4242 0.0.0.0 Remote Login Listener
    8019 0.0.0.0 Tomcat Connector AJP
    8080 0.0.0.0 Jetty Connector HTTP
    8090 0.0.0.0 Tomcat Connector HTTP
    8443 0.0.0.0 Jetty Connector HTTPS
    8453 0.0.0.0 Tomcat Connector HTTPS
   61616 0.0.0.0 ActiveMQ Message Broker Connector
  Started Application Modules:
    EAR: org/apache/geronimo/Console
    WAR: org/apache/geronimo/applications/Welcome
  Web Applications:
    http://hcunico:8080/
    http://hcunico:8080/console
    http://hcunico:8080/console-standard
Geronimo Application Server started

Back to Top

Configure database via Geronimo Console

Access the Geronimo Console by pointing your Web browser to the following URL:

http://localhost:8080/console

Enter system as the username and manager as the password, click Login.

Once logged in, on the bottom left corner from the left navigation panel click on DB Manager. In the text area labeled SQL Command/s enter the following SQL statement and click Run SQL; this will create the table used by the Session Bean.

CREATE TABLE CUSTOMER(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR(45),BIRTHDATE DATE,SSS_NO VARCHAR(25),ADDRESS VARCHAR(60),ANNUAL_SALARY DOUBLE,LOAN_AMOUNT DOUBLE)

Back to Top

Step-by-step migration

When you built the Session Bean sample application, Maven packaged the deployment descriptors for both JBoss jboss.xml and Geronimo openejb-jar.xml as they were already provided by the sample application. These files are located in the <session_home>/dd/META-INF directory.

The following example shows the JBoss deployment descriptor.

jboss.xml
<?xml version="1.0"?>

<jboss>
	<container-configurations>
		<container-configuration>
			<container-name>Standard BMP EntityBean</container-name>
			<commit-option>A</commit-option>
  		</container-configuration>
	</container-configurations>
	<enterprise-beans>
		<entity>
			<ejb-name>CustomerEJB</ejb-name>
			<jndi-name>CustomerHomeRemote</jndi-name>
			<resource-ref>
				<res-ref-name>jdbc/ibm-demo</res-ref-name>
				<jndi-name>java:/DefaultDS</jndi-name>
			</resource-ref>
      		<configuration-name>Standard BMP EntityBean</configuration-name>
		</entity>
		<session>
			<ejb-name>LoanManagerEJB</ejb-name>
			<jndi-name>LoanManagerHomeRemote</jndi-name>
			<ejb-ref>
				<ejb-ref-name>ejb/CustomerHomeRemote</ejb-ref-name>
				<jndi-name>CustomerHomeRemote</jndi-name>
			</ejb-ref>
		</session>
		<session>
			<ejb-name>StatefulLoanManagerEJB</ejb-name>
			<jndi-name>StatefulLoanManagerHomeRemote</jndi-name>
			<ejb-ref>
				<ejb-ref-name>ejb/CustomerHomeRemote</ejb-ref-name>
				<jndi-name>CustomerHomeRemote</jndi-name>
			</ejb-ref>
		</session>
	</enterprise-beans>
</jboss>

Compare it with the contents of the Geronimo deployment plan shown in the following example.

openejb-jar.xml
<openejb-jar
    xmlns="http://www.openejb.org/xml/ns/openejb-jar"
    xmlns:naming="http://geronimo.apache.org/xml/ns/naming"
    xmlns:security="http://geronimo.apache.org/xml/ns/security"
    xmlns:sys="http://geronimo.apache.org/xml/ns/deployment"
    configId="SessionBeanDemo"
    parentId="org/apache/geronimo/Server">
 <enterprise-beans>
 	
    <entity>
        <ejb-name>CustomerEJB</ejb-name>
        <jndi-name>CustomerHomeRemote</jndi-name>
        <local-jndi-name></local-jndi-name>
        <resource-ref>
            <ref-name>jdbc/ibm-demo</ref-name>
            <resource-link>SystemDatasource</resource-link>
        </resource-ref>
    </entity>
    
    <session>
        <ejb-name>LoanManagerEJB</ejb-name>
        <jndi-name>LoanManagerHomeRemote</jndi-name>
        <ejb-ref>
            <ref-name>ejb/CustomerHomeRemote</ref-name>
            <ejb-link>CustomerEJB</ejb-link>
        </ejb-ref>        
   </session>
 	
 	<session>
        <ejb-name>StatefulLoanManagerEJB</ejb-name>
        <jndi-name>StatefulLoanManagerHomeRemote</jndi-name>
        <ejb-ref>
            <ref-name>ejb/CustomerHomeRemote</ref-name>
            <ejb-link>CustomerEJB</ejb-link>
        </ejb-ref>        
   </session>
   
 </enterprise-beans>
    
</openejb-jar>

As with all Geronimo deployment plans, this configuration requires a parent configuration. In this case, it is org/apache/geronimo/Server, then follows the definition of the EJBs.

The entity element defines an entity bean. The resource-ref element defines the data source that this EJB will be using. Then the session beans are defined in the session element as well as their jndi references in the ejb-ref elements. Here is a detailed listing of the elements:

  • ejb-name - identifies the EJB that these settings apply to; this should match the ejb-name for the EJB in ejb-jar.xml
  • jndi-name - the Home interface for the EJB is registered in JNDI at the address specified here; this global JNDI name is used by application clients to connect to this EJB (while it can be used by other server-side components, EJB references are the recommended approach); it is only meaningful if the EJB has a (remote) Home interface.
  • local-jndi-name - the LocalHome interface for the EJB is registered in JNDI at the address specified here; this global JNDI name can be used by other server-side components that want to use this EJB, though the recommended approach is to define and use a local EJB reference instead; it is only meaningful if the EJB has a LocalHome interface.
  • tssGroup - this is a set of elements that contains CORBA security settings, for EJBs exposed as CORBA objects; it is not necessary if the EJB will not be accessed via CORBA.
  • openejb-jndiEnvironmentRefsGroup - a set of elements that handle resolving references declared by the current Session bean (including EJB references, Resource references, and Web Service references).

Back to Top

Set the JNDI properties

Edit the jndi.properties file located in in the <session_home>/jndi directory as shown in the following example:

jndi.properties
####################################################################
### JBoss Settings
####################################################################
#java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
#java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
#java.naming.provider.url=localhost


####################################################################
### Geronimo Settings
####################################################################
java.naming.factory.initial=org.openejb.client.RemoteInitialContextFactory
java.naming.provider.url=localhost:4201
java.naming.security.principal=username
java.naming.security.credentials=passwd

Back to Top

Modify the WAR files

Open the web.xml file located in the <session_home>/src/webapp/WEB-INF directory and comment out the following taglib element:

Excerpt from the web.xml file
...
<!--	 <taglib>
	 	<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
		<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
	</taglib> -->
...

Open the two jsp files, stateless.jsp and stateful.jsp located in the <session_home>/src/webapp directory and make sure you replace the following line:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> - this is used for JBoss.

with this other:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> - this is used for Apache Geronimo.

Back to Top

Build the sample application

Build the migrated Session Bean sample application by running the following command from the <session_home> directory:

maven ejb war

This command will create the session-ejb-SNAPSHOT.jar and session-ejb.war files in the <session_home>/target directory.

Back to Top

Deploy the migrated application

In order to deploy the application make sure the Geronimo server is up and running. From a command line change directory to <session_home> and type the following command:

maven geronimo:deploy

Once the application is deployed and started you can verify its status by typing the following command from the <geronimo_home>/bin directory:

java -jar deployer.jar --user system --password manager list-modules

Look for the SessionBeanDemo and SessionWebApp entries.

To test the migrated Session Bean application run the following commands from the <session_home> directory:

maven run-g:stateless - for the stateless client
maven run-g:stateful - for the stateful client

The following example shows the results of the stateless session client.

E:\session>maven run-g:stateless
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

build:start:

run-g:stateless:
    [java] Customers with denied loan applications:
BUILD SUCCESSFUL
Total time: 3 seconds
Finished at: Wed Nov 02 13:08:03 EST 2005

The following example shows the results of the stateful session client.

E:\session>maven run-g:stateful
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

build:start:

run-g:stateful:
    [java] Submitting new loan application...
    [java] -> submit count is now = 1
    [java] Submitting new loan application...
    [java] -> submit count is now = 2
    [java] Submitting new loan application...
    [java] -> submit count is now = 3
    [java] Submitting new loan application...
    [java] -> submit count is now = 4
    [java] Submitting new loan application...
    [java] -> submit count is now = 5
    [java] Submitting new loan application...
    [java] -> submit count is now = 6
    [java] Submitting new loan application...
    [java] -> submit count is now = 7
    [java] Submitting new loan application...
    [java] -> submit count is now = 8
    [java] Submitting new loan application...
    [java] -> submit count is now = 9
    [java] 9 loan applications were submitted.
BUILD SUCCESSFUL
Total time: 3 seconds
Finished at: Wed Nov 02 13:10:36 EST 2005

If you run the stateless client again you will see now some customers with a denied loan application.

E:\session>maven run-g:stateless
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2

build:start:

run-g:stateless:
    [java] Customers with denied loan applications:
    [java] - Customer1
    [java] - Customer6
BUILD SUCCESSFUL
Total time: 3 seconds
Finished at: Wed Nov 02 13:11:39 EST 2005

As with JBoss, you can also run the Web Application client by pointing a Web browser to the following URL:

http://localhost:8080/session

As with JBoss, the following figures illustrate the home page for the Web Application as well as the testing for the stateless and stateful beans depending on your selection on the Web Application home page.

Back to Top

Summary

This article has shown how to migrate a sample application that uses Session Beans, from JBoss v4.0.2 to Apache Geronimo. This article provided step-by-step instructions to build the application, deploy and run it, and then migrate it to the Geronimo environment.

The following list summarizes the major differences found during this sample application migration.

  • In order to deploy an EJB jar file in JBoss you need to just copy the configuration file to the deploy directory but in Geronimo you need to use the deployer tool.
  • Geronimo needs three additional jars to run this application.
  • The contents of the deployment plans for EJB jar files in JBoss and in Geronimo are almost similar except for the name of the elements and the need for the Geronimo deployment plan to have a configId and a parentId.

Back to Top

  • No labels