Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Enterprise Java Beans has been one of the corner stones of the J2EE specification. As a J2EE 1.4 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 inbuilt 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 of these clients refer to 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 the CMP & BMP Entity Beans. Relations between CMP entities are managed as Container Managed Relations.

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 referring refer EJB's via different kinds differents kind of clients.

This article is organized into in to following sections : - .

...

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

  • Stateful and stateless Session Beans
  • BMP (Bean Managed Persistence) Entity Beans
  • CMP (Container Managed Persistence) 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 through from external CORBA objects

Application Overview
Anchor
application
application

...

  1. Banking Remote Application
    A small swing application client which has more super user capabilities in the banking enviroment. 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 quite 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 Customer can have more than one Account while an account Account can only be owned by one customerCustomer. ExchangeRate represents a rate value given by the bank relative to the USD for a particular currency.

The following figure below gives the overall architecture of the banking application.

...

  • org.apache.geronimo.samples.bank.client
    • MainUI - User interface for the Account Balance Modifier.
  • org.apache.geronimo.samples.bank.dto
    • AccountDTO - Use to transfer Account entity related data between different application layers.
    • ExchangeRateDTO - Transfer ExchangeRate entity related data between different application layers.
  • org.apache.geronimo.samples.bank.ejb
    • AccountBean - CMP Entity Bean, represent account entity related data in the DB.
    • BankManagerFacadeBean - Stateless Session Bean, acting as a service class for different application clients.
    • CustomerBean - CMP Entity Bean, represents customer entity related data.
    • ExchangeRateBean - BMP Entity Bean, represents exchange rate relative to a USD.
  • org.apache.geronimo.samples.bank.util
    • PropertyLoader - Loads configuration properties to the Account Balance Modifier Client.
  • 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. An overview Overview of the structural content of the EAR file is given in the following example.

...

