首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

Message Driven Bean in Java EE 五

2012-10-27 
Message Driven Bean in Java EE 5In the past, I posted a few examples of implementing Messaging usin

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:

    Create a "Java project" in Eclipse.Add the Glassfish runtime library as a dependency for the project.Thefollowing is the code for the Message Driven Bean that I used for theExample. 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.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.java
Create the JMS Connection Factory and Queue:The connection factory and the queue can be created using the adminconsole or from the command line. The admin console is quite easy, youjust have to go to the Resources->JMS Resources->ConnectionFactories and Resources->JMS Resources->Destination Resources.From the command line you have to use the following two commands fromthe GLASSFIS_HOME/bin directory.
asadmin 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/connectionFactory
Deploy 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

热点排行