Spring整合ActiveMQ测试
第一部分:创建项目(使用maven)
--注意:使用IDEA创建maven普通项目还是聚合项目,都可以不用勾选,直接点next。
然后填入坐标和模块的名字
然后点击左上角的+号,选择web。
在新打开的页面下会显示web选项,这里的路径改为\src\main\webapp ,再修改web.xml文件的路径。
IDEA不会pom.xml文件默认生成jar文件,要在pom.xml添加<packaging>war</packaging>构建时生成war文件。
第二步:导入pom依赖
导包说明:
Spring核心包+AOP
common-logging
activemq核心包
spring整合jms包
jsp相关依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>org.abcdit.activemq</groupId> 7 <artifactId>activemq-demo4-spring</artifactId> 8 <packaging>war</packaging> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <dependencies> 13 <!-- ActiveMQ client客户端完整jar包依赖 --> 14 <dependency> 15 <groupId>org.apache.activemq</groupId> 16 <artifactId>activemq-all</artifactId> 17 <version>5.9.1</version> 18 </dependency> 19 <!-- activemq spring --> 20 <dependency> 21 <groupId>org.apache.activemq</groupId> 22 <artifactId>activemq-spring</artifactId> 23 <version>5.9.1</version> 24 </dependency> 25 <dependency> 26 <groupId>org.apache.activemq</groupId> 27 <artifactId>activemq-pool</artifactId> 28 <version>5.9.1</version> 29 </dependency> 30 31 <!-- Spring-JMS插件相关jar包依赖 --> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-jms</artifactId> 35 <version>4.3.16.RELEASE</version> 36 </dependency> 37 <!-- Spring框架上下文jar包依赖 --> 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-context</artifactId> 41 <version>4.3.16.RELEASE</version> 42 </dependency> 43 <!-- SpringMVC插件jar包依赖 --> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-webmvc</artifactId> 47 <version>4.3.16.RELEASE</version> 48 </dependency> 49 <!-- jsp相关 --> 50 <dependency> 51 <groupId>jstl</groupId> 52 <artifactId>jstl</artifactId> 53 <version>1.2</version> 54 </dependency> 55 <dependency> 56 <groupId>javax.servlet</groupId> 57 <artifactId>javax.servlet-api</artifactId> 58 <version>3.0.1</version> 59 <scope>provided</scope> 60 </dependency> 61 <dependency> 62 <groupId>javax.servlet.jsp</groupId> 63 <artifactId>javax.servlet.jsp-api</artifactId> 64 <version>2.2.1</version> 65 <scope>provided</scope> 66 </dependency> 67 68 <!--spring jms--> 69 <dependency> 70 <groupId>org.springframework</groupId> 71 <artifactId>spring-jms</artifactId> 72 <version>4.3.16.RELEASE</version> 73 </dependency> 74 75 </dependencies> 76 </project>
第二部分:spring整合springmvc
第一步:修改web.xml,配置springmvc核心控制器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 6 version="3.0"> 7 <!-- 配置编码过滤器,防止post请求乱码 --> 8 <filter> 9 <filter-name>characterEncodingFilter</filter-name> 10 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 11 <init-param> 12 <param-name>encoding</param-name> 13 <param-value>UTF-8</param-value> 14 </init-param> 15 </filter> 16 <filter-mapping> 17 <filter-name>characterEncodingFilter</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping> 20 21 <!--核心控制器--> 22 <servlet> 23 <servlet-name>dispatcherServlet</servlet-name> 24 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 25 26 <!--指定配置文件--> 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:spring-*.xml</param-value> 30 </init-param> 31 <load-on-startup>1</load-on-startup> 32 </servlet> 33 <servlet-mapping> 34 <servlet-name>dispatcherServlet</servlet-name> 35 <url-pattern>*.action</url-pattern> 36 </servlet-mapping> 37 </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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 7 8 <context:component-scan base-package="org.abcdit.acticemq"/> 9 <mvc:annotation-driven/> 10 </beans>
第三步:创建相关jsp页面
-订单页面order.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>Insert title here</title> 8 </head> 9 <body> 10 <form action="/save.action" method="post"> 11 用户编号:<input type="text" name="userid"><br> 12 订单金额:<input type="text" name="price"><br> 13 <input type="submit" value="提交"> 14 </form> 15 </body> 16 </html>
-成功页面success.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>Insert title here</title> 8 </head> 9 <body> 10 订单提交成功!!!请稍后去结算中心支付。。。 11 </body> 12 </html>
第四步:java代码实现
创建订单Order类
--注意事项
ActiveMQ处理对象时,对象必须实现序列化
--修改Order类,实现序列化接口
1 package org.abcdit.acticemq.pojo; 2 3 import java.io.Serializable; 4 5 public class Order implements Serializable { 6 7 private static final long serialVersionUID = -6760045770187998139L; 8 9 private Integer id; 10 11 private Integer userid; 12 13 private float price; 14 15 //补全get/set方法 16 }
创建OrderController类
1 package org.abcdit.acticemq.ordercontroller; 2 3 import org.abcdit.acticemq.pojo.Order; 4 import org.abcdit.acticemq.producer.OrderProducer; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 9 @Controller 10 public class OrderController { 11 12 @Autowired 13 private OrderProducer orderProducer; 14 15 @RequestMapping("/save.action") 16 public String save(Order order){ 17 18 System.out.println("当前订单提交的用户是:"+order.getUserid()+",订单金额:"+order.getPrice()); 19 20 orderProducer.sendToMQ(order); 21 22 return "/success.jsp"; 23 24 } 25 26 }
第三部分:Spring整合ActiveMQ
第一步:搭建ActiveMQ服务器。
第二步:创建消息生成者OrderProducer。
1 package org.abcdit.acticemq.producer; 2 3 import org.abcdit.acticemq.pojo.Order; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.jms.core.JmsTemplate; 6 import org.springframework.jms.core.MessageCreator; 7 import org.springframework.stereotype.Controller; 8 9 import javax.jms.JMSException; 10 import javax.jms.Message; 11 import javax.jms.ObjectMessage; 12 import javax.jms.Session; 13 14 @Controller 15 public class OrderProducer { 16 17 @Autowired 18 private JmsTemplate jmsTemplate; 19 20 //注意:内部类调用外部类属性,需要用final修饰 21 public void sendToMQ(final Order order){ 22 23 jmsTemplate.send("spring-test", new MessageCreator() { 24 @Override 25 public Message createMessage(Session session) throws JMSException { 26 27 //注意事项:需要被传到的对象,必须要继承序列接口 28 ObjectMessage objectMessage = session.createObjectMessage(); 29 return objectMessage; 30 } 31 }); 32 } 33 34 35 }
第三步:创建消息消费者OrderListener类
1 package org.abcdit.acticemq.listener; 2 3 import org.abcdit.acticemq.pojo.Order; 4 import org.springframework.stereotype.Component; 5 6 import javax.jms.JMSException; 7 import javax.jms.Message; 8 import javax.jms.MessageListener; 9 import javax.jms.ObjectMessage; 10 11 @Component 12 public class OrderListener implements MessageListener { 13 14 15 @Override 16 public void onMessage(Message message) { 17 18 try { 19 ObjectMessage objectMessage = (ObjectMessage) message; 20 Order order = (Order) objectMessage.getObject(); 21 22 System.out.println("消费者:"+order.getUserid()+",价格:"+order.getPrice()); 23 24 } catch (JMSException e) { 25 e.printStackTrace(); 26 } 27 28 } 29 }
第四步:spring整合ActiveMQ
-创建spring-jms.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" xmlns:jms="http://www.springframework.org/schema/jms" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> 5 6 <!--1.配置activemq连接工厂 7 使用连接池好处:链接只需要初始化一次,每次要使用的时候,直接从连接池获取,用完之后还给连接池。省去了每次创建、销毁连接的时间。 8 --> 9 <bean name="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> 10 <property name="connectionFactory"> 11 <!--配置连接池--> 12 <bean class="org.apache.activemq.ActiveMQConnectionFactory" > 13 <property name="userName" value="admin"/> 14 <property name="password" value="admin"/> 15 <property name="brokerURL" value="tcp://192.168.253.112:61616"/> 16 </bean> 17 </property> 18 <property name="maxConnections" value="30"/> 19 </bean> 20 21 <!--2.配置缓存工厂,提供对象并发效率 [可选,仅仅是缓存作用]--> 22 <bean name="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> 23 <!--指定1配置的工厂源--> 24 <property name="targetConnectionFactory" ref="pooledConnectionFactory"/> 25 <!-- 配置缓存session数 --> 26 <property name="sessionCacheSize" value="10"/> 27 </bean> 28 29 <!--3.获得操作对象,JMSTemplate--> 30 <bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 31 <!--指定连接工厂--> 32 <property name="connectionFactory" ref="cachingConnectionFactory"/> 33 34 </bean> 35 36 <!--4.配置消费者的监听器--> 37 <jms:listener-container acknowledge="auto" connection-factory="cachingConnectionFactory" response-destination-type="queue"> 38 <jms:listener destination="spring-test" ref="orderListener"/> 39 </jms:listener-container> 40 41 </beans>
第五步:修改web.xml文件,加载jms配置
1 <!-- 配置springmvc核心控制器 --> 2 <servlet> 3 <servlet-name>dispatcherServlet</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 6 <init-param> 7 <param-name>contextConfigLocation</param-name> 8 <!-- <param-value>classpath:springmvc.xml</param-value> --> 9 <param-value>classpath:spring*.xml</param-value> 10 </init-param> 11 12 <load-on-startup>1</load-on-startup> 13 </servlet> 14 <servlet-mapping> 15 <servlet-name>dispatcherServlet</servlet-name> 16 <url-pattern>*.action</url-pattern> 17 </servlet-mapping>
--注入OrderProducer,修改业务逻辑(已经注入)
1 @Controller 2 public class OrderController { 3 @Autowired 4 private OrderProducer producer; 5 6 @RequestMapping("/save.action") 7 public String save(Order order){ 8 9 // System.out.println("当前提交的订单用户是:"+order.getUserid()+",订单金额:"+order.getPrice()); 10 11 producer.sendToMQ(order); 12 13 return "/success.jsp"; 14 } 15 }
查看ActiveMQ控制台
Uncategorized exception occurred during JMS processing; nested exception is javax.jms.JMSException: Error while attempting to add new Connection to the pool; nested exception is javax.jms.JMSException: Could not connect to broker URL: tcp://192.168.253.112:61616. Reason: java.net.ConnectException: Connection timed out: connect
出现Could not connect to broker URL: tcp://192.168.253.112:61616报错,
1.在spring-jsm.xml文件配置activemq连接工厂时,配置连接池-brokerURL地址出错!
最后,整合成功!