基于Spring的JMS,ActiveMQ实现部署到Tomcat,下面是详细的代码,不解释。
applicationContext-jms.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 创建连接工厂 --> <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jms/ConnectionFactory" /> </bean> <!-- 声明消息目标 --> <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jms/Queue" /> </bean> <!-- 创建JMS模板 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsConnectionFactory" /> <property name="defaultDestination" ref="destination" /> </bean> <!-- 消息发布者 --> <bean id="sender" class="message.Sender"> <property name="jmsTemplate" ref="jmsTemplate" /> </bean> <!-- 消息订阅者 --> <bean id="receive" class="message.Receiver" /> <!-- listener container,MDP无需实现接口 --> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory" /> <property name="destination" ref="destination" /> <property name="messageListener" ref="receive" /> </bean> </beans>
使用到了jndi
webapp/META-INF/context.xml
<Context antiJARLocking="true"> <Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="vm://localhost" brokerName="LocalActiveMQBroker" /> <Resource name="jms/Queue" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" description="JMS Queue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="FOO.BAR" /> </Context>
webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>jms</display-name> <!-- 配置启动JMS服务 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>SendMessage</servlet-name> <servlet-class>action.SendMessage</servlet-class> </servlet> <servlet-mapping> <servlet-name>SendMessage</servlet-name> <url-pattern>/servlet/SendMessage</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>
action/SendMessage.java
package action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import message.Sender; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class SendMessage extends HttpServlet { /** * */ private static final long serialVersionUID = -8037168825230466473L; /** * Constructor of the object. */ public SendMessage() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * 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>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ApplicationContext xt = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); Sender sender = (Sender) xt.getBean("sender"); String Text = request.getParameter("text"); sender.send(Text); response.encodeRedirectURL("/index.jsp"); System.out.println("测试JMS"); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
message/Receiver.java
package message; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class Receiver implements MessageListener { public void onMessage(Message message) { if (message instanceof TextMessage) { TextMessage text = (TextMessage) message; try { System.out.println("Receive:" + text.getText()); } catch (JMSException e) { e.printStackTrace(); } } } }
message/Sender.java
package message; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class Sender { private JmsTemplate jmsTemplate; public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void send(final String text) { System.out.println("---Send:" + text); jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(text); } }); } }