...
Apache Geronimo ships with embedded Derby database. The Derby libraries are present in the server repository at <geronimo_home>/repository/org/apache/derby
. By default, a Derby database by name SystemDatabase
is created and the files related to the database are stored at <geronimo_home>/var/derby/SystemDatabase
. Along with that, by default, server deploys a database connection pool over the SystemDatabase
with the configuration name org.apache.geronimo.configs/system-database/2.1/car
. The name of the database connection pool is SystemDatasource
. The configuration artifacts are stored at <geronimo_home>/repository/org/apache/geronimo/configs/system-database
. The deployment plan used for database connection pool is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | SystemDatasourceXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
<dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
<dep:moduleId>
<dep:groupId>org.apache.geronimo.configs</dep:groupId>
<dep:artifactId>system-database</dep:artifactId>
<dep:version>2.1</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
<dep:dependencies>
<dep:dependency>
<dep:groupId>org.apache.geronimo.configs</dep:groupId>
<dep:artifactId>transaction</dep:artifactId>
<dep:version>2.1</dep:version>
<dep:type>car</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>org.apache.geronimo.modules</dep:groupId>
<dep:artifactId>geronimo-derby</dep:artifactId>
<dep:version>2.1</dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>org.apache.geronimo.modules</dep:groupId>
<dep:artifactId>geronimo-timer</dep:artifactId>
<dep:version>2.1</dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>org.apache.derby</dep:groupId>
<dep:artifactId>derby</dep:artifactId>
<dep:version>10.2.2.0</dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>org.apache.derby</dep:groupId>
<dep:artifactId>derbynet</dep:artifactId>
<dep:version>10.2.2.0</dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>org.apache.derby</dep:groupId>
<dep:artifactId>derbyclient</dep:artifactId>
<dep:version>10.2.2.0</dep:version>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>org.tranql</dep:groupId>
<dep:artifactId>tranql-connector-derby-embed-xa
</dep:artifactId>
<dep:version>1.3</dep:version>
<dep:type>rar</dep:type>
</dep:dependency>
</dep:dependencies>
<dep:hidden-classes/>
<dep:non-overridable-classes/>
</dep:environment>
<resourceadapter>
<outbound-resourceadapter>
<connection-definition>
<connectionfactory-interface>javax.sql.DataSource
</connectionfactory-interface>
<connectiondefinition-instance>
<name>SystemDatasource</name>
<config-property-setting name="UserName"/>
<config-property-setting name="Password"/>
<config-property-setting name="DatabaseName">
SystemDatabase
</config-property-setting>
<config-property-setting name="CreateDatabase">
true
</config-property-setting>
<connectionmanager>
<xa-transaction>
<transaction-caching/>
</xa-transaction>
<single-pool>
<max-size>100</max-size>
<blocking-timeout-milliseconds>
5000
</blocking-timeout-milliseconds>
<select-one-assume-match/>
</single-pool>
</connectionmanager>
</connectiondefinition-instance>
<connectiondefinition-instance>
<name>NoTxDatasource</name>
<config-property-setting name="UserName"/>
<config-property-setting name="Password"/>
<config-property-setting name="DatabaseName">
SystemDatabase
</config-property-setting>
<config-property-setting name="CreateDatabase">
true
</config-property-setting>
<connectionmanager>
<no-transaction/>
<single-pool>
<max-size>100</max-size>
<blocking-timeout-milliseconds>5000
</blocking-timeout-milliseconds>
<select-one-assume-match/>
</single-pool>
</connectionmanager>
</connectiondefinition-instance>
</connection-definition>
</outbound-resourceadapter>
</resourceadapter>
<gbean name="DerbySystem"
class="org.apache.geronimo.derby.DerbySystemGBean">
<reference name="ServerInfo">
<name>ServerInfo</name>
</reference>
<attribute name="derbySystemHome">var/derby</attribute>
</gbean>
<gbean name="DerbyNetwork"
class="org.apache.geronimo.derby.DerbyNetworkGBean">
<reference name="derbySystem">
<name>DerbySystem</name>
</reference>
<attribute name="host">localhost</attribute>
<attribute name="port">1527</attribute>
</gbean>
<gbean name="DerbyLog"
class="org.apache.geronimo.derby.DerbyLogGBean">
<reference name="DerbySystem">
<name>DerbySystem</name>
</reference>
</gbean>
<gbean name="DerbyDriver"
class="org.apache.geronimo.system.util.JDBCDriverRegistrationGBean">
<attribute name="driverClassName">
org.apache.derby.jdbc.EmbeddedDriver
</attribute>
</gbean>
<gbean name="DerbyClientDriver"
class="org.apache.geronimo.system.util.JDBCDriverRegistrationGBean">
<attribute name="driverClassName">
org.apache.derby.jdbc.ClientDriver
</attribute>
</gbean>
<gbean name="TransactionalThreadPooledTimer" class="org.apache.geronimo.timer.jdbc.JDBCStoreThreadPooledTransactionalTimer">
<attribute name="repeatCount">5</attribute>
<reference name="TransactionManager">
<name>TransactionManager</name>
</reference>
<reference name="ManagedConnectionFactoryWrapper">
<name>SystemDatasource</name>
</reference>
<reference name="ThreadPool">
<name>DefaultThreadPool</name>
</reference>
<dependency>
<name>DerbySystem</name>
</dependency>
</gbean>
<gbean name="NonTransactionalThreadPooledTimer" class="org.apache.geronimo.timer.jdbc.JDBCStoreThreadPooledNonTransactionalTimer">
<reference name="TransactionManager">
<name>TransactionManager</name>
</reference>
<reference name="ManagedConnectionFactoryWrapper">
<name>SystemDatasource</name>
</reference>
<reference name="ThreadPool">
<name>DefaultThreadPool</name>
</reference>
<dependency>
<name>DerbySystem</name>
</dependency>
</gbean>
</connector>
|
...
By default, a JMS resource adapter that connects to embedded activemq message broker is deployed and running in the apache geronimo server. This is an outbound jms resource adapter that configures a connection factory and two message queues. The configuration name of the resource adapter is org.apache.geronimo.configs/activemq-ra/2.1/car
. The artifacts of the resource adapter are stored at <geronimo_home>/repository/org/apache/geronimo/configs/activemq-ra
. The deployment plan is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | ActiveMQ RAXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
<dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
<dep:moduleId>
<dep:groupId>org.apache.geronimo.configs</dep:groupId>
<dep:artifactId>activemq-ra</dep:artifactId>
<dep:version>2.1</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
<dep:dependencies>
<dep:dependency>
<dep:groupId>org.apache.geronimo.configs</dep:groupId>
<dep:artifactId>activemq-broker</dep:artifactId>
<dep:version>2.1</dep:version>
<dep:type>car</dep:type>
</dep:dependency>
</dep:dependencies>
<dep:hidden-classes/>
<dep:non-overridable-classes/>
</dep:environment>
<resourceadapter>
<resourceadapter-instance>
<resourceadapter-name>ActiveMQ RA</resourceadapter-name>
<config-property-setting name="ServerUrl">tcp://0.0.0.0:61616</config-property-setting>
<config-property-setting name="UserName">geronimo</config-property-setting>
<config-property-setting name="Password">geronimo</config-property-setting>
<workmanager>
<gbean-link>DefaultWorkManager</gbean-link>
</workmanager>
</resourceadapter-instance>
<outbound-resourceadapter>
<connection-definition>
<connectionfactory-interface>javax.jms.ConnectionFactory</connectionfactory-interface>
<connectiondefinition-instance>
<name>DefaultActiveMQConnectionFactory</name>
<implemented-interface>javax.jms.QueueConnectionFactory</implemented-interface>
<implemented-interface>javax.jms.TopicConnectionFactory</implemented-interface>
<connectionmanager>
<xa-transaction>
<transaction-caching/>
</xa-transaction>
<single-pool>
<max-size>10</max-size>
<blocking-timeout-milliseconds>5000</blocking-timeout-milliseconds>
<match-one/>
</single-pool>
</connectionmanager>
</connectiondefinition-instance>
</connection-definition>
</outbound-resourceadapter>
</resourceadapter>
<adminobject>
<adminobject-interface>javax.jms.Queue</adminobject-interface>
<adminobject-class>org.apache.activemq.command.ActiveMQQueue</adminobject-class>
<adminobject-instance>
<message-destination-name>MDBTransferBeanOutQueue</message-destination-name>
<config-property-setting name="PhysicalName">MDBTransferBeanOutQueue</config-property-setting>
</adminobject-instance>
</adminobject>
<adminobject>
<adminobject-interface>javax.jms.Queue</adminobject-interface>
<adminobject-class>org.apache.activemq.command.ActiveMQQueue</adminobject-class>
<adminobject-instance>
<message-destination-name>SendReceiveQueue</message-destination-name>
<config-property-setting name="PhysicalName">SendReceiveQueue</config-property-setting>
</adminobject-instance>
</adminobject>
</connector>
|
...
geronimo-admin security realm
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | geronimo-admin security realmxml |
---|
|
<module xmlns="http://geronimo.apache.org/xml/ns/deployment-1.2">
<environment>
<moduleId>
<groupId>console.realm</groupId>
<artifactId>geronimo-admin</artifactId>
<version>1.0</version>
<type>car</type>
</moduleId>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.framework</groupId>
<artifactId>j2ee-security</artifactId>
<type>car</type>
</dependency>
</dependencies>
</environment>
<gbean name="geronimo-admin"
class="org.apache.geronimo.security.realm.GenericSecurityRealm"
xsi:type="dep:gbeanType"
xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<attribute name="realmName">geronimo-admin</attribute>
<reference name="ServerInfo">
<name>ServerInfo</name>
</reference>
<xml-reference name="LoginModuleConfiguration">
<log:login-config xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0">
<log:login-module control-flag="REQUIRED" wrap-principals="false">
<log:login-domain-name>geronimo-admin</log:login-domain-name>
<log:login-module-class>
org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule
</log:login-module-class>
<log:option name="groupsURI">var/security/groups.properties</log:option>
<log:option name="usersURI">var/security/users.properties</log:option>
</log:login-module>
</log:login-config>
</xml-reference>
</gbean>
</module>
|
...
For example, the following web.xml
and geronimo-web.xml
are the deployment descriptor and Geronimo deployment plan respectively, of a web application that connects to a datasource deployed on DB2 and retrieves data from a table.
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | web.xml | xml |
---|
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<resource-ref>
<res-ref-name>jdbc/DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<welcome-file-list>
<welcome-file>jsp/EMPdemo.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
...
The web module connects to back end datasource using its JNDI name jdbc/DataSource
as declared in the web.xml
.
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | geronimo-web.xml | xml |
---|
|
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
<sys:environment>
<sys:moduleId>
<sys:groupId>samples</sys:groupId>
<sys:artifactId>EmployeeDemo</sys:artifactId>
<sys:version>2.5</sys:version>
<sys:type>war</sys:type>
</sys:moduleId>
<sys:dependencies>
<sys:dependency>
<sys:groupId>samples</sys:groupId>
<sys:artifactId>EmployeeDatasource</sys:artifactId>
<sys:version>2.5</sys:version>
<sys:type>rar</sys:type>
</sys:dependency>
</sys:dependencies>
</sys:environment>
<context-root>/EmployeeDemo</context-root>
<naming:resource-ref>
<naming:ref-name>jdbc/DataSource</naming:ref-name>
<naming:resource-link>jdbc/EmployeeDatasource</naming:resource-link>
</naming:resource-ref>
</web-app>
|
...
In the EMPdemo.jsp
, the following java code snippet is used to obtain a connection from the datasource.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | EMPdemo.jspJAVA |
---|
|
....
....
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/DataSource");
Connection con = ds.getConnection();
....
....
|
...
For example, the below XML content is the deployment descriptor (ejb-jar.xml
) of a stateless session bean that connects to a back end DB2 database.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | ejb-jar.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<description>Stateless Session Bean Example</description>
<display-name>Stateless Session Bean Example</display-name>
<enterprise-beans>
<session>
<ejb-name>RetrieveEmployeeInfoBean</ejb-name>
<business-remote>examples.session.stateless_dd.RetrieveEmployeeInfo</business-remote>
<ejb-class>examples.session.stateless_dd.RetrieveEmployeeInfoBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref>
<res-ref-name>jdbc/DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</session>
</enterprise-beans>
<interceptors>
<interceptor>
<interceptor-class>
examples.session.stateless_dd.RetrieveEmployeeInfoCallbacks
</interceptor-class>
<post-construct>
<lifecycle-callback-method>construct</lifecycle-callback-method>
</post-construct>
<post-activate>
<lifecycle-callback-method>activate</lifecycle-callback-method>
</post-activate>
<pre-passivate>
<lifecycle-callback-method>passivate</lifecycle-callback-method>
</pre-passivate>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>RetrieveEmployeeInfoBean</ejb-name>
<interceptor-class>
examples.session.stateless_dd.RetrieveEmployeeInfoCallbacks
</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
|
...
For the above deployment descriptor, we will have to provide a corresponding deployment plan file (openejb-jar.xml
) that maps the declared datasource to actual datasource deployed in the server. The following is the deployment plan.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | openejb-jar.xmlXML |
---|
|
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
<sys:environment>
<sys:moduleId>
<sys:groupId>samples</sys:groupId>
<sys:artifactId>EmployeeDemo-ejb-dd</sys:artifactId>
<sys:version>3.0</sys:version>
<sys:type>jar</sys:type>
</sys:moduleId>
<sys:dependencies>
<sys:dependency>
<sys:groupId>console.dbpool</sys:groupId>
<sys:artifactId>jdbc/FEmployeeDatasource</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>rar</sys:type>
</sys:dependency>
</sys:dependencies>
</sys:environment>
<enterprise-beans>
<session>
<ejb-name>RetrieveEmployeeInfoBean</ejb-name>
<naming:resource-ref>
<naming:ref-name>jdbc/DataSource</naming:ref-name>
<naming:resource-link>jdbc/EmployeeDatasource</naming:resource-link>
</naming:resource-ref>
</session>
</enterprise-beans>
</openejb-jar>
|
...
In the ejb bean class, the following java code is used to obtain a connection from the datasource.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | examples.session.stateless_dd.RetrieveEmployeeInfoBean.javaJAVA |
---|
|
....
....
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/DataSource");
Connection con = ds.getConnection();
....
....
|
...
The deployment descriptor of the OrderEJB.jar is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | ejb-jar.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<description>Stateless Session Bean Example</description>
<display-name>Stateless Session Bean Example</display-name>
<enterprise-beans>
<session>
<ejb-name>RetrieveOrderInfoBean</ejb-name>
<business-local>
examples.session.stateless_dd.RetrieveOrderInfo
</business-local>
<ejb-class>
examples.session.stateless_dd.RetrieveOrderInfoBean
</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref>
<res-ref-name>jdbc/DB2DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</session>
</enterprise-beans>
<interceptors>
<interceptor>
<interceptor-class>
examples.session.stateless_dd.RetrieveOrderCallbacks
</interceptor-class>
<post-construct>
<lifecycle-callback-method>construct</lifecycle-callback-method>
</post-construct>
<pre-destroy>
<lifecycle-callback-method>destroy</lifecycle-callback-method>
</pre-destroy>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>RetrieveOrderInfoBean</ejb-name>
<interceptor-class>
examples.session.stateless_dd.RetrieveOrderCallbacks
</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
|
...
In the RetrieveOrderInfoBean
, the following code is used to look up the Datasource object and obtain a database connection.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | examples.session.stateless_dd.RetrieveOrderInfoBean | JAVA |
---|
|
...
...
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/DB2DataSource");
System.out.println("Got DataSource\n");
con = ds.getConnection();
System.out.println("Got Connection\n");
...
...
|
The deployment descriptor of the OrderWEB.war
is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | web.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>OrderWEB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>RetrieveOrder</display-name>
<servlet-name>RetrieveOrder</servlet-name>
<servlet-class>
examples.web.servlet.RetrieveOrder
</servlet-class>
</servlet>
<ejb-local-ref>
<ejb-ref-name>ejb/RetrieveOrderInfo</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>
examples.session.stateless_dd.RetrieveOrderInfo
</local>
<ejb-link>RetrieveOrderInfoBean</ejb-link>
</ejb-local-ref>
<servlet-mapping>
<servlet-name>RetrieveOrder</servlet-name>
<url-pattern>/RetrieveOrder</url-pattern>
</servlet-mapping>
</web-app>
|
...
In the RetrieveOrder
servlet, the following code is used to look up the ejb to retrieve the order details.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | examples.web.servlet.RetrieveOrderJAVA |
---|
|
...
...
Context ctx = new InitialContext();
System.out.println("Instantiating beans...");
retrieveOInfo = (RetrieveOrderInfo)ctx.lookup("java:comp/env/ejb/RetrieveOrderInfo");
String orderIdStr = request.getParameter("orderid");
int orderId = Integer.parseInt(orderIdStr);
OrderInfo oInfo = retrieveOInfo.getOrderInfo(orderId);
...
...
|
The deployment descriptor of the Order.ear is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | application.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/j2ee/application_5.xsd" version="5">
<description>EAR Example</description>
<display-name>Order Sample</display-name>
<module>
<web>
<web-uri>OrderWEB.war</web-uri>
<context-root>/OrderDemo</context-root>
</web>
</module>
<module>
<ejb>OrderEJB.jar</ejb>
</module>
</application>
|
...
The deployment plan of the Order.ear
is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | geronimo-application.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"
application-name="Order">
<sys:environment>
<sys:moduleId>
<sys:groupId>Order</sys:groupId>
<sys:artifactId>OrderEAR</sys:artifactId>
<sys:version>5.0</sys:version>
<sys:type>car</sys:type>
</sys:moduleId>
</sys:environment>
<module>
<web>OrderWEB.war</web>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" >
<sys:environment>
<sys:moduleId>
<sys:groupId>Order</sys:groupId>
<sys:artifactId>OrderWEB</sys:artifactId>
<sys:version>2.5</sys:version>
<sys:type>war</sys:type>
</sys:moduleId>
</sys:environment>
<context-root>/OrderDemo</context-root>
</web-app>
</module>
<module>
<ejb>OrderEJB.jar</ejb>
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2">
<sys:environment>
<sys:moduleId>
<sys:groupId>Order</sys:groupId>
<sys:artifactId>OrderEJB</sys:artifactId>
<sys:version>3.0</sys:version>
<sys:type>jar</sys:type>
</sys:moduleId>
<sys:dependencies>
<sys:dependency>
<sys:groupId>console.dbpool</sys:groupId>
<sys:artifactId>OrderDS</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>rar</sys:type>
</sys:dependency>
</sys:dependencies>
</sys:environment>
<enterprise-beans>
<session>
<ejb-name>RetrieveOrderInfoBean</ejb-name>
<naming:resource-ref>
<naming:ref-name>jdbc/DB2DataSource</naming:ref-name>
<naming:resource-link>OrderDS</naming:resource-link>
</naming:resource-ref>
</session>
</enterprise-beans>
</openejb-jar>
</module>
</application>
|
...
The following is the deployment descriptor of the JEE application client module that looks up an ejb and calls a method on it. The ejb converts the Indian Rupess (Rs.) into American Dollars ($). The client sends a double value which is Indian Rupees to ejb. The ejb returns equivalent American Dollars as double value.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | application-client.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application-client xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application-client_5.xsd"
version="5">
<ejb-ref>
<ejb-ref-name>ejb/Converter</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<remote>examples.appclient.Converter</remote>
</ejb-ref>
</application-client>
|
...
Following is the corresponding deployment plan of the JEE client module.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | geronimo-application-client.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application-client xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:security="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:connector="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
<sys:client-environment>
<sys:moduleId>
<sys:groupId>Converter</sys:groupId>
<sys:artifactId>Converter-app-client</sys:artifactId>
<sys:version>3.0</sys:version>
<sys:type>jar</sys:type>
</sys:moduleId>
</sys:client-environment>
<sys:server-environment>
<sys:moduleId>
<sys:groupId>Converter</sys:groupId>
<sys:artifactId>Converter-app-client-server</sys:artifactId>
<sys:version>3.0</sys:version>
<sys:type>jar</sys:type>
</sys:moduleId>
</sys:server-environment>
<ejb-ref>
<ref-name>ejb/Converter</ref-name>
<naming:pattern>
<naming:groupId>Converter</naming:groupId>
<naming:artifactId>ConverterEAR</naming:artifactId>
<naming:version>5.0</naming:version>
<naming:module>ConverterEJB.jar</naming:module>
<naming:name>ConverterBean</naming:name>
</naming:pattern>
</ejb-ref>
</application-client>
|
...
The below is the client code that looks up the ejb and calls the method on it.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | ConverterClient.java | JAVA |
---|
|
package examples.appclient.client;
import javax.naming.Context;
import javax.naming.InitialContext;
import examples.appclient.Converter;
public class ConverterClient {
//The remote interface of the ConverterBean packaged with the
//JEE client jar
private static Converter converter;
private static double amount = 50;
public static void main(String[] args) {
amount = Double.parseDouble(args[0]);
doConversion();
}
public static void doConversion() {
try {
Context context = new InitialContext();
converter = (Converter)
context.lookup("java:comp/env/ejb/Converter");
double dollars = converter.getDollars(amount);
System.out.println("Rs " + amount + " is " + dollars + " Dollars.");
System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
|
The META-INF/MANIFEST.MF
file should contain the following entry for the client to run.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | MANIFEST.MFXML |
---|
|
Manifest-Version: 1.0
Main-Class: examples.appclient.client.ConverterClient
|
...
Apache geronimo ships with ActiveMQ message broker and an inbound and outbound JMS resource adapter for the ActiveMQ broker. This sample illustrates deploying and running two MDBs that listen to a jms topic TextTopic
. When the web client publishes a message to this topic, the two MDBs receive the message and process it. Because the message is published to the topic, all the configured listeners, in this case the two MDBs, receive a copy of the message. In addition to that, we will also illustrate how to deploy a JMS resource adapter within the application scope. Usually, the resource adapters are deployed at the server scope and can be used by all other applications as well. In this example, a JMS resource plan is embedded in the application deployment plan geronimo-application.xml
and deployed while deploying the application archive.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | ejb-jar.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8" ?>
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>TextMessageBean1</ejb-name>
<ejb-class>
sample.mdb.TextMessageBean1
</ejb-class>
<messaging-type>
javax.jms.MessageListener
</messaging-type>
<transaction-type>Bean</transaction-type>
<message-destination-type>
javax.jms.Topic
</message-destination-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>
destinationType
</activation-config-property-name>
<activation-config-property-value>
javax.jms.Topic
</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
<message-driven>
<ejb-name>TextMessageBean2</ejb-name>
<ejb-class>
sample.mdb.TextMessageBean2
</ejb-class>
<messaging-type>
javax.jms.MessageListener
</messaging-type>
<transaction-type>Bean</transaction-type>
<message-destination-type>
javax.jms.Topic
</message-destination-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>
destinationType
</activation-config-property-name>
<activation-config-property-value>
javax.jms.Topic
</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</ejb-jar>
|
The ejb-jar.xml
declares the two MDBs sample.mdb.TextMessageBean1
and sample.mdb.TextMessageBean2
both listen to a javax.jms.Topic destination.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | openejb-jar.xmlXML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2"
xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
<sys:environment>
<sys:moduleId>
<sys:groupId>Sample</sys:groupId>
<sys:artifactId>MDB-EJB</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>car</sys:type>
</sys:moduleId>
</sys:environment>
<enterprise-beans>
<message-driven>
<ejb-name>
TextMessageBean1
</ejb-name>
<nam:resource-adapter>
<nam:resource-link>
TradeJMSResources
</nam:resource-link>
</nam:resource-adapter>
<activation-config>
<activation-config-property>
<activation-config-property-name>
destination
</activation-config-property-name>
<activation-config-property-value>
TextMessageTopic
</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>
destinationType
</activation-config-property-name>
<activation-config-property-value>
javax.jms.Topic</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
<message-driven>
<ejb-name>TextMessageBean2</ejb-name>
<nam:resource-adapter>
<nam:resource-link>
TradeJMSResources
</nam:resource-link>
</nam:resource-adapter>
<activation-config>
<activation-config-property>
<activation-config-property-name>
destination
</activation-config-property-name>
<activation-config-property-value>
TextMessageTopic
</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>
destinationType
</activation-config-property-name>
<activation-config-property-value>
javax.jms.Topic
</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</openejb-jar>
|
...
The code for the two MDBs are as follows.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | TextMessageBean1.javaJAVA |
---|
|
package sample.mdb;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
public class TextMessageBean1 {
public TextMessageBean1() {
}
public void onMessage(Message msg) {
if (msg instanceof TextMessage) {
TextMessage tm = (TextMessage) msg;
try {
String text = tm.getText();
System.out.println("Received new message
in TextMessageBean1 : "
+ text);
System.out.println("CustomerId : " +
tm.getIntProperty("CustomerId"));
System.out.println("CustomerName : " +
tm.getStringProperty("CustomerName"));
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
|
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | TextMessageBean2.javaJAVA |
---|
|
package sample.mdb;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
public class TextMessageBean2 {
public TextMessageBean2() {
}
public void onMessage(Message msg) {
if (msg instanceof TextMessage) {
TextMessage tm = (TextMessage) msg;
try {
String text = tm.getText();
System.out.println("Received new message
in TextMessageBean2 : "
+ text);
System.out.println("CustomerId : " +
tm.getIntProperty("CustomerId"));
System.out.println("CustomerName : " +
tm.getStringProperty("CustomerName"));
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
|
...
The web client for the application is as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | web.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>MDBSampleWEB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Test</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>sample.mdb.Test</servlet-class>
</servlet>
<resource-ref>
<description>jms broker</description>
<res-ref-name>jms/broker</res-ref-name>
<res-type>
javax.jms.TopicConnectionFactory
</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-env-ref>
<description>Predefined Topic</description>
<resource-env-ref-name>
jms/Topic/TextTopic
</resource-env-ref-name>
<resource-env-ref-type>
javax.jms.Topic</resource-env-ref-type>
</resource-env-ref>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
|
The web client declares the javax.jms.TopicConnectionFactory
and the topic to which the servlet has to publish the message. The names declared here are mapped to actual resources in the geronimo-web.xml
as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | geronimo-web.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"
xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
<sys:environment>
<sys:moduleId>
<sys:groupId>sample</sys:groupId>
<sys:artifactId>MDB-Web</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>car</sys:type>
</sys:moduleId>
</sys:environment>
<context-root>/MDBSampleWEB</context-root>
<nam:resource-ref>
<nam:ref-name>
jms/broker</nam:ref-name>
<nam:resource-link>
jms/TopicConnectionFactory
</nam:resource-link>
</nam:resource-ref>
<nam:resource-env-ref>
<nam:ref-name>
jms/Topic/TextTopic
</nam:ref-name>
<nam:message-destination-link>
TextMessageTopic
</nam:message-destination-link>
</nam:resource-env-ref>
</web-app>
|
Please note that the jms/TopicConnectionFactory
and jms/Topic/TextTopic
are the names of the actual connection factory and jms topic. These are deployed by a jms resource plan embedded in the EAR's deployment plan as follows.
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | application.xml | XML |
---|
|
<?xml version="1.0" encoding="ASCII"?>
<application
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns=http://java.sun.com/xml/ns/javaee
xmlns:app="http://java.sun.com/xml/ns/javaee/application_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd"
version="5">
<display-name>MDBSampleEAR</display-name>
<module>
<ejb>MDBSampleEJB.jar</ejb>
</module>
<module>
<web>
<web-uri>MDBSampleWEB.war</web-uri>
<context-root>MDBSampleWEB</context-root>
</web>
</module>
</application>
|
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | geronimo-application.xml | XML |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<application
xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-2.0"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"
application-name="MDBSampleEAR">
<sys:environment>
<sys:moduleId>
<sys:groupId>default</sys:groupId>
<sys:artifactId>MDBSampleEAR</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>car</sys:type>
</sys:moduleId>
</sys:environment>
<ext-module>
<connector>TopicJMSSample</connector>
<external-path>
<sys:groupId>
org.apache.geronimo.modules
</sys:groupId>
<sys:artifactId>
geronimo-activemq-ra
</sys:artifactId>
<sys:version>2.1</sys:version>
</external-path>
<connector
xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
<resourceadapter>
<!--how to connect to the JMS Server-->
<resourceadapter-instance>
<resourceadapter-name>
TradeJMSResources
</resourceadapter-name>
<config-property-setting name="ServerUrl">
tcp://localhost:61616
</config-property-setting>
<config-property-setting name="UserName">
not needed
</config-property-setting>
<config-property-setting name="Password">
not needed
</config-property-setting>
<workmanager>
<gbean-link>DefaultWorkManager</gbean-link>
</workmanager>
</resourceadapter-instance>
<!--defines a ConnectionFactory-->
<outbound-resourceadapter>
<connection-definition>
<connectionfactory-interface>
javax.jms.ConnectionFactory
</connectionfactory-interface>
<connectiondefinition-instance>
<name>jms/TopicConnectionFactory</name>
<implemented-interface>
javax.jms.TopicConnectionFactory
</implemented-interface>
<connectionmanager>
<xa-transaction>
<transaction-caching/>
</xa-transaction>
<single-pool>
<max-size>10</max-size>
<min-size>0</min-size>
<blocking-timeout-milliseconds>
5000
</blocking-timeout-milliseconds>
<idle-timeout-minutes>0</idle-timeout-minutes>
<match-one/>
</single-pool>
</connectionmanager>
</connectiondefinition-instance>
</connection-definition>
</outbound-resourceadapter>
</resourceadapter>
<adminobject>
<adminobject-interface>
javax.jms.Topic</adminobject-interface>
<adminobject-class>
org.activemq.message.ActiveMQTopic
</adminobject-class>
<adminobject-instance>
<message-destination-name>
TextMessageTopic
</message-destination-name>
<config-property-setting name="PhysicalName">
TextMessageTopic
</config-property-setting>
</adminobject-instance>
</adminobject>
</connector>
</ext-module>
</application>
|
...
Note |
---|
The reference to resource archive file is provided using the following xml elements in the above plan. Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | Referencing LibrariesJAVA |
---|
|
<external-path>
<sys:groupId>
org.apache.geronimo.modules
</sys:groupId>
<sys:artifactId>
geronimo-activemq-ra
</sys:artifactId>
<sys:version>2.1</sys:version>
</external-path>
|
The above pattern is the way how geronimo references various libraries available in the server repository. Any external libraries can also be uploaded to server repository using admin console portlet. After starting the server, click on Console Navigation => Services => Repository to display Repository Viewer portlet. In this portlet, users can upload required third party libraries by providing proper groupId, artifactId and version values for the libraries being uploaded. The activeMQ rar file is also available in the repository as org.apache.geronimo.modules/geronimo-activemq-ra/2.1/rar . Click on this link to get the usage instructions on how to reference this library in other modules. |
The web client that look up the connection factory and topic and sends messages is as follows.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | Test.javaJAVA |
---|
|
package sample.mdb;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.DeliveryMode;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jms.Connection;
public class Test extends
javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private TopicConnection connection;
private Topic topic;
public Test() {
super();
}
public void init()
throws ServletException {
String connectionFactoryName = "java:comp/env/jms/broker";
String topicName = "java:comp/env/jms/Topic/TextTopic";
try {
InitialContext naming = new InitialContext();
TopicConnectionFactory connectionFactory =
(TopicConnectionFactory)
naming.lookup(connectionFactoryName);
connection =
connectionFactory.createTopicConnection();
topic = (Topic) naming.lookup(topicName);
}catch(Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
TopicSession publishSession = null;
PrintWriter out = response.getWriter();
try {
boolean transacted = false;
publishSession =
connection.createTopicSession(transacted,
Session.AUTO_ACKNOWLEDGE);
TopicPublisher sender =
publishSession.createPublisher(topic);
sender.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage message =
publishSession.createTextMessage("Customer Info");
message.setIntProperty("CustomerId",
Integer.parseInt(request.getParameter("CustomerId")));
message.setStringProperty("CustomerName",
request.getParameter("CustomerName"));
sender.send(message);
out.println("Message is successfully sent...!!");
}
catch(Exception e) {
throw new ServletException(e);
}
finally {
try {
if (publishSession != null) {
publishSession.close();
}
}
catch (Exception e) {}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
}
public void destroy() {
if (connection != null) {
try {
if (connection != null) {
connection.close();
}
}
catch (Exception e) { }
}
}
}
|
...