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

Compare with Current View Page History

« Previous Version 7 Next »

Application Overview

This sample applications includes an ejb module with a stateless session bean, jpa entity beans, a web client, and a very simple command line non-javaee client. There are no security features.

There are three jpa entities Account, Customer and ExchangeRate. They are not set up to demonstrate any entity relationships.

todo: rewrite to use jpa entity relationships

The jpa entities rely on the datsources defined in the SamplesDatasource plugin.

Application contents

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

  • org.apache.geronimo.samples.bank.ejb
    • BankManagerFacadeBean - Stateless Session Bean, acting as a service class for different application clients.
    • Account - Entity Bean, represent account entity related data in the DB.
    • 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
   +-BankWeb.war
	+-jsp
            +- customer_info.jsp
	    +- customer_main.jsp
	    +- error.jsp
	    +- exchange_rates.jsp
	    +- index.jsp
	+-WEB-INF
	    +- web.xml
	    +- classes
   +-META-INF
        +- application.xml
   +- BankDB.sql

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

There is no need for an openejb plan.

persistence.xml defines a persistence unit which is used by an EntityManagerFactory in order to talk to SampleDatabase through the SampleDatasource configuration. The name that is given to this <persistent-unit> will be used when referencing it via an annotation in the EJB. The <jta-data-source> and <non-jta-data-source> MUST NOT point to the same thing.

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
	     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="BankPU">
        <description>Entity Beans for Bank</description>
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>SampleTxDatasource</jta-data-source>
        <non-jta-data-source>SampleNoTxDatasource</non-jta-data-source>
        <class>org.apache.geronimo.samples.bank.ejb.Account</class>
        <class>org.apache.geronimo.samples.bank.ejb.Customer</class>
        <class>org.apache.geronimo.samples.bank.ejb.ExchangeRate</class>
    </persistence-unit>
</persistence>

web.xml lists the servlets and url-patterns.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
    version="2.5">

    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <display-name>CustomerServiceServlet</display-name>
        <servlet-name>CustomerServiceServlet</servlet-name>
        <servlet-class>org.apache.geronimo.samples.bank.web.CustomerServiceServlet</servlet-class>
    </servlet>

    <servlet>
        <display-name>CommonServiceServlet</display-name>
        <servlet-name>CommonServiceServlet</servlet-name>
        <servlet-class>org.apache.geronimo.samples.bank.web.CommonServiceServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CustomerServiceServlet</servlet-name>
        <url-pattern>/customer_info</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>CommonServiceServlet</servlet-name>
        <url-pattern>/exchange_rates</url-pattern>
    </servlet-mapping>
</web-app>

No web plan is needed.

The geronimo plan plan.xml contains the db initialization gbean that runs a script to create tables and populate them. If you leave this out openjpa will create or update the structure of the tables on first access. The unprocessed version of this plan is at bank-jetty/src/main/plan/plan.xml. The processed version shown here with plugin name and all dependencies filled in can be found at bank-jetty/target/resources/META-INF/plan.xml after building the project.

geronimo-application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.2">
  <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
    <dep:moduleId>
      <dep:groupId>org.apache.geronimo.samples</dep:groupId>
      <dep:artifactId>bank-jetty</dep:artifactId>
      <dep:version>2.2-SNAPSHOT</dep:version>
      <dep:type>car</dep:type>
    </dep:moduleId>
    <dep:dependencies>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>jasper</dep:artifactId>
        <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>jetty6</dep:artifactId>
        <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>openjpa</dep:artifactId>
        <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>openejb</dep:artifactId>
        <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.samples</dep:groupId>
        <dep:artifactId>sample-datasource</dep:artifactId>
        <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
    </dep:dependencies>
    <dep:hidden-classes/>
    <dep:non-overridable-classes/>
  </dep:environment>
  <gbean name="DBInitialization" class="org.apache.geronimo.connector.DatabaseInitializationGBean">
    <attribute name="testSQL">select * from customer</attribute>
    <attribute name="path">BankDB.sql</attribute>
    <reference name="DataSource">
      <name>SampleTxDatasource</name>
    </reference>
  </gbean>
</application>

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

Configuring, Building and Deploying the Sample Application

Currently this sample application is available from our subversion repository, use the following svn command to retrieve the content:

svn co http://svn.apache.org/repos/asf/geronimo/samples/branches/2.1/samples/bank/ bank_home

A directory named bank_home will be created when you check out from the source, you can choose a directory name and location convenient to you. We will generically refer to this directory as <bank_home>.

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 Administration Console and follow the given steps to create the BankDB database.

  1. Select DB Manager link from the Console Navigation in the left.
  2. Name the database as BankDB and click Create button.
  3. Select BankDB to the Use DB field.
  4. Open BankDB.sql in the <bank_home>/bank-ear/src/main/resources directory from a text editor.
  5. Paste the content BankDB.sql to the SQL Commands text area and press Run SQL button.
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);

Building

Use a command prompt to navigate into the <bank_home> directory and run the mvn install command to build. It will create the bank-ear-2.1-SNAPSHOT.ear under the <bank_home> directory. 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.1-SNAPSHOT.ear from bank folder in to the Archive input box.
  3. Press Install button to deploy application in the server.

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 Java EE 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.
  • No labels