Child pages
  • bank - EJB sample application

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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.

Code Block
xml
xml
borderStylesolid
titlepersistence.xmlxml
<?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.

Code Block
xml
xml
borderStylesolid
titleweb.xmlxml
<?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>

...

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.

Code Block
xml
xml
borderStylesolid
titleplan.xmlxml
<?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.1.2</dep:version>
      <dep:type>car</dep:type>
    </dep:moduleId>
    <dep:dependencies>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.samples</dep:groupId>
        <dep:artifactId>sample-datasource</dep:artifactId>
        <dep:version>2.1.2</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.1.2</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>jasper</dep:artifactId>
        <dep:version>2.1.2</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.1.2</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.1.2</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>

...

The bank command line client does a JNDI lookup to the remote interface. Later, the list of ExchangeRate is obtained and printed out on the screen.

Code Block
xml
xml
borderStylesolid
titleBankClient.javaxml
package org.apache.geronimo.samples.bank.client;

import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.geronimo.samples.bank.ejb.BankManagerFacadeRemote;
import org.apache.geronimo.samples.bank.ejb.ExchangeRate;

public class BankClient {
    public static void main(String[] args) {


        Properties p = new Properties();
        p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        p.setProperty(Context.PROVIDER_URL, "ejbd://localhost:4201");
        try {
            Context ic = new InitialContext(p);
            BankManagerFacadeRemote bmr = (BankManagerFacadeRemote) ic.lookup("BankManagerFacadeBeanRemote");
            List<ExchangeRate> rates = bmr.getExchangeRates();
            System.out.println("Exchange Rates");
            for (int i = 0; i < (int) rates.size(); i++) {
                ExchangeRate rate = rates.get(i);
                System.out.println("Currency: " + rate.getCurrency());
                System.out.println("Rate: " + rate.getRate());
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

...