1:在web.xml文件中配置一个spring用的上下文:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/jmsconfig.xml </param-value> </context-param>
jmsconfig.xml用来装配jms,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/Ntelagent/RequestQCF</value> </property> </bean> <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/Ntelagent/RequestQ</value> </property> </bean> <bean id="jmsConnectionFactoryForReceive" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/Ntelagent/ResponseQCF</value> </property> </bean> <bean id="destinationForReceive" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/Ntelagent/ResponseQ</value> </property> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102"> <property name="connectionFactory"> <ref bean="jmsConnectionFactory"/> </property> <property name="defaultDestination"> <ref bean="destination"/> </property> <property name="messageConverter"> <ref bean="jmsTrxConverter"/> </property> <property name="receiveTimeout"> <value>1</value> </property> </bean> <bean id="jmsTemplateForReceive" class="org.springframework.jms.core.JmsTemplate102"> <property name="connectionFactory"> <ref bean="jmsConnectionFactoryForReceive"/> </property> <property name="defaultDestination"> <ref bean="destinationForReceive"/> </property> <property name="messageConverter"> <ref bean="jmsTrxConverter"/> </property> <property name="receiveTimeout"> <value>1</value> </property> </bean> <bean id="jmsTrxConverter" class="co.transport.jms.JmsTransactionConverter"> <property name="rspQueue"> <ref bean="destinationForReceive"/> </property> </bean> <bean id="jmsRequestTransport" class="co.transport.jms.JmsRequestTransport"> <property name="jmsTemplate"> <ref bean="jmsTemplate"/> </property> <property name="jmsTemplateForReceive"> <ref bean="jmsTemplateForReceive"/> </property> </bean> </beans>
其中:
jmsConnectionFactory为jms连接工厂,属性jndiName的value为server服务配置的jndi名称.
destination为消息队列,属性jndiName为消息队列的jndi名称.
jmsTemplate为配置spring消息模版:采用JmsTemplate102(如果你的JMS实现符合JMS规范1.1版,则应该使用JmsTemplate),其中的messageConverter属性配置了一个消息转换器,因为通常消息在发送前和接收后都需要进行一个前置和后置处理,转换器便进行这个工作。
由于我的sendQueue很receiveRueue是不同的queue,所以我配置了两个jmsTemplate:
jmsTemplateForReceive负责接收消息,jmsTemplate负责发发送消息.
发送消息的代码很简单:
jmsTemplate.convertAndSend(request);
接收消息也很简单:
Object obj = jmsTemplate.receiveAndConvert();
如果需要用一个过滤器接收特定的消息,则:
Object obj = this.jmsTemplateForReceive.receiveSelectedAndConvert(this.messageSelector);
发送和接收消息的class如下:
public class JmsRequestTransport implements RequestTransport { private JmsTemplate jmsTemplate; private JmsTemplate jmsTemplateForReceive; private String messageSelector; private String destinationName; public void setJmsTemplate(JmsTemplate template){ this.jmsTemplate = template; } public void request(Object request) { jmsTemplate.convertAndSend(request); } public Object receive() { System.out.println("in JmsRequestTransport: destinationName = " + this.jmsTemplateForReceive.getDefaultDestinationName()); System.out.println("in JmsRequestTransport: messageSelector = " + messageSelector); Object obj = this.jmsTemplateForReceive.receiveSelectedAndConvert(this.messageSelector); return obj; } public String getMessageSelector() { return messageSelector; } public void setMessageSelector(String string) { messageSelector = string; } public String getDestinationName() { return destinationName; } public void setDestinationName(String string) { destinationName = string; } public JmsTemplate getJmsTemplateForReceive() { return jmsTemplateForReceive; } public void setJmsTemplateForReceive(JmsTemplate template) { jmsTemplateForReceive = template; } }
要实现一个消息转换器,只需要实现MessageConverter接口,MessageConverter很简单,它只有两个方法需要实现:
public Object fromMessage(Message msg){}
public Message toMessage(Object obj, Session session) throws JMSException{}
fromMessage为接收消息后,对消息进行的转换(通常是把一个message转化为一个Object对象)
toMessage为发送消息前需要的转化(通常为把一个Object转化为一个message对象)
我的JmsTransactionConverter转换器如下:
public class JmsTransactionConverter implements MessageConverter { private Queue rspQueue; public JmsTransactionConverter(){} public Object fromMessage(Message msg){ MessageBean msgBean = new MessageBean(); TextMessage massage = (TextMessage)msg; try { String str = massage.getText(); msgBean.setHead("HeadTest"); msgBean.setOutput(str); msgBean.setStatus("success"); } catch (JMSException e) { // TODO Auto-generated catch block msgBean = null; e.printStackTrace(); } finally { return msgBean; } } public Message toMessage(Object obj, Session session) throws JMSException{ String s = (String)obj; TextMessage message = session.createTextMessage(s); message.setStringProperty("mytype","java"); return message; } public Queue getRspQueue() { return rspQueue; } public void setRspQueue(Queue queue) { rspQueue = queue; } } 其中:MessageBean是一个简单javabean: public class MessageBean { private String head; private String output; private String status; public String getHead() { return head; } public String getOutput() { return output; } public String getStatus() { return status; } public void setHead(String string) { head = string; } public void setOutput(String string) { output = string; } public void setStatus(String string) { status = string; } } 然后就可以用两个servlet来测试发送和接收消息: 发送消息: public class JMSTestServlet extends HttpServlet { /** * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ ServletOutputStream out = resp.getOutputStream(); try { ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); JmsRequestTransport transport = (JmsRequestTransport)wac.getBean("jmsRequestTransport"); System.out.println("in JMSTestServlet.doGet:begin send message"); String messageToSend = req.getParameter("message"); if(messageToSend == null) { messageToSend = "Default Message"; } transport.request(messageToSend); String s = "in JMSTestServlet.doGet:after send message and message is: " + messageToSend; out.println("<HTML><BODY>"); out.println(s); out.println("<br><input type=button name=back value=back onclick=history.back()>"); out.println("<br><a href=receiveServlet>receive</a>"); out.println("</HTML></BODY>"); } catch(Exception e) { out.println("<HTML><BODY>"); out.println(e.toString()); out.println("</HTML></BODY>"); } } 接收消息: public class receiveServlet extends HttpServlet { /** * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletOutputStream out = resp.getOutputStream(); String selector = req.getParameter("selector"); String distination = req.getParameter("distination"); try { String s = "nothing"; ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); JmsRequestTransport transport = (JmsRequestTransport)wac.getBean("jmsRequestTransport"); System.out.println("selector = " + selector); System.out.println("distination = " + distination); transport.setDestinationName(distination); transport.setMessageSelector(selector); Object obj = transport.receive(); if(obj != null) { MessageBean msgBean = (MessageBean)obj; //System.out.println("in receiveServlet:"); s = "receive the message is: " + msgBean.getHead() + "," + msgBean.getOutput() + ", " + msgBean.getStatus(); } else { s = "there is no message where " + selector; } out.println("<HTML><BODY>"); out.println(s); out.println("<br>selector is :" + selector); out.println("<br>distination is :" + distination); out.println("<br><a href=jsp/sendMessage.jsp>sendMessage</a>"); out.println("</HTML></BODY>"); } catch(Exception e) { out.println("<HTML><BODY>"); out.println("<br>selector is :" + selector); out.println("<br>distination is :" + distination); out.println("<br>" + e.toString()); out.println("</HTML></BODY>"); } } }