Code Block
xml
xml
borderStylesolid
titleopenejb-jar.xml
&lt;<?xml version=&quot;"1.0&quot;" encoding=&quot;"UTF-8&quot;?&gt;
&lt;openejb"?>
<openejb-jar xmlns=&quot;"http://www.openejb.org/xml/ns/openejb-jar-2.1&quot;&gt;">
	&lt;dep<dep:environment xmlns:dep=&quot;"http://geronimo.apache.org/xml/ns/deployment-1.1&quot;&gt;">
		&lt;dep:moduleId&gt;<dep:moduleId>
			&lt;dep:groupId&gt;default&lt;<dep:groupId>default</dep:groupId&gt;groupId>
			&lt;dep:artifactId&gt;BankEJB&lt;<dep:artifactId>BankEJB</dep:artifactId&gt;artifactId>
			&lt;dep:version&gt;1.0&lt;/dep:version&gt;<dep:version>1.0</dep:version>
			&lt;dep:type&gt;car&lt;<dep:type>car</dep:type&gt;type>
		&lt;</dep:moduleId&gt;moduleId>
		&lt;dep<dep:dependencies/&gt;>
		&lt;dep<dep:hidden-classes/&gt;>
		&lt;dep<dep:non-overridable-classes/&gt;>
	&lt;</dep:environment&gt;environment>
	&lt;cmp-<cmp-connection-factory&gt;factory>
		&lt;resource-link&gt;BankPool&lt;<resource-link>BankPool</resource-link&gt;link>
	&lt;</cmp-connection-factory&gt;factory>
	&lt;enterprise-beans&gt;<enterprise-beans>
		&lt;session&gt;<session>
			&lt;ejb-name&gt;BankManagerFacadeBean&lt;<ejb-name>BankManagerFacadeBean</ejb-name&gt;name>
			&lt;jndi-name&gt;org.<jndi-name>org.apache.geronimo.samples.bank.ejb.BankManagerFacadeBean&lt;BankManagerFacadeBean</jndi-name&gt;name>
			&lt;ejb-ref&gt;<ejb-ref>
				&lt;ref-name&gt;ejb/Customer&lt;<ref-name>ejb/Customer</ref-name&gt;name>
				&lt;ejb-link&gt;Customer&lt;<ejb-link>Customer</ejb-link&gt;link>
			&lt;</ejb-ref&gt;ref>
			&lt;ejb-ref&gt;<ejb-ref>
				&lt;ref-name&gt;ejb/Account&lt;<ref-name>ejb/Account</ref-name&gt;name>
				&lt;ejb-link&gt;Account&lt;<ejb-link>Account</ejb-link&gt;link>
			&lt;</ejb-ref&gt;ref>
			&lt;ejb-ref&gt;<ejb-ref>
				&lt;ref-name&gt;ejb/ExchangeRate&lt;<ref-name>ejb/ExchangeRate</ref-name&gt;name>
				&lt;ejb-link&gt;ExchangeRate&lt;<ejb-link>ExchangeRate</ejb-link&gt;link>
			&lt;</ejb-ref&gt;ref>
		&lt;/session&gt;</session>
	  	
		&lt;entity&gt;<entity>
			&lt;ejb-name&gt;Account&lt;<ejb-name>Account</ejb-name&gt;name>
			&lt;local<local-jndi-name&gt;AccountLocalEntity&lt;name>AccountLocalEntity</local-jndi-name&gt;name>
			&lt;table-name&gt;Account&lt;<table-name>Account</table-name&gt;name>
			
			&lt;cmp<cmp-field-mapping&gt;mapping>
				&lt;cmp<cmp-field-name&gt;accountNumber&lt;name>accountNumber</cmp-field-name&gt;name>
				&lt;table-column&gt;ACC_NO&lt;<table-column>ACC_NO</table-column&gt;column>
			&lt;</cmp-field-mapping&gt;mapping>
			
			&lt;cmp<cmp-field-mapping&gt;mapping>
				&lt;cmp<cmp-field-name&gt;accountType&lt;name>accountType</cmp-field-name&gt;name>
				&lt;table-column&gt;ACC_TYPE&lt;<table-column>ACC_TYPE</table-column&gt;column>
			&lt;</cmp-field-mapping&gt;mapping>
			
			&lt;cmp<cmp-field-mapping&gt;mapping>
				&lt;cmp<cmp-field-name&gt;customer&lt;name>customer</cmp-field-name&gt;name>
				&lt;table-column&gt;CUSTID_FK&lt;<table-column>CUSTID_FK</table-column&gt;column>
			&lt;</cmp-field-mapping&gt;mapping>
			
			&lt;cmp<cmp-field-mapping&gt;mapping>
				&lt;cmp<cmp-field-name&gt;balance&lt;name>balance</cmp-field-name&gt;name>
				&lt;table-column&gt;BALANCE&lt;<table-column>BALANCE</table-column&gt;column>
			&lt;</cmp-field-mapping&gt;mapping>
			
			&lt;resource-ref&gt;<resource-ref>
				&lt;ref-name&gt;jdbc/BankDataSource&lt;<ref-name>jdbc/BankDataSource</ref-name&gt;name>
				&lt;resource-link&gt;BankPool&lt;<resource-link>BankPool</resource-link&gt;link>
			&lt;</resource-ref&gt;ref>
		&lt;/entity&gt;</entity>
	  	
		&lt;entity&gt;<entity>
			&lt;ejb-name&gt;Customer&lt;<ejb-name>Customer</ejb-name&gt;name>
			&lt;local<local-jndi-name&gt;CustomerLocalEntity&lt;name>CustomerLocalEntity</local-jndi-name&gt;name>
			&lt;table-name&gt;Customer&lt;<table-name>Customer</table-name&gt;name>
			
			&lt;cmp<cmp-field-mapping&gt;mapping>
				&lt;cmp<cmp-field-name&gt;customerId&lt;name>customerId</cmp-field-name&gt;
