HomeDocumentation > Sample applications > EJB sample application


Enterprise Java Beans has been one of the corner stones of the J2EE specification. As a J2EE 1.5 certified application server, Apache Geronimo supports EJB's extensively with the help of OpenEJB EJB Container. 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. In this article, you will see how an initial database application is extended and used for both local and remotely referred application clients for an Enterprise Java Beans back end. The application uses the built-in Apache Derby as its database. Use this article to learn how to simplify your enterprise application development process.

Banking application has two types of application clients namely "Banking Remote Application" and "Banking Web Application". Each of these clients demonstrate how to refer Enterprise Java Beans in remote and local interfaces respectively. Both these clients are referring a common business layer which has been implemented with the help of Session and Entity Beans. Stateless Session Beans are acting as the business service interface between business entities and application clients. All the business entities of the application layer are implemented with Entity Beans.

After reading this article you should be able get the best out of EJB features of Geronimo, such as defining Enterprise Java Beans, managing relations between them and refer EJB's via differents kind of clients.

This article is organized in to following sections.

Overview of EJB Features

EJB implementation may vary from one vendor to another.The following are the main list of features Apache Geronimo supports as a J2EE container.

  • Stateful and stateless Session Beans
  • Entity Beans
  • Message driven beans (MDBs)
  • Interoperability using RMI-IIOP or JAXRPC
  • Ability to expose stateless session beans and MDBs as Web Services
  • Support for sending and receiving messages via Web Services
  • Easy provisioning and hot deployment of EJB and JMX-based Web Services
  • Access to EJBs from external CORBA objects

Application Overview

As mentioned above the Banking application supports two types of business application clients.The overview of each client is given below.

  1. Banking Remote Application
    A small swing application client which has more super user capabilities in the banking environment. Only a limited number of banking staff have access to this application. It allows viewing and updating of balance of bank accounts.
  2. Banking Web Application
    This is a Web application open to the Customers. It enables them to view their bank account information. Additionally, users of this application can view the exchange rates given by the bank. For the sake of simplicity, security features of each application will be ignored even though it can be achieved very easily in the context of Geronimo.

Both of these clients use a common business service layer. Behind that business service layer, there are three common business entities that appear in the banking application domain Account, Customer and ExchangeRate. Each Customer can have more than one Account while an Account can only be owned by one Customer. ExchangeRate represents a rate value given by the bank relative to the USD for a particular currency.

Application contents

The Banking application consists of the following list of packages and classes.

  • org.apache.geronimo.samples.bank.ejb
    • Account - Entity Bean, represent account entity related data in the DB.
    • BankManagerFacadeBean - Stateless Session Bean, acting as a service class for different application clients.
    • Customer - Entity Bean, represents customer entity related data.
    • ExchangeRate - Entity Bean, represents exchange rate relative to a USD.
  • org.apache.geronimo.samples.bank.web
    • CustomerServiceServlet - Dispatches web requests of Customer Account Balance Viewer to the service layer.
    • CommonServiceServlet - Dispatches web requests of Exchange Rate viewing scenario.

Finally, the banking application will be deployed as an EAR to the application server. The overview of the structural content of the EAR file is given in the following example.

|-Bank.ear
   |-BankEJB.jar
	|-META-INF
	    |- persistence.xml
	    |- openejb-jar.xml
   |-BankWeb.war
	|-jsp
            |- customer_info.jsp
	    |- customer_main.jsp
	    |- error.jsp
	    |- exchange_rates.jsp
	    |- index.jsp
	|-WEB-INF
	    |- web.xml
	    |- geronimo-web.xml
	    |- classes
   |-META-INF
        |- application.xml
        |- geronimo-application.xml

First, we will look at how the business service layer of the application has been implemented with the help of EJBs.

Corresponding openejb-jar.xml defines Geronimo specific features of EJBs. Since we will deploy the database pool along with the application, there is no need to provide a dependency to an existing database pool.

openejb-jar.xml

persistence.xml defines a persistence unit which is used by an EntityManagerFactory in order to talk to BankDB through the BankPool configuration. The name that is given to this <persistent-unit> will be used when trying to reference it via an annotation in the EJB. By setting the SynchronizeMappings property it will not overwrite what is already in the database. So this is important to have when you do not want your data to be deleted. The jta-data-source and non-jta-data-source should point to the same thing.

persistence.xml

web.xml does not do anything special here. It just enumerates the servlets present in the web-app and maps the url-pattern for each of them.

web.xml

geronimo-web.xml is the Geronimo specific deployment plan. As usual, it specifies the module's information and context-root. So in order to visit the web-app the root url will be http://localhost:8080/Bank.

geronimo-web.xml

BankPool.xml provides the connection information for a database on Geronimo. In this case we are using Geronimo's built-in Derby database. The dependency for it should actually be org.apache.geronimo.configs/system-database//car. It has caused me problems when trying to use anything different. The following database pool plan is generated from the console. The only change I made to it was the dependency to make it point to the system-database where the derby engine is running. This db pool will be deployed with the EAR application.

