Versions Compared

Key

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

...

The following commands illustrates the deployment and running of the client module.

No Format
bgColor#000000
borderStylesolid
C:\\Geronimo-2.1\bin>deploy.bat --user system --password manager deploy C:\temp\ConverterEJBClient.jar
Using GERONIMO_BASE:   C:\Geronimo-2.1
Using GERONIMO_HOME:   C:\Geronimo-2.1
Using GERONIMO_TMPDIR: var\temp
Using JRE_HOME:        C:\JDK\jre
    Deployed Converter/Converter-app-client-server/3.0/jar

C:\Geronimo-2.1\bin>java -Djava.endorsed.dirs="C:\Geronimo-2.1\lib\endorsed" -jar
              C:\Geronimo-2.1\bin\client.jar Converter/Converter-app-client/3.0/jar 4000

Rs 4000.0 is 100.0 Dollars.

Message Driven Bean deployment plan.

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
borderStylesolid
titleejb-jar.xml

<?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
borderStylesolid
titleopenejb-jar.xml

<?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>

In the deployment plan openejb-jar.xml, the two MDBs are configured as end point listeners for the jms topic TextMessageTopic.

The code for the two MDBs are as follows.

Code Block
JAVA
JAVA
borderStylesolid
titleTextMessageBean1.java

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
borderStylesolid
titleTextMessageBean2.java

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();
   }
  }
 }
}

After receiving the message, the MDBs just print the contents of the message.

The web client for the application is as follows.

Code Block
XML
XML
borderStylesolid
titleweb.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
borderStylesolid
titlegeronimo-web.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
borderStylesolid
titleapplication.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
borderStylesolid
titlegeronimo-application.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>

The resource adapter is configured under <ext-module> xml elements. Also, these elements mention the resource archive (rar)}}to be used to deploy the JMS resource adapter in {{external-path xml elements; the resource adapter plan is provided later. It configures {{ jms/TopicConnectionFactory}} and TextTopic.

The web client that look up the connection factory and topic and sends messages is as follows.

Code Block
JAVA
JAVA
borderStylesolid
titleTextMessageBean1.java

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) {          }
 }
 }

}

When the above servlet is hit on a browser with as http://localhost:8080/MDBSampleWEB/Test?CustomerId=10&CustomerName=PhaniImage Added,

The following output is displayed in the server console.

No Format
bgColor#000000
borderStylesolid

-------------------------------------------
Received new message in TextMessageBean1 : Customer Info
CustomerId : 10
CustomerName : Phani
-------------------------------------------
-------------------------------------------
Received new message in TextMessageBean2 : Customer Info
CustomerId : 10
CustomerName : Phani
-------------------------------------------

Code Block