name>
				&lt;table-column&gt;CUST_ID&lt;<table-column>CUST_ID</table-column&gt;column>
			&lt;</cmp-field-mapping&gt;mapping>
			
			&lt;cmp<cmp-field-mapping&gt;mapping>
				&lt;cmp<cmp-field-name&gt;customerName&lt;name>customerName</cmp-field-name&gt;name>
				&lt;table-column&gt;CUST_NAME&lt;<table-column>CUST_NAME</table-column&gt;column>
			&lt;</cmp-field-mapping&gt;mapping>
			
			&lt;resource-ref&gt;<resource-ref>
				&lt;ref-name&gt;jdbc/BankDataSource&lt;<ref-name>jdbc/BankDataSource</ref-name&gt;name>
				&lt;resource-link&gt;BankPool&lt;<resource-link>BankPool</resource-link&gt;link>
			&lt;</resource-ref&gt;ref>
		&lt;/entity&gt;</entity>
	  	
			&lt;entity&gt;
			&lt;ejb-name&gt;ExchangeRate&lt;/ejb-name&gt;<entity>
			<ejb-name>ExchangeRate</ejb-name>
			&lt;local<local-jndi-name&gt;ExchangeRate&lt;name>ExchangeRate</local-jndi-name&gt;name>
			&lt;resource-ref&gt;<resource-ref>
				&lt;ref-name&gt;jdbc/BankDataSource&lt;<ref-name>jdbc/BankDataSource</ref-name&gt;name>
				&lt;resource-link&gt;BankPool&lt;/resource-link&gt;<resource-link>BankPool</resource-link>
			&lt;</resource-ref&gt;ref>
		&lt;/entity&gt;
	&lt;</entity>
	</enterprise-beans&gt;beans>

	&lt;relationships&gt;<relationships>
		&lt;ejb-relation&gt;<ejb-relation>
			&lt;ejb<ejb-relation-name&gt;Customer-Account&lt;name>Customer-Account</ejb-relation-name&gt;name>
			&lt;ejb<ejb-relationship-role&gt;role>
				&lt;ejb<ejb-relationship-role-name&gt;Accountname>Account-to-Customer&lt;Customer</ejb-relationship-role-name&gt;name>
				&lt;relationship<relationship-role-source&gt;source>
					&lt;ejb-name&gt;Account&lt;<ejb-name>Account</ejb-name&gt;name>
				&lt;</relationship-role-source&gt;source>
				&lt;cmr-field&gt;<cmr-field>
					&lt;cmr<cmr-field-name&gt;customer&lt;name>customer</cmr-field-name&gt;name>
				&lt;</cmr-field&gt;field>
				&lt;foreign<foreign-key-column-on-source/&gt;>
				&lt;role-mapping&gt;<role-mapping>
					&lt;cmr<cmr-field-mapping&gt;mapping>
						&lt;key-column&gt;CUST_ID&lt;<key-column>CUST_ID</key-column&gt;column>
						&lt;<foreign-key-column>CUSTID_FK</foreign-key-column&gt;CUSTID_FK&lt;/foreign-key-column&gt;column>
					&lt;</cmr-field-mapping&gt;mapping>
				&lt;</role-mapping&gt;mapping>
			&lt;</ejb-relationship-role&gt;role>
		&lt;</ejb-relation&gt;relation>

	&lt;/relationships&gt;

&lt;</relationships>

</openejb-jar&gt;jar>

BankPool.xml is a typical database pool configuration file, which will be connected to the BankDB defined in the inbuilt Derby database. Entity beans of the application refer the defined database via this configuration file.

Code Block
xml
xml
borderStylesolid
titleBankPool.xml
&lt;<?xml version=&quot;"1.0&quot;" encoding=&quot;"UTF-8&quot;?&gt;
&lt;connector"?>
<connector xmlns=&quot;"http://geronimo.apache.org/xml/ns/j2ee/connector-1.1&quot;&gt;">
	&lt;dep<dep:environment xmlns:dep=&quot;"http://geronimo.apache.org/xml/ns/deployment-1.1&quot;&gt;">
		&lt;dep:moduleId&gt;<dep:moduleId>
			&lt;dep:groupId&gt;console.dbpool&lt;<dep:groupId>console.dbpool</dep:groupId&gt;groupId>
			&lt;dep:artifactId&gt;BankPool&lt;<dep:artifactId>BankPool</dep:artifactId&gt;artifactId>
			&lt;dep:version&gt;1.0&lt;<dep:version>1.0</dep:version&gt;version>
			&lt;dep:type&gt;rar&lt;<dep:type>rar</dep:type&gt;type>
		&lt;</dep:moduleId&gt;moduleId>
		&lt;dep:dependencies&gt;<dep:dependencies>
			&lt;dep:dependency&gt;<dep:dependency>
				&lt;dep:groupId&gt;org<dep:groupId>org.apache.derby&lt;derby</dep:groupId&gt;groupId>
				&lt;dep:artifactId&gt;derby&lt;<dep:artifactId>derby</dep:artifactId&gt;artifactId>
				&lt;dep:version&gt;10<dep:version>10.1.1.0&lt;0</dep:version&gt;version>
				&lt;dep:type&gt;jar&lt;<dep:type>jar</dep:type&gt;
