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.
Note the various @ActivationConfigProperty annotations. The property names are specific to the particular resource adapter used. Each resource adapter is required to specify an ActivationSpec javabean and the javabean properties are the allowable propertyNames for these annotations. The ActiveMQ ActivationSpec class is org.apache.activemq.ra.ActiveMQActivationSpec.

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{

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

In this application there is a MDB that will listen on OrderQueue. The openejb-jar section of the geronimo plan indicates that the OrderRecvMDB MDB uses the jms-resources resource adapter instance.

Code Block
xml
xml
borderStylesolid
titleopenejb-jar.xmlxml
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2">
    <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>

The connector section of the geronimo plan configures a resource adapter instance named "jms-resources" that has a ConnectionFactory to be used by the web client and the Order queue used by both client and mdb.

Code Block
xml
xml
borderStylesolid
titlejms-resources.xmlxml
        <connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
           <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>
        </connector>    
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("(client) 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("(client) 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>

...