Versions Compared

Key

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

...

The Message-Driven Bean uses the @MessageDriven annotation to replace the declaration of this MDB in the ejb-jar.xml file. By providing the annotation with further information it knows to look for a destination (in this case it happens to be a queue) to process. So this MDB will sit there and process messages passed into the 'OrderQueue.' The end result is that is echoes this message to the screen.

Code Block
java
java
borderStylesolid
titleOrderRecvMDB.javajava
//
// MessageDrivenBean that listens to items on the
// 'OrderQueue' queue and processes them accordingly.
//
@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
    @ActivationConfigProperty(propertyName="destination", propertyValue="OrderQueue")
})
public class OrderRecvMDB implements MessageListener{

    private static final String ORDER_MGMT_INFO = "order_mgmt.properties";
    private static final String ORDER_REPO = "order.repo";

    public OrderRecvMDB() {
    }

    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("Order Received \n"+ textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

In this application there is a MDB that will listen on OrderQueue. openejb-jar.xml tells Geronimo that there is a MDB which is associated with the jms-resources JMS Resource Group. It links OrderRecvMDB with OrderQueue via CommonConnectionFactory.

Code Block
xml
xml
borderStylesolid
titleopenejb-jar.xmlxml
<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.2">
    <sys:environment>
        <sys:moduleId>
	    <sys:groupId>${pom.groupId}</sys:groupId>
	    <sys:artifactId>${pom.artifactId}</sys:artifactId>
	    <sys:version>${version}</sys:version>
	    <sys:type>jar</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:dependencies>
        <sys:hidden-classes/>
        <sys:non-overridable-classes/>
    </sys:environment>
    <enterprise-beans>
        <message-driven>
            <ejb-name>OrderRecvMDB</ejb-name>
            <resource-adapter>
                <resource-link>jms-resources</resource-link>
            </resource-adapter>
        </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.  This geronimo-application.xml also includes a section for defining a JMS queue and a common queue connection factory to access it. This is used for deploying the geronimo-activemq-ra.rar that is embedded in the ear.

Code Block
xml
xml
borderStylesolid
titlegeronimo-application.xmlxml
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.1">
    <environment xmlns="http://geronimo.apache.org/xml/ns/deployment-1.2">
        <moduleId>
            <groupId>${pom.groupId}</groupId>
            <artifactId>${pom.artifactId}</artifactId>
            <version>${version}</version>
            <type>ear</type>
        </moduleId>
    </environment>
    <module>
       <connector>geronimo-activemq-ra-2.0-SNAPSHOT.rar</connector>
        <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>${pom.groupId}</dep:groupId>
                    <dep:artifactId>jms-resources</dep:artifactId>
                    <dep:version>${version}</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>jms-resources</resourceadapter-name>
                    <nam:workmanager xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2">
                        <nam:gbean-link>DefaultWorkManager</nam:gbean-link>
                    </nam:workmanager>
                </resourceadapter-instance>
                <outbound-resourceadapter>
                    <connection-definition>
                        <connectionfactory-interface>javax.jms.ConnectionFactory</connectionfactory-interface>
                        <connectiondefinition-instance>
                            <name>CommonConnectionFactory</name>
                            <implemented-interface>javax.jms.QueueConnectionFactory</implemented-interface>
                            <implemented-interface>javax.jms.TopicConnectionFactory</implemented-interface>
                            <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.apache.activemq.command.ActiveMQQueue</adminobject-class>
                <adminobject-instance>
                    <message-destination-name>OrderQueue</message-destination-name>
                    <config-property-setting name="PhysicalName">OrderQueue</config-property-setting>
                </adminobject-instance>
            </adminobject>
            <adminobject>
                <adminobject-interface>javax.jms.Topic</adminobject-interface>
                <adminobject-class>org.apache.activemq.command.ActiveMQTopic</adminobject-class>
            </adminobject>
        </connector>    
    </module>
</application>
Code Block
xml
xml
borderStylesolid
titleapplication.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<application 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_5.xsd" version="5">
  <description>Geronimo Sample EAR for jms-mdb-sample</description>
  <display-name>Geronimo Sample EAR for jms-mdb-sample</display-name>
  <module>
    <connector>geronimo-activemq-ra-2.0-SNAPSHOT.rar</connector>
  </module>
  <module>
    <ejb>jms-mdb-sample-ejb-2.0-SNAPSHOT.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>jms-mdb-sample-war-2.0-SNAPSHOT.war</web-uri>
      <context-root>/order</context-root>
    </web>
  </module>
</application>

...

Note

Please note that Geronimo ignores the 'mappedName' configuration attribute for @Resource. Instead, use 'name' when annotating.

Code Block
java
java
borderStylesolid
titleOrderSenderServlet.javajava
public class OrderSenderServlet extends HttpServlet {

    @Resource(name="CommonConnectionFactory")
    private ConnectionFactory factory;

    @Resource(name="OrderQueue")
    private Queue receivingQueue;

    public void init() throws ServletException {
        super.init();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        manageOrders(req,res);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req,res);
    }

    private void manageOrders(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
        String path = "/error.jsp";
        Connection connection = null;
	MessageProducer messageProducer = null;
	Session sess = null;
	try
        {
	    String customerId = req.getParameter("customerId");
	    String orderId = req.getParameter("orderId");
	    String qty = req.getParameter("quantity");
	    String model = req.getParameter("model");

	    if(!customerId.equals("") && !orderId.equals("") && !qty.equals("")){
	    	System.out.println("Start Sending Order Request");
	    	// creating online order request
	    	String orderRequest = "<Order orderId=\""+orderId+"\" custId=\""+customerId+"\" qty=\""+qty+"\" model=\""+model+"\"/>" ;
		connection = factory.createConnection();
		sess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		path = "/index.jsp";
		TextMessage msg = sess.createTextMessage("<OrderId=" + orderId + " CustomerId=" + customerId
							+ " Quantity=" + qty + " Model=" + model + ">" );
		messageProducer = sess.createProducer(receivingQueue);
		messageProducer.send(msg);
		System.out.println("Order Request Send");
	    } else{
	    	String error = "";

	    	if(customerId.equals("")){
	    	    error = "Customer Id Cannot be Empty";
	    	}else if(orderId.equals("")){
	    	    error = "Order Id Cannot be Empty";
	    	}else if(qty.equals("")){
	    	    error = "Quantity Cannot be Empty";
	    	}
	    	req.setAttribute("error",error);
	    }
        } catch (Exception e)
        {
            System.out.println("Error "+e);
            e.printStackTrace();
        } finally {
            try {
                if(messageProducer != null) messageProducer.close();
                if(sess != null)sess.close();
                if(connection!= null)connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        getServletContext().getRequestDispatcher(path).forward(req,res);
    }
}

web.xml of the archive has the relevant configurations for the both queue connection factory and the queue, which is essential to refer to resources in a local enviroment.

Code Block
xml
xml
borderStylesolid
titleweb.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
    <description>JMS Servlet Sample</description>
    <servlet>
        <servlet-name>OrderSenderServlet</servlet-name>
        <servlet-class>org.apache.geronimo.samples.order.OrderSenderServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>OrderSenderServlet</servlet-name>
        <url-pattern>/order</url-pattern>
    </servlet-mapping>

    <resource-ref>
	<res-ref-name>CommonConnectionFactory</res-ref-name>
	<res-type>javax.jms.QueueConnectionFactory</res-type>
	<res-auth>Container</res-auth>
	<es-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

    <message-destination-ref>
	<message-destination-ref-name>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>/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

...