type>
			&lt;</dep:dependency&gt;dependency>
		&lt;</dep:dependencies&gt;dependencies>
	&lt;</dep:environment&gt;environment>
	&lt;resourceadapter&gt;<resourceadapter>
		&lt;outbound-resourceadapter&gt;<outbound-resourceadapter>
			&lt;connection-definition&gt;<connection-definition>
				&lt;connectionfactory-interface&gt;javax<connectionfactory-interface>javax.sql.DataSource&lt;DataSource</connectionfactory-interface&gt;interface>
				&lt;connectiondefinition-instance&gt;<connectiondefinition-instance>
					&lt;name&gt;BankPool&lt;/name&gt;<name>BankPool</name>
					&lt;config<config-property-setting name=&quot;Driver&quot;&gt;org"Driver">org.apache.derby.jdbc.EmbeddedDriver&lt;EmbeddedDriver</config-property-setting&gt;setting>
					&lt;config<config-property-setting name=&quot;UserName&quot;&gt;app&lt;"UserName">app</config-property-setting&gt;setting>
					&lt;config-<config-property-setting name=&quot;ConnectionURL&quot;&gt;jdbc"ConnectionURL">jdbc:derby:BankDB&lt;BankDB</config-property-setting&gt;setting>
					&lt;connectionmanager&gt;<connectionmanager>
						&lt;local<local-transaction/&gt;>
						&lt;single-pool&gt;<single-pool>
							&lt;max-size&gt;10&lt;<max-size>10</max-size&gt;size>
							&lt;min-size&gt;0&lt;/min-size&gt;
<min-size>0</min-size>
							&lt;match<match-one/&gt;>
						&lt;</single-pool&gt;pool>
					&lt;/connectionmanager&gt;</connectionmanager>
				&lt;</connectiondefinition-instance&gt;instance>
			&lt;</connection-definition&gt;definition>
		&lt;</outbound-resourceadapter&gt;resourceadapter>
	&lt;/resourceadapter&gt;
&lt;/connector&gt;</resourceadapter>
</connector>

geronimo-application.xml and application.xml define the main components of the EAR. Both EJB component and Web archive information are given in these files. Additionally, these two XML files define application scoped database connection pool with tranql-connector-1.2.rar and BankPool.xml.

Code Block
xml
xml
borderStylesolid
titlegeronimo-application.xml
&lt;<?xml version=&quot;"1.0&quot;" encoding=&quot;"UTF-8&quot;?&gt;
&lt;application"?>
<application xmlns=&quot;"http://geronimo.apache.org/xml/ns/j2ee/application-1.1&quot;&gt;">
	&lt;dep<dep:environment xmlns:dep=&quot;"http://geronimo.apache.org/xml/ns/deployment-1.1&quot;&gt;">
		&lt;dep:moduleId&gt;<dep:moduleId>
			&lt;dep:groupId&gt;default&lt;/dep:groupId&gt;
			&lt;dep:artifactId&gt;Bank&lt;/dep:artifactId&gt;
			&lt;dep:version&gt;1.0&lt;/dep:version&gt;
			&lt;dep:type&gt;car&lt;/dep:type&gt;
		&lt;/dep:moduleId&gt;
		&lt;dep:dependencies/&gt;
		&lt;dep:hidden-classes/&gt;
		&lt;dep<dep:groupId>default</dep:groupId>
			<dep:artifactId>Bank</dep:artifactId>
			<dep:version>1.0</dep:version>
			<dep:type>car</dep:type>
		</dep:moduleId>
		<dep:dependencies/>
		<dep:hidden-classes/>
		<dep:non-overridable-classes/&gt;>
	&lt;</dep:environment&gt;environment>
	&lt;module&gt;<module>
		&lt;connector&gt;tranql<connector>tranql-connector-1.2.rar&lt;/connector&gt;rar</connector>
		&lt;alt-dd&gt;BankPool.xml&lt;<alt-dd>BankPool.xml</alt-dd&gt;
	&lt;/module&gt;
&lt;/application&gt;dd>
	</module>
</application>
Code Block
xml
xml
borderStylesolid
titleapplication.xml
&lt;<?xml version=&quot;"1.0&quot;" encoding=&quot;"UTF-8&quot;?&gt;
&lt;application"?>
<application
		xmlns=&quot;"http://java.sun.com/xml/ns/j2ee&quot;"
		xmlns:xsi=&quot;"http://www.w3.org/2001/XMLSchema-instance&quot;"
		xsi:schemaLocation=&quot;"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd&quot;"
		version=&quot;"1.4&quot;&gt;
	&lt;module&gt;">
	<module>
		&lt;ejb&gt;BankEJB.jar&lt;/ejb&gt;
	&lt;/module&gt;
	&lt;module&gt;
		&lt;web&gt;
			&lt;web-uri&gt;BankWeb.war&lt;/web-uri&gt;
			&lt;context-root&gt;/Bank&lt;/context-root&gt;
		&lt;/web&gt;
	&lt;/module&gt;
	&lt;module&gt;
		&lt;connector&gt;tranql<ejb>BankEJB.jar</ejb>
	</module>
	<module>
		<web>
			<web-uri>BankWeb.war</web-uri>
			<context-root>/Bank</context-root>
		</web>
	</module>
	<module>
		<connector>tranql-connector-1.2.rar&lt;/connector&gt;
	&lt;/module&gt;
