spring整合activeMQ
Posted 光何
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合activeMQ相关的知识,希望对你有一定的参考价值。
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>io.guangsoft</groupId>
6 <artifactId>amq</artifactId>
7 <version>0.1</version>
8 <packaging>war</packaging>
9 <properties>
10 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11 <spring.version>4.1.7.RELEASE</spring.version>
12 <jackson.version>2.5.0</jackson.version>
13 </properties>
14 <dependencies>
15 <dependency>
16 <groupId>org.springframework</groupId>
17 <artifactId>spring-core</artifactId>
18 <version>${spring.version}</version>
19 </dependency>
20 <dependency>
21 <groupId>org.apache.commons</groupId>
22 <artifactId>commons-lang3</artifactId>
23 <version>3.4</version>
24 </dependency>
25 <dependency>
26 <groupId>org.springframework</groupId>
27 <artifactId>spring-context</artifactId>
28 <version>${spring.version}</version>
29 </dependency>
30 <dependency>
31 <groupId>org.springframework</groupId>
32 <artifactId>spring-aop</artifactId>
33 <version>${spring.version}</version>
34 </dependency>
35 <dependency>
36 <groupId>org.springframework</groupId>
37 <artifactId>spring-web</artifactId>
38 <version>${spring.version}</version>
39 </dependency>
40 <dependency>
41 <groupId>org.springframework</groupId>
42 <artifactId>spring-webmvc</artifactId>
43 <version>${spring.version}</version>
44 </dependency>
45 <dependency>
46 <groupId>org.springframework</groupId>
47 <artifactId>spring-context-support</artifactId>
48 <version>${spring.version}</version>
49 </dependency>
50 <dependency>
51 <groupId>org.springframework</groupId>
52 <artifactId>spring-test</artifactId>
53 <version>${spring.version}</version>
54 <scope>test</scope>
55 </dependency>
56 <dependency>
57 <groupId>org.springframework</groupId>
58 <artifactId>spring-tx</artifactId>
59 <version>${spring.version}</version>
60 </dependency>
61 <dependency>
62 <groupId>org.springframework</groupId>
63 <artifactId>spring-expression</artifactId>
64 <version>${spring.version}</version>
65 </dependency>
66 <dependency>
67 <groupId>org.apache.commons</groupId>
68 <artifactId>commons-pool2</artifactId>
69 <version>2.4.2</version>
70 </dependency>
71 <dependency>
72 <groupId>javax.servlet</groupId>
73 <artifactId>javax.servlet-api</artifactId>
74 <version>3.1.0</version>
75 </dependency>
76 <dependency>
77 <groupId>org.apache.activemq</groupId>
78 <artifactId>activemq-all</artifactId>
79 <version>5.14.3</version>
80 </dependency>
81 <dependency>
82 <groupId>dom4j</groupId>
83 <artifactId>dom4j</artifactId>
84 <version>1.6.1</version>
85 </dependency>
86 <dependency>
87 <groupId>commons-io</groupId>
88 <artifactId>commons-io</artifactId>
89 <version>2.4</version>
90 </dependency>
91 <dependency>
92 <groupId>com.fasterxml.jackson.core</groupId>
93 <artifactId>jackson-core</artifactId>
94 <version>${jackson.version}</version>
95 </dependency>
96 <dependency>
97 <groupId>com.fasterxml.jackson.core</groupId>
98 <artifactId>jackson-databind</artifactId>
99 <version>${jackson.version}</version>
100 </dependency>
101 <dependency>
102 <groupId>javax.servlet</groupId>
103 <artifactId>jstl</artifactId>
104 <version>1.2</version>
105 </dependency>
106 <dependency>
107 <groupId>jaxen</groupId>
108 <artifactId>jaxen</artifactId>
109 <version>1.1.6</version>
110 </dependency>
111 <dependency>
112 <groupId>org.slf4j</groupId>
113 <artifactId>slf4j-log4j12</artifactId>
114 <version>1.5.8</version>
115 </dependency>
116 <!-- swagger -->
117 <dependency>
118 <groupId>io.springfox</groupId>
119 <artifactId>springfox-swagger2</artifactId>
120 <version>2.6.1</version>
121 </dependency>
122 <dependency>
123 <groupId>io.springfox</groupId>
124 <artifactId>springfox-swagger-ui</artifactId>
125 <version>2.6.1</version>
126 </dependency>
127 </dependencies>
128 <build>
129 <plugins>
130 <plugin>
131 <groupId>org.apache.maven.plugins</groupId>
132 <artifactId>maven-compiler-plugin</artifactId>
133 <configuration>
134 <source>1.8</source>
135 <target>1.8</target>
136 </configuration>
137 </plugin>
138 </plugins>
139 </build>
140 </project>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5 id="WebApp_ID" version="3.0">
6 <display-name>activeMQSpring</display-name>
7 <!-- contextConfigLocation 参数定义了要装入的 Spring 配置文件。 -->
8 <context-param>
9 <param-name>contextConfigLocation</param-name>
10 <param-value>
11 classpath:activemq.xml
12 </param-value>
13 </context-param>
14 <listener>
15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16 </listener>
17 <servlet>
18 <servlet-name>springMVC</servlet-name>
19 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
20 <init-param>
21 <param-name>contextConfigLocation</param-name>
22 <param-value>classpath:spring-mvc.xml</param-value>
23 </init-param>
24 <load-on-startup>1</load-on-startup>
25 </servlet>
26 <servlet-mapping>
27 <servlet-name>springMVC</servlet-name>
28 <url-pattern>/</url-pattern>
29 </servlet-mapping>
30 <filter>
31 <filter-name>characterEncodingFilter</filter-name>
32 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33 <init-param>
34 <param-name>encoding</param-name>
35 <param-value>UTF-8</param-value>
36 </init-param>
37 <init-param>
38 <param-name>forceRequestEncoding</param-name>
39 <param-value>true</param-value>
40 </init-param>
41 <init-param>
42 <param-name>forceResponseEncoding</param-name>
43 <param-value>true</param-value>
44 </init-param>
45 </filter>
46 <filter-mapping>
47 <filter-name>characterEncodingFilter</filter-name>
48 <url-pattern>/*</url-pattern>
49 </filter-mapping>
50 </web-app>
spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context-4.1.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
12
13 <!-- 将base-package 目录下java文件中的所有@Component @[email protected]等这些注解的类,则把这些类注册为bean -->
14 <!-- <context:component-scan
15 base-package="io.guangsoft.amq.controller" /> -->
16 <!-- 自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter
17 两个bean,是spring MVC为@Controllers分发请求所必须的。 -->
18 <context:component-scan base-package="io.guangsoft.amq" />
19 <mvc:annotation-driven />
20 <bean id="viewResolver"
21 class="org.springframework.web.servlet.view.UrlBasedViewResolver">
22 <property name="viewClass"
23 value="org.springframework.web.servlet.view.JstlView" />
24 <property name="prefix" value="/WEB-INF/views/" />
25 <property name="suffix" value=".jsp" />
26 </bean>
27 </beans>
activemq.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:amq="http://activemq.apache.org/schema/core"
5 xmlns:jms="http://www.springframework.org/schema/jms"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xmlns:mvc="http://www.springframework.org/schema/mvc"
8 xsi:schemaLocation="
9 http://www.springframework.org/schema/beans
10 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
11 http://www.springframework.org/schema/context
12 http://www.springframework.org/schema/context/spring-context-4.1.xsd
13 http://www.springframework.org/schema/mvc
14 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
15 http://www.springframework.org/schema/jms
16 http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
17 http://activemq.apache.org/schema/core
18 http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd">
19 <amq:connectionFactory id="amqConnectionFactory"
20 brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin" />
21 <!-- 配置JMS连接工长 -->
22 <bean id="connectionFactory"
23 class="org.springframework.jms.connection.CachingConnectionFactory">
24 <constructor-arg ref="amqConnectionFactory" />
25 <property name="sessionCacheSize" value="100" />
26 </bean>
27 <!-- 定义消息队列(Queue) -->
28 <bean id="queueDestination"
29 class="org.apache.activemq.command.ActiveMQQueue">
30 <!-- 设置消息队列的名字 -->
31 <constructor-arg>
32 <value>test</value>
33 </constructor-arg>
34 </bean>
35 <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
36 <bean id="jmsTemplate"
37 class="org.springframework.jms.core.JmsTemplate">
38 <property name="connectionFactory" ref="connectionFactory" />
39 <property name="defaultDestination"
40 ref="queueDestination" />
41 <property name="receiveTimeout" value="10000" />
42 <!-- true是topic,false是queue,默认是false,此处显示写出false -->
43 <property name="pubSubDomain" value="false" />
44 </bean>
45 <bean id="producerService"
46 class="io.guangsoft.amq.producer.ProducerService">
47 </bean>
48 <bean id="consumerService"
49 class="io.guangsoft.amq.consumer.ConsumerService">
50 </bean>
51 </beans>
ProducerService.java
1 package io.guangsoft.amq.service;
2 import javax.annotation.Resource;
3 import javax.jms.Destination;
4 import javax.jms.JMSException;
5 import javax.jms.Message;
6 import javax.jms.Session;
7 import org.springframework.jms.core.JmsTemplate;
8 import org.springframework.jms.core.MessageCreator;
9 import org.springframework.stereotype.Service;
10 @Service
11 public class ProducerService {
12 @Resource(name = "jmsTemplate")
13 private JmsTemplate jmsTemplate;
14 public void sendMessage(Destination destination, final String msg) {
15 System.out.println("Send " + msg + " to Destination " + destination.toString());
16 MessageCreator messageCreator = new MessageCreator() {
17 public Message createMessage(Session session) throws JMSException {
18 return session.createTextMessage(msg);
19 }
20 };
21 jmsTemplate.send(destination, messageCreator);
22 }
23 public void sendMessage(final String msg) {
24 String destination = jmsTemplate.getDefaultDestinationName().toString();
25 System.out.println("Send " + msg + " to Destination " + destination);
26 MessageCreator messageCreator = new MessageCreator() {
27 public Message createMessage(Session session) throws JMSException {
28 return session.createTextMessage(msg);
29 }
30 };
31 jmsTemplate.send(messageCreator);
32 }
33 }
ConsumerService.java
1 package io.guangsoft.amq.service;
2 import javax.annotation.Resource;
3 import javax.jms.Destination;
4 import javax.jms.JMSException;
5 import javax.jms.TextMessage;
6 import org.springframework.jms.core.JmsTemplate;
7 import org.springframework.stereotype.Service;
8 @Service
9 public class ConsumerService {
10 @Resource(name = "jmsTemplate")
11 private JmsTemplate jmsTemplate;
12 public TextMessage receive(Destination destination) {
13 TextMessage tm = (TextMessage) jmsTemplate.receive(destination);
14 if (tm != null) {
15 try {
16 System.out.println("Get Message: " + tm.getText() + " from Destination " + destination.toString());
17 } catch (JMSException e) {
18 e.printStackTrace();
19 }
20 }
21 return tm;
22 }
23 }
amqController.java
1 package io.guangsoft.amq.controller;
2 import java.io.IOException;
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5 import javax.annotation.Resource;
6 import javax.jms.Destination;
7 import javax.jms.JMSException;
8 import javax.jms.TextMessage;
9 import javax.management.MBeanServerConnection;
10 import javax.management.remote.JMXConnector;
11 import javax.management.remote.JMXConnectorFactory;
12 import javax.management.remote.JMXServiceURL;
13 import org.springframework.stereotype.Controller;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RequestMethod;
16 import org.springframework.web.bind.annotation.RequestParam;
17 import org.springframework.web.servlet.ModelAndView;
18
19 import io.guangsoft.amq.service.ConsumerService;
20 import io.guangsoft.amq.service.ProducerService;
21 @Controller
22 public class amqController {
23 @Resource(name = "queueDestination")
24 private Destination queueDestination;
25 @Resource(name = "producerService")
26 private ProducerService producer;
27 @Resource(name = "consumerService")
28 private ConsumerService consumer;
29
30 @RequestMapping(value="/index",method=RequestMethod.GET)
31 public ModelAndView welcome(){
32 ModelAndView mv = new ModelAndView();
33 mv.setViewName("index");
34 return mv;
35 }
36
37 @RequestMapping(value = "/producer", method = RequestMethod.GET)
38 public ModelAndView producer() {
39 Date now = new Date();
40 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
41 String time = sdf.format(now);
42 System.out.println(time);
43 ModelAndView mv = new ModelAndView();
44 mv.addObject("time", time);
45 mv.setViewName("producer");
46 return mv;
47 }
48 @RequestMapping(value = "/onsend", method = RequestMethod.POST)
49 public ModelAndView producer(@RequestParam("message") String message) {
50 ModelAndView mv = new ModelAndView();
51 producer.sendMessage(queueDestination, message);
52 mv.setViewName("index");
53 return mv;
54 }
55 @RequestMapping(value = "/receive", method = RequestMethod.GET)
56 public ModelAndView queue_receive() throws JMSException {
57 ModelAndView mv = new ModelAndView();
58 TextMessage tm = consumer.receive(queueDestination);
59 if (tm == null) {
60 mv.addObject("textMessage", "Queue is Empty");
61 } else {
62 mv.addObject("textMessage", tm.getText());
63 }
64 mv.setViewName("consumer");
65 return mv;
66 }
67 public ModelAndView jmsManager() throws IOException {
68 ModelAndView mv = new ModelAndView();
69 mv.setViewName("index");
70 JMXServiceURL url = new JMXServiceURL("");
71 JMXConnector connector = JMXConnectorFactory.connect(url);
72 connector.connect();
73 MBeanServerConnection connection = connector.getMBeanServerConnection();
74 return mv;
75 }
76 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>Welcome</title>
8 </head>
9 <body>
10
11 <h1>Welcome!!!</h1>
12 <h2><a href="producer">Send Messages</a></h2>
13 <h2><a href="receive">Get Message From Queue</a></h2>
14 </body>
15 </html>
producer.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>JMS-Producer</title>
8 </head>
9 <body>
10 <h1>JMS-Producer!!!</h1>
11 <form action="onsend" method="post">
12 MessageText:<textarea name="message">${time}</textarea>
13 <input type="submit" value="Submit">
14 </form>
15 <h2><a href="index">RETURN HOME</a></h2>
16 </body>
17 </html>
consumer.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>JMS-Reciever</title>
8 </head>
9 <body>
10 <h1>JMS-Recever!!!</h1>
11 <h2>${textMessage}</h2>
12 <h2><a href="index">RETURN HOME</a></h2>
13 </body>
14 </html>
以上是关于spring整合activeMQ的主要内容,如果未能解决你的问题,请参考以下文章
activemq 学习系列 activemq 与 spring boot 整合