Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

First, we will look at defining the connection factories and queues in the Geronimo server using jms-resource-plan.xml. It defines two JMS queues and a common queue connection factory to access them.

Code Block
xml
borderStylesolid
titlejms-resource-plan.xml
borderStylesolid
xml
<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.1">
    <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
        <dep:moduleId>
            <dep:groupId>org.apache.geronimo.samples</dep:groupId>
            <dep:artifactId>jms-resources</dep:artifactId>
            <dep:version>1.2</dep:version>
            <dep:type>rar</dep:type>
        </dep:moduleId>
        <dep:dependencies>
            <dep:dependency>
                <dep:groupId>org.apache.geronimo.configs</dep:groupId>
                <dep:artifactId>activemq-broker</dep:artifactId>
                <dep:type>car</dep:type>
            </dep:dependency>
        </dep:dependencies>
    </dep:environment>
    <resourceadapter>
        <resourceadapter-instance>
            <resourceadapter-name>CommonConnectionFactory</resourceadapter-name>
            <config-property-setting name="Password">geronimo</config-property-setting>
            <config-property-setting name="UserName">geronimo</config-property-setting>
            <nam:workmanager xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1">
                <nam:gbean-link>DefaultWorkManager</nam:gbean-link>
            </nam:workmanager>
        </resourceadapter-instance>
        <outbound-resourceadapter>
            <connection-definition>
                <connectionfactory-interface>javax.jms.QueueConnectionFactory</connectionfactory-interface>
                <connectiondefinition-instance>
                    <name>CommonConnectionFactory</name>
                    <connectionmanager>
                        <xa-transaction>
                            <transaction-caching/>
                        </xa-transaction>
                        <single-pool>
                            <match-one/>
                        </single-pool>
                    </connectionmanager>
                </connectiondefinition-instance>
            </connection-definition>
        </outbound-resourceadapter>
    </resourceadapter>
    <adminobject>
        <adminobject-interface>javax.jms.Queue</adminobject-interface>
        <adminobject-class>org.activemq.message.ActiveMQQueue</adminobject-class>
        <adminobject-instance>
            <message-destination-name>OrderQueue</message-destination-name>
            <config-property-setting name="PhysicalName">OrderQueue</config-property-setting>
        </adminobject-instance>
		<adminobject-instance>
            <message-destination-name>ConsignmentQueue</message-destination-name>
            <config-property-setting name="PhysicalName">ConsignmentQueue</config-property-setting>
        </adminobject-instance>
    </adminobject>
    <adminobject>
        <adminobject-interface>javax.jms.Topic</adminobject-interface>
        <adminobject-class>org.activemq.message.ActiveMQTopic</adminobject-class>
    </adminobject>
</connector>

In this application there is a MDB that will listen on OrderQueue. openejb-jar.xml defines Geronimo specific features of that MDB. It links OrderRecv MDB with OrderQueue via CommonConnectionFactory.

Code Block
xmlborderStylesolid
titleopenejb-jar.xml
borderStylesolid
xml
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
 xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.1" 
 xmlns:security="http://geronimo.apache.org/xml/ns/security-1.1" 
 xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
  <sys:environment>
    <sys:moduleId>
      <sys:groupId>org.apache.geronimo.samples</sys:groupId>
      <sys:artifactId>OrderEjb</sys:artifactId>
      <sys:version>1.2</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
    <sys:dependencies>
		<sys:dependency>
			<sys:groupId>org.apache.geronimo.configs</sys:groupId>
			<sys:artifactId>activemq-broker</sys:artifactId>
			<sys:type>car</sys:type>
        </sys:dependency>
		<sys:dependency>
            <sys:groupId>org.apache.geronimo.samples</sys:groupId>
            <sys:artifactId>jms-resources</sys:artifactId>
            <sys:version>1.2</sys:version>
            <sys:type>rar</sys:type>
        </sys:dependency>
    </sys:dependencies>
    <sys:hidden-classes/>
    <sys:non-overridable-classes/>
  </sys:environment>
  <enterprise-beans>
    <message-driven>
      <ejb-name>OrderRecvMDB</ejb-name>
		<resource-adapter>
			<resource-link>CommonConnectionFactory</resource-link>
      	</resource-adapter>
		<activation-config>
		  <activation-config-property>
		    <activation-config-property-name>destination</activation-config-property-name>
		    <activation-config-property-value>OrderQueue</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.Queue</activation-config-property-value>
		  </activation-config-property>
		</activation-config>
    </message-driven>
  </enterprise-beans>
</openejb-jar>

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 as usual.

Code Block
xmlborderStylesolid
titlegeronimo-application.xml
borderStylesolid
xml
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.1">
  <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
    <dep:moduleId>
      <dep:groupId>org.apache.geronimo.samples</dep:groupId>
      <dep:artifactId>Order</dep:artifactId>
      <dep:version>1.2</dep:version>
      <dep:type>car</dep:type>
    </dep:moduleId>
    <dep:dependencies/>
    <dep:hidden-classes/>
    <dep:non-overridable-classes/>
  </dep:environment> 
</application>


Code Block
borderStyle
xmlsolid
titleapplication.xml
borderStylesolid
xml
<?xml version="1.0" encoding="UTF-8"?>
<application 
       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/application_1_4.xsd"
       version="1.4">
    <module>
        <ejb>OrderEjb.jar</ejb>
    </module> 
	<module>
		<web>
			<web-uri>OrderWeb.war</web-uri>
			<context-root>/Order</context-root>
		</web>
	</module>
</application>

Order Processing Web application sends messages to the Order Queue. OrderSenderServlet will handle the relevant order request generation and the sending. web.xml of the archive has the relevant configurations for the both queue connection factory and the queue, which is essential to refer resources in a local enviroment.

