Message Driven Bean in Java EE 5
In the past, I posted a few examples of implementing Messaging usingJ2EE and Spring. In this post, I will give an example of how toimplement Message Driven beans using Java EE 5. I used Eclipse 3.2 andGlassfish for this example. Follow these steps to run the example: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:
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<?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.xmlThis 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.javaasadmin create-jms-resource --user admin --restype javax.jms.Queue --property imqDestinationName=testQueue jms/testQueueasadmin create-jms-resource --user admin --restype javax.jms.ConnectionFactory --property imqDestinationName=connectionFactory jms/connectionFactoryDeploy the MDB:Since we created a Java Project, eclipse does not allow you to installfrom the IDE, so you have to export the Java jar file and use the adminconsole to deploy. Deploy it as an "EJB Module".Deploy the Client as a Web application