BankPool.xml

geronimo-application.xml tells the application that there is a database pool that needs to be deployed as well. The db pool is defined in BankPool.xml and the driver that is needs in order to be deployed is the tranql-connector-ra-1.3.rar file--these two files will reside on the top level layer of the resultant EAR file.

geronimo-application.xml

Sample Database

The sample database that is being used to demonstrate this application is inbuilt Derby database. The name of the sample database is BankDB and it consists of three tables, CUSTOMER ,ACCOUNT and EXCHANGE_RATE. The fields for each of these tables are described below.

Table Name

Fields

CUSTOMER

customerId (PRIMARY KEY)
name

ACCOUNT

accountNumber (PRIMARY KEY)
accountType
balance
customerId

EXCHANGERATE

rateId (PRIMARY KEY)
currency
rate

The CUSTOMER table stores the data related to the customers. It stores only the identification number and and the name. ACCOUNT table has a unique account number for identification. Account type and balance are the other information stored. Account.customerId is a foriegn key to the Customer table which is the owner of the Account. EXCHANGERATE table has a primary key of rateId for an identification. Each record of EXCHANGERATE has currency name and rate paid by the bank.

Tools used

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

Apache Derby

Apache Derby, an Apache DB subproject, is a relational database implemented in Java. Its footprint is so small you can easily embed it in any Java-based solution. In addition to its embedded framework, Derby supports a more familiar client/server framework with the Derby Network Server.
http://db.apache.org/derby/index.html

Apache Maven 2

Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather than the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts. The declarative, lifecycle-based approach used by Maven 1 is, for many, a radical departure from more traditional build techniques, and Maven 2 goes even further in this regard. Maven 2 can be download from the following URL:
http://maven.apache.org

Back to Top

Configuring, Building and Deploying the Sample Application

Download the bank application from the following link:
Bank

After decompressing the given file, the bank directory will be created.

Source Code

You can checkout the source code of this sample from SVN:

svn checkout http://svn.apache.org/repos/asf/geronimo/samples/trunk/samples/bank

Configuring

Configuration of the application consists of creating the database and defining the connection pool to access it.

Creating and Populating Database

After starting Apache Geronimo log into the console and follow the given steps to create the BankDB.

BankDB.sql
CREATE TABLE Customer(
	customerId VARCHAR(255) PRIMARY KEY,
	name VARCHAR(255)
);

CREATE TABLE Account(
	accountNumber VARCHAR(255) PRIMARY KEY,
	accountType VARCHAR(255),
	balance DOUBLE,
	customerId VARCHAR(255),
	FOREIGN KEY (customerId) REFERENCES customer
);

CREATE TABLE ExchangeRate(
	rateId VARCHAR(255) PRIMARY KEY,
	currency VARCHAR(255),
	rate DOUBLE
);

INSERT INTO Customer(customerId, name) VALUES('12345','John Doe');
INSERT INTO Account(accountNumber, accountType, balance, customerId) VALUES('1234567890','Savings',1005.35,'12345');
INSERT INTO Account(accountNumber, accountType, balance, customerId) VALUES('2345678901','Current',999.95,'12345');

INSERT INTO ExchangeRate(rateId,currency,rate) VALUES('001','EURO',0.812);
INSERT INTO ExchangeRate(rateId,currency,rate) VALUES('002','YEN',111.15);
INSERT INTO ExchangeRate(rateId,currency,rate) VALUES('003','SLR',99.18);
  1. Select DB Manager link from the Console Navigation in the left.
  2. Give the database name as BankDB and click Create button.
  3. Select BankDB to the Use DB field.
  4. Open BankDB.sql in the bank directory from a text editor.
  5. Paste the content BankDB.sql to the SQL Commands text area and press Run SQL button.

Building

Use a command prompt to navigate into the bank directory and just give mvn install site command to build. It will create the bank-ear-2.0-SNAPSHOT.ear under the bank folder. Now, you are ready to deploy bank application in the Geronimo Application server.

Deploying the Application

Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.

  1. Scroll down to Deploy New from the Console Navigation panel.
  2. Load bank-ear-2.0-SNAPSHOT.ear from bank folder in to the Archive input box.
  3. Press Install button to deploy application in the server.

Back to Top

Banking Web Application

To test the sample web application open a browser and type http://localhost:8080/Bank. It will forward you to the index page of banking application which has direct links to the view customer and exchange rate information. To view the list of account information of each customer, provide a relavant customer id in the DB. Exchange rate page will display list of all currencies in the exchange rate table. (Note that 'Bank' is case-sensitive)

Summary

This article has shown you how to use the EJB features of the Apache Geronimo. It has provided step-by-step instructions to build an application, deploy and run it to elaborate those features.

Following are some of the highlights of the article.

  • Apache Geronimo is a J2EE 1.5 Certified application server and it provides all the necessary features to Enterprise applications.
  • Create and configure Session and Entity Beans.
  • Access defined enterprise level services by different sorts of clients.