- Download and Install Glassfish: You can download the latest build of Glassfish from the Glassfish Download site. To install follow these steps
- In the download directory, run the following command
java -Xmx256m -jar glassfish-installer-version-build.jar
- The previous command will create a directory by the name glassfish. Go to the glassfish directory and run this command
ant -f setup-cluster.xml
- The admin console for the default installation will be at http://localhost:4848/asadmin, and the default username and password are "admin" and "adminadmin" respectively.
- In the download directory, run the following command
- Download and Install the Glassfish Plugin for Eclipse from here.
- Create a Glassfish Server in Eclipse: (For some reason, Eclipse did not detect the Server Runtime without creating a Server, we'll worry about that later)
- Creating the EJB 3 Message Driven Bean:
- Create a "Java project" in Eclipse.
- Add the Glassfish runtime library as a dependency for the project.
- The following is the code for the Message Driven Bean that I used for the Example. This is in the jms package of the Java project.
package jms;
import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(mappedName = "jms/testQueue")
public class Messaging3Mdb implements MessageListener {
@Resource
private MessageDrivenContext mdc;
public Messaging3Mdb() {
}
public void onMessage(Message inMessage) {
TextMessage msg = null;
try {
msg = (TextMessage) inMessage;
System.out.println("Message received : " + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
}
}
}Messaging3Mdb.java
- Creating the Client: I used a Servlet for the client, so that I could also use JMS resource injection. To create the Client
- Create a "Dynamic Web Project" in Eclipse.
- Change the Web.xml file to Reflect Java EE 5 descriptor, as shown below
<?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">
<display-name>Messaging3Web</display-name>
<servlet>
<description></description>
<display-name>MessagingClient</display-name>
<servlet-name>MessagingClient</servlet-name>
<servlet-class>servlets.MessagingClient</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessagingClient</servlet-name>
<url-pattern>/MessagingClient</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>web.xml - This is the code for the Servlet that acts as a client to the MDB created above
package servlets;
import java.io.IOException;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MessagingClient extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
@Resource(mappedName = "jms/testQueue")
private Queue queue;
@Resource(mappedName = "jms/connectionFactory")
private ConnectionFactory jmsConnectionFactory;
public MessagingClient() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection connection = null;
Destination dest = (Destination) queue;
try {
connection = jmsConnectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(dest);
TextMessage message = session.createTextMessage();
message.setText("Hello");
response.getOutputStream().println("Sending message: " + message.getText());
System.out.println("Sending message: " + message.getText());
producer.send(message);
producer.send(session.createMessage());
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}MessagingClient.java
- Create the JMS Connection Factory and Queue: The connection factory and the queue can be created using the admin console or from the command line. The admin console is quite easy, you just have to go to the Resources->JMS Resources->Connection Factories and Resources->JMS Resources->Destination Resources. From the command line you have to use the following two commands from the GLASSFIS_HOME/bin directory.
asadmin create-jms-resource --user admin --restype javax.jms.Queue --property imqDestinationName=testQueue jms/testQueue
asadmin create-jms-resource --user admin --restype javax.jms.ConnectionFactory --property imqDestinationName=connectionFactory jms/connectionFactory - Deploy the MDB: Since we created a Java Project, eclipse does not allow you to install from the IDE, so you have to export the Java jar file and use the admin console to deploy. Deploy it as an "EJB Module".
- Deploy the Client as a Web application
Thursday, December 21, 2006
Message Driven Bean in Java EE 5
In the past, I posted a few examples of implementing Messaging using J2EE and Spring. In this post, I will give an example of how to implement Message Driven beans using Java EE 5. I used Eclipse 3.2 and Glassfish for this example. Follow these steps to run the example:
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...
-
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...
-
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 ...
-
This is an example code for a simple PDF merge using iText 5. We use three InputStream s in a List as input and merged file is written to th...
-
The previous post described how to implement a JMS messaging client using Spring JMS . This post will describe how to implement the Message ...
-
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...
-
Update: A new post for validation in struts with annotation is available at: Struts 2 Validation: Annotations . Struts 2.0 relies on a val...
-
Displaytag is an opensource tag library that can be used to display tables on JSPs. Apart from being able to display tables, the displaytag...
-
In this post we will see a way to merge multiple PDF files while adding page numbers at the bottom of each page in the format Page 1 of 10 ....
Very useful example. Thanks for sharing.
ReplyDeleteOn another note, is there a way to annotate a servlet to listen for a queue over JMS? After looking over the servlet 2.5 nothing really jumped out at me.
For example a MDB is annotated as follows:
@MessageDriven(mappedName = "jms/myQueue", activationConfig =
{
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class NewMessage implements MessageListener
{
/** Creates a new instance of NewMessage */
public NewMessage ()
{
}
public void onMessage (Message message)
{
System.out.println ("myQueue Received");
}
}
Is there a similar annotation scheme for servlets or do we do it the old fashioned way be implementing a MessageListener?
Well, you will not find any such information the servlet specificaton, as it does not have anything to do with Messaging. I would rather not have a servlet do a dual job for messaging too. But if you don't have any other choice then you will have to go by the old way. I haven't tried it though.
ReplyDeleteWhy does all examples shows the "mappedName=.." usage and no one example of binding jndi name in server specific file (sun-web.xml)? Using "mappedName" is not a portable way, and you will not use it in a real application. Is anybody know where to look at for binding example of using jms in a servlet in dd file?
ReplyDeleteYou can see the new post http://java-x.blogspot.com/2007/03/message-driven-bean-in-java-ee-5-part-2.html for an example of how to use MDB with deployment descriptors.
ReplyDeleteNice Abhi.
ReplyDeletethis is good Example
and perfect analysis
Thanks
Anil Kumar Shukla
You are inconsistent in the way the steps are presented. In some steps you give excellent detail, yet in others your brevity is infuriating. I can understand if you want to assume some things.. but be consistent with this. If you want to give instructions on how to perform these things, read your own steps and follow them.. you'll find that step 6 onwards is too brief to be useful. yet step 1-4 is fine, it even says which project to select. Perhaps you just lost interest...
ReplyDeletein my project i found 1 error in
ReplyDeleteMessageProducer messageProducer= session.createroducer(queue);
plz helm me to solve this error...