先看最后的目录结构:
mdb为EJB3项目;mdbTest为Web项目,add project:
上代码:
web项目中的servlet: mdb.java:
import java.io.IOException; import java.io.PrintWriter;
import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.TextMessage; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class mdb extends HttpServlet {
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>");
try { //获取QueueConnectionFactory对象 InitialContext context =new InitialContext(); QueueConnectionFactory factory =(QueueConnectionFactory)context.lookup("ConnectionFactory"); //创建QueueConnection QueueConnection connnection =factory.createQueueConnection(); //创建QueueSession QueueSession session =connnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); //获取Destination对象 Queue queue =(Queue)context.lookup("queue/myqueue"); //获取文本信息 TextMessage msg =session.createTextMessage(); //创建发布者 QueueSender sender =session.createSender(queue); for(int i=0;i<5;i++){ msg.setText("世界你好啊! "+i); //发送消息 sender.send(msg); } System.out.println("消息已发送"); } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
}
ejb项目中的Message Driven Bean: MyQueueMDBBean.java
package com.test.ejb;
import javax.ejb.EJBException; import javax.ejb.MessageDriven; import javax.ejb.MessageDrivenBean; import javax.ejb.MessageDrivenContext; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import javax.ejb.ActivationConfigProperty; @MessageDriven( activationConfig ={ @ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"), @ActivationConfigProperty(propertyName="destination",propertyValue="queue/myqueue") } ) public class MyQueueMDBBean implements MessageDrivenBean,MessageListener { private MessageDrivenContext mdc = null; public MyQueueMDBBean(){ System.out.println("初始化构造函数MDBean()"); } public void onMessage(Message msg) { TextMessage textMessage =(TextMessage)msg; try { System.out.println("MyQueueMDBBean被调用了【"+textMessage.getText()+"】"); } catch (JMSException e) { e.printStackTrace(); } } public void ejbCreate() { System.out.println("MDBean.ejbCreate()"); }
public void ejbRemove() throws EJBException { //A container invokes this method before it ends the life of the message-driven object. System.out.println("MDBean.remove()"); }
public void setMessageDrivenContext(MessageDrivenContext mdc)throws EJBException { //Set the associated message-driven context. System.out.println("MDBean.setMessageDrivenContext()"); this.mdc=mdc; } }
在deploy目录下的mail-service.xml下加上代码:
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.org.destination:server=Queue,name=myqueue" > <attribute name="JNDIName" >queue/myqueue</attribute> <depends optional-attribute-name = "DestinationManager" > jboss.mq:service=DestinationManager </depends> </mbean>
现在部署此ejb。
并新建一个空目录:queue
通过jboss在queue中生成文件:myqueue-service.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <server> <mbean xmbean-dd="xmdesc/Queue-xmbean.xml" name="jboss.messaging.destination:service=Queue,name=myQueue" code="org.jboss.jms.server.destination.QueueService"> <attribute name="SecurityConfig"> <security> <role create="true" name="guest2" read="true" write="true"/> </security> </attribute> <attribute name="PageSize">2000</attribute> <attribute name="ServerPeer">jboss.messaging:service=ServerPeer</attribute> <attribute name="FullSize">200000</attribute> <attribute name="JNDIName">queue/myQueue</attribute> <attribute name="RedeliveryDelay">-1</attribute> <attribute name="DownCacheSize">2000</attribute> <attribute name="MaxSize">-1</attribute> <attribute name="MaxDeliveryAttempts">-1</attribute> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean> </server>
最后jboss配置一下:
部署测试:http://localhost:8080/mdbTest/mdb
在jboss命令行输出:(初次运行结果)
21:53:49,847 INFO [STDOUT] MyQueueMDBBean被调用了【世界你好啊! 0】
21:53:49,851 INFO [STDOUT] 初始化构造函数MDBean()
21:53:49,852 INFO [STDOUT] 消息已发送
21:53:49,858 INFO [STDOUT] MyQueueMDBBean被调用了【世界你好啊! 1】
21:53:49,864 INFO [STDOUT] MyQueueMDBBean被调用了【世界你好啊! 2】
21:53:49,866 INFO [STDOUT] MyQueueMDBBean被调用了【世界你好啊! 3】
21:53:49,868 INFO [STDOUT] MyQueueMDBBean被调用了【世界你好啊! 4】
本人亲测,就是上面的效果。当然,若版本不致的话,可能出现问题。