- Setup XDoclet
- Download XDoclet from here, and extract it.
- In Eclipse->Window->preferences, select xdoclet and set the Xdoclet home to the appropriate directory.
- Create the Message Driven Bean
- Create an EJB project in Eclipse.
- In the J2EE perspective, right-click on the Deployment descriptor and create a new Message Driven Bean. Eclipse generates the required classes and the ejb-jar.xml file with the new MDB definition in it. Modify the Bean to look like this
public class MessagingExampleBean implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
Add the following definitions to the ejb-jar.xml
private javax.ejb.MessageDrivenContext messageContext = null;
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext messageContext) throws javax.ejb.EJBException {
this.messageContext = messageContext;
}
public void ejbCreate() {
}
public void ejbRemove() {
messageContext = null;
}
public MessagingExampleBean() {
}
public void onMessage(javax.jms.Message message) {
System.out.println("Message Driven Bean got message " + message);
}
}<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="2.1" 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/ejb-jar_2_1.xsd">
<display-name>MessagingExample</display-name>
<enterprise-beans>
<message-driven>
<display-name>MessagingExampleMDB</display-name>
<ejb-name>MessagingExampleMDB</ejb-name>
<ejb-class>jms.MessagingExampleMdb</ejb-class>
<transaction-type>Bean</transaction-type>
<message-destination-type>javax.jms.Queue</message-destination-type>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MessagingExampleMDB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar> - Create a new file weblogic-ejb-jar.xml. This is required for Weblogic bindings.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd">
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>MessagingExampleMDB</ejb-name>
<message-driven-descriptor>
<pool>
<max-beans-in-free-pool>5</max-beans-in-free-pool>
<initial-beans-in-free-pool>5</initial-beans-in-free-pool>
</pool>
<destination-jndi-name>jms/testQueue</destination-jndi-name>
<initial-context-factory>weblogic.jndi.WLInitialContextFactory</initial-context-factory>
<connection-factory-jndi-name>jms/connectionFactory</connection-factory-jndi-name>
<jms-polling-interval-seconds>20</jms-polling-interval-seconds>
</message-driven-descriptor>
<transaction-descriptor>
<trans-timeout-seconds>3600</trans-timeout-seconds>
</transaction-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
- Create the Client. For this example, I used a servlet that simply sends a "Hello" message to the MDB through the Queue. Here is the code for it
public class MessaginClientServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "weblogic.examples.jms.QueueConnectionFactory";
public final static String QUEUE = "weblogic.examples.jms.exampleQueue";
public MessaginClientServlet() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Context ctx = getInitialContext("t3://localhost:20001");
QueueConnectionFactory qconFactory;
QueueConnection connection;
QueueSession session;
QueueSender sender;
Queue queue;
TextMessage msg;
qconFactory = (QueueConnectionFactory) ctx.lookup("jms/connectionFactory");
connection = qconFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup("jms/testQueue");
msg = session.createTextMessage();
sender = session.createSender(queue);
msg.setText("Hello World");
connection.start();
sender.send(msg);
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private InitialContext getInitialContext(String url) throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
Wednesday, December 06, 2006
Messaging Quickstart: Sample Code
The previous post described how to setup a Queue in Weblogic Server. This post shows the code necessary to run a Simple Messaging example using a servlet and Message Driven Bean. You can always implement an message listener instead of using a Message Driven Bean, but using MDBs is much cleaner and easier. Follow these steps to run the example
Labels:
example/sample code,
messaging,
weblogic
Subscribe to:
Post Comments (Atom)
Popular Posts
-
In a previous post, I described how to use Quartz scheduler for scheduling . In this post, I describe the configuration changes required for...
-
JUnit 4 introduces a completely different API to the older versions. JUnit 4 uses Java 5 annotations to describe tests instead of using in...
-
New posts with iText 5.5.12 Following are two new posts for PDF Merge with iText 5.5.12 Merge PDF files using iText 5 Merge and Paginate PDF...
-
This post will describe how to create and deploy a Java Web Application war to Heroku using Heroku CLI. You will need a basic understanding ...
-
The JDBC abstraction layer of Spring framework offers an understandable exception hierarchy, simplifies error handling, and greatly reduces ...
-
The String class doesn't have the ability to compare text from a natural language perspective. Its equals and compareTo methods compa...
-
In this post we will see how to do an offline install Jenkins and required plugins on a Red Hat Enterprise Linux Server release 7.3. This is...
-
Last week, I described how to implement JMS, using a stand-alone client and a Message Driven Bean . In this post and the next, I will descr...
-
The previous post described how to implement a JMS messaging client using Spring JMS . This post will describe how to implement the Message ...
-
In the past, I had a few posts on how to implement pagination using displaytag( 1 , 2 ). That solution is feasible only with small result se...
Hey, this post rocks. It saved my head at work!!! :) :) :) :)
ReplyDeleteHi,
ReplyDeletewhile I sending message from client i m facing following error,
javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.io.EOFException]
at weblogic.jrmp.Context.lookup(Context.java:189)
at weblogic.jrmp.Context.lookup(Context.java:195)
at javax.naming.InitialContext.lookup(Unknown Source)
at JMSClient.sendToQueue(JMSClient.java:41)
at JMSClient.main(JMSClient.java:26)
Caused by: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.io.EOFException
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at weblogic.jrmp.Context.lookup(Context.java:185)
... 4 more
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(Unknown Source)
... 9 more
Thanks in advance,
Rahul.
Hi, Have you been able to solve that javax.naming.CommunicationException.
ReplyDeleteI have the same thing.
Thanks,
Alex
Kudos to Abhi. Thanks for simplifying simple things.
ReplyDeleteThis is an exellent article. People tried reading the weblogic article 100 times and they don't get how to create a simple Queue and QueueConenction factory and MDB. Though I created in activeMQ and others easily, this weblogic JMS Queue's documentation especially for JMS on BEA server is confusing enough for any level of professional. Suggestion to Bea(oh, now it is Oracle right), to incorporate this link in their documentation.
Really good article from implementation prospective. Thanks .
ReplyDeleteYou have given good example by which we developer can gain confidance by practicaly doing the things rather than simply reading the theory.
Whats the resolution for the above menitoned error
ReplyDeleteHi Frens,
ReplyDeleteI tried working on this sample code but couldnt run it fine.
Can anyone please elaborate what are the next steps after done with all coding (what to run and how). I am very new to JMS.
Thanks and Cheers,
You absolutely have wonderful stories.
ReplyDeleteI really like your website.
ReplyDeleteThis is very good and useful information.
ReplyDeletethank you for giving such useful information that is often difficult to come by. Good job.
ReplyDeletetruly appreciate this post. I’ve been looking everywhere for this!
ReplyDeleteThank you for a great article.
ReplyDelete