...
The code for the two MDBs are as follows.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
title | TextMessageBean1.java |
---|
borderStyle | solid | 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 |
---|
title | TextMessageBean2.java |
---|
borderStyle | solid | 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();
}
}
}
}
|
...
Code Block |
---|
title | application.xml |
---|
borderStyle | solid |
---|
|
<?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 |
---|
title | geronimo-application.xml |
---|
borderStyle | solidXML |
---|
|
<?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 |
---|
title | Referencing Libraries |
---|
borderStyle | solidJAVA |
---|
|
<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 |
---|
title | Test.java |
---|
borderStyle | solidJAVA |
---|
|
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) { }
}
}
}
|
...