&lt;/application&gt;rar</connector>
	</module>
</application>

Since Banking Web Application is a part of EAR, the BankManagerFacade Session Bean will be reffered as a local interface. Those additional configuration information required for the EJB reference can be found in the web.xml.

Code Block
xml
xml
borderStylesolid
titleweb.xml
&lt;<?xml version=&quot;"1.0&quot;" encoding=&quot;"UTF-8&quot;?&gt;
&lt;web"?>
<web-app xmlns=&quot;"http://java.sun.com/xml/ns/j2ee&quot;"
		xmlns:xsi=&quot;"http://www.w3.org/2001/XMLSchema-instance&quot;"
		xsi:schemaLocation=&quot;"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;"
		version=&quot;"2.4&quot;&gt;">
	
	&lt;welcome<welcome-file-list&gt;list>
		&lt;welcome-file&gt;<welcome-file>/jsp/index.jsp&lt;jsp</welcome-file&gt;file>
	&lt;</welcome-file-list&gt;list>

	&lt;servlet&gt;<servlet>
		&lt;display-name&gt;CustomerServiceServlet&lt;<display-name>CustomerServiceServlet</display-name&gt;name>
		&lt;servlet-name&gt;CustomerServiceServlet&lt;<servlet-name>CustomerServiceServlet</servlet-name&gt;name>
		&lt;servlet-class&gt;org<servlet-class>org.apache.geronimo.samples.bank.web.CustomerServiceServlet&lt;CustomerServiceServlet</servlet-class&gt;class>
	&lt;/servlet&gt;</servlet>
	
	&lt;servlet&gt;<servlet>
		&lt;display-name&gt;CommonServiceServlet&lt;<display-name>CommonServiceServlet</display-name&gt;name>
		&lt;servlet-name&gt;CommonServiceServlet&lt;<servlet-name>CommonServiceServlet</servlet-name&gt;name>
		&lt;servlet-class&gt;org<servlet-class>org.apache.geronimo.samples.bank.web.CommonServiceServlet&lt;CommonServiceServlet</servlet-class&gt;class>
	&lt;/servlet&gt;</servlet>
  	
	&lt;servlet-mapping&gt;<servlet-mapping>
		&lt;servlet-name&gt;CustomerServiceServlet&lt;<servlet-name>CustomerServiceServlet</servlet-name&gt;name>
		&lt;url-pattern&gt;<url-pattern>/customer_info&lt;info</url-pattern&gt;pattern>
	&lt;</servlet-mapping&gt;mapping>
	
	&lt;servlet-mapping&gt;<servlet-mapping>
		&lt;servlet-name&gt;CommonServiceServlet&lt;<servlet-name>CommonServiceServlet</servlet-name&gt;name>
		&lt;url-pattern&gt;<url-pattern>/exchange_rates&lt;rates</url-pattern&gt;pattern>
	&lt;</servlet-mapping&gt;mapping>
	
	
	&lt;<!-- To refer local EJB's  --&gt;>
	&lt;ejb<ejb-local-ref&gt;ref>
		&lt;ejb<ejb-ref-name&gt;ejb/BankManagerFacade&lt;name>ejb/BankManagerFacade</ejb-ref-name&gt;name>
		&lt;ejb<ejb-ref-type&gt;Session&lt;type>Session</ejb-ref-type&gt;type>
		&lt;local-home&gt;org<local-home>org.apache.geronimo.samples.bank.ejb.BankManagerFacadeHomeLocal&lt;BankManagerFacadeHomeLocal</local-home&gt;home>
		&lt;local&gt;org<local>org.apache.geronimo.samples.bank.ejb.BankManagerFacadeLocal&lt;/local&gt;BankManagerFacadeLocal</local>
		&lt;ejb-link&gt;BankManagerFacadeBean&lt;<ejb-link>BankManagerFacadeBean</ejb-link&gt;link>
	&lt;</ejb-local-ref&gt;ref>

&lt;</web-app&gt;app>

Account Balance Modifier swing application refers the same BankManagerFacade Session bean as a remotely refer EJB. It's configuration information can be found in the bank_client.properties file.

...