Code Block
xmlborderStylesolid
titleweb.xml
borderStylesolid
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_4.xsd"
		version="2.4">
	
	<servlet>
		<display-name>OrderSenderServlet</display-name>
		<servlet-name>OrderSenderServlet</servlet-name>
		<servlet-class>org.apache.geronimo.samples.order.web.OrderSenderServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>OrderSenderServlet</servlet-name>
		<url-pattern>/order</url-pattern>
	</servlet-mapping>	
	
	<resource-ref>
	    <res-ref-name>jms/CommonConnectionFactory</res-ref-name>
	    <res-type>javax.jms.QueueConnectionFactory</res-type>
	    <res-auth>Container</res-auth>
		<res-sharing-scope>Shareable</res-sharing-scope>
 	</resource-ref>
	
	<message-destination-ref>
	    <message-destination-ref-name>jms/OrderQueue</message-destination-ref-name>
	    <message-destination-type>javax.jms.Queue</message-destination-type>
	    <message-destination-usage>Produces</message-destination-usage>
	    <message-destination-link>OrderQueue</message-destination-link>
	</message-destination-ref>
	
	<welcome-file-list>
		<welcome-file>/jsp/index.jsp</welcome-file>
	</welcome-file-list>
	
</web-app>

...

geronimo-web.xml will act as a mediator between defined JMS resources in the application server and the web.xml.

Code Block
borderStyle
xmlsolid
titlegeronimo-web.xml
borderStylesolid
xml
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.1">
  <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
    <dep:moduleId>
      <dep:groupId>org.apache.geronimo.samples</dep:groupId>
      <dep:artifactId>OrderWeb</dep:artifactId>
      <dep:version>1.2</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:type>car</dep:type>
        </dep:dependency>
		<dep:dependency>
            <dep:groupId>org.apache.geronimo.samples</dep:groupId>
            <dep:artifactId>jms-resources</dep:artifactId>
            <dep:version>1.2</dep:version>
            <dep:type>rar</dep:type>
        </dep:dependency>
	</dep:dependencies>
    <dep:hidden-classes/>
    <dep:non-overridable-classes/>
  </dep:environment>
  <context-root>/Order</context-root> 
  <resource-ref>
    <ref-name>jms/CommonConnectionFactory</ref-name>
    <resource-link>CommonConnectionFactory</resource-link>
  </resource-ref>

  <resource-env-ref>
	   <ref-name>jms/OrderQueue</ref-name>
	   <admin-object-link>OrderQueue</admin-object-link>
  </resource-env-ref> 
</web-app>

The next important part of this sample application is how to send messages from out side the application server context. Consignment sender will handle it for the application as given below.

Code Block
javaborderStylesolid
titleConsignmentSender.java
borderStylesolid
java
Context ctx = new InitialContext(env);			
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup(propLoader.getValue(CONNECTION_FACTORY_NAMES));		
conn = factory.createQueueConnection();			
Queue myQueue = (Queue) ctx.lookup(propLoader.getValue(QUEUE_NAME));			
session = conn.createQueueSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(myQueue);
conn.start();		
	
consignmentMessage = session.createTextMessage();			
consignmentMessage.setText(content);			
producer.send(consignmentMessage);			
System.out.println("Consignment Sent !!!");

How to listen on a JMS queue other than a MDB? The answer to this question can be found in the consignment reciever application.

Code Block
javaborderStylesolid
titleConsignmentReciever.java
borderStylesolid
java
System.out.println("Start Listening Consignment Data ");
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(propLoader.getValue(PROVIDER_URL));
connection = (QueueConnection)connectionFactory.createConnection();
connection.start();
			
session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(propLoader.getValue(QUEUE_NAME));
consumer = session.createConsumer(queue);

while(true){
	Message message = consumer.receive();
	processMessage(message);
}

...

Set the correct paths for the xdoclet.home and geronimo.home directories to perform the application build process correctly.

Code Block
borderStyle
xmlsolid
titlebuild.properties
borderStylesolid
xml
## Set the Geronimo 1.2 Home
geronimo.home=/home/karthi/Projects/Geronimo/geronimo-jetty-j2ee-1.2-beta

## Set the XDoclet Home 
xdoclet.home=/home/karthi/installation/xdoclet-1.2.3

Note

This build script depends on XDoclet version 1.2.3 and Geronimo 1.2. Also make sure to use "/" as you directory separator in Windows environment when you are setting the properties.

Code Block
xmlborderStylesolid
titleorder_mgmt.properties
borderStylesolid
xml
###########################################################

## Order Sender(Web Application) Properties
#  Connection Factory Name. 
jms.connection=java:comp/env/jms/CommonConnectionFactory
# Queue Name.
jms.queue=java:comp/env/jms/OrderQueue

###########################################################

## Order Reciever(EJB Application) Properties
#  Change a directory in server to store order requests. 
order.repo=/home/lsf/Lasantha/Temp/order

###########################################################

## Consignment Sender Properties
#  Queue connection factory type.
java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
#  Server location, change according to your enviroment.
java.naming.provider.url=tcp://localhost:61616
#  Connection factory names.
connectionFactoryNames=CommonConnectionFactory
# Queue name.
queue.ConsignmentQueue=ConsignmentQueue

###########################################################

## Consignment Reciever Properties
#  Provider url, change according to your enviroment.
provider.url=tcp://localhost:61616
# Queue name.
queue.name=ConsignmentQueue
# Change a directory in client machine to store consignment requests.
consignment.dir=/home/lsf/Lasantha/Temp/consignment

...