spring activemq 整合

创建maven项目

项目目录结构为

 

首先配置相关maven依赖

  <!-- 版本管理 -->
  <properties>
      <springframework>4.1.8.RELEASE</springframework>
  </properties>
  
  <dependencies>
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        
    
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework}</version>
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jms</artifactId>  
            <version>${springframework}</version>  
        </dependency> 
                
        <!-- activemq -->
        <dependency>  
            <groupId>org.apache.activemq</groupId>  
            <artifactId>activemq-pool</artifactId>  
            <version>5.12.1</version>  
        </dependency>   
        <dependency>  
            <groupId>org.apache.activemq</groupId>  
            <artifactId>activemq-core</artifactId>  
            <version>5.7.0</version>
        </dependency>
    
        <!-- log4j核心包-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        
      
  </dependencies>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
 
  <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:activemq/activemq.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
    <!-- springmvc前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
  
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- post乱码过虑器 --> 
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
      
      <context-param>   
        <param-name>log4jConfigLocation</param-name>   
        <param-value>classpath:/log4j/log4j.xml</param-value>   
    </context-param>   
    <listener>   
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>   
    </listener> 


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 activemq.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" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd">
 
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
    <property name="brokerURL" value="tcp://localhost:61616"/>  
    </bean>  

    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">  
        <property name="connectionFactory" ref="targetConnectionFactory"/>  
        <property name="maxConnections" value="10"/>  
    </bean>  
    
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
        <property name="targetConnectionFactory" ref="pooledConnectionFactory"/>  
    </bean>  

    <!-- Spring JmsTemplate 的消息生产者 start -->

    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>
    
    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
    </bean>

    <!--Spring JmsTemplate 的消息生产者 end -->


    <!-- 消息消费者 start -->
    
    <bean id="queueReceiverListener" class="com.sinosoft.activemq.consumer.QueueReceiverListener"/>
    <bean id="queueReceiverListener1" class="com.sinosoft.activemq.consumer.QueueReceiverListener1"/>
    
    <!-- 定义Queue监听器 -->
    <jms:listener-container destination-type="queue"
        container-type="default" connection-factory="connectionFactory"
        acknowledge="auto">
        <jms:listener destination="queueTest" ref="queueReceiverListener" />
        <jms:listener destination="queueTest" ref="queueReceiverListener1" />
    </jms:listener-container>

    <!-- 定义Topic监听器 -->
    
    <bean id="topicReceiverListener" class="com.sinosoft.activemq.consumer.TopicReceiverListener"/> 
    <bean id="topicReceiverListener1" class="com.sinosoft.activemq.consumer.TopicReceiverListener1"/>
    <jms:listener-container destination-type="topic"
        container-type="default" connection-factory="connectionFactory"
        acknowledge="auto">
        <jms:listener destination="topicTest" ref="topicReceiverListener" />
        <jms:listener destination="topicTest" ref="topicReceiverListener1" />
    </jms:listener-container>
    
    <!-- 消息消费者 end -->
</beans>

springmvc.xml 配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 com.controlle -->
    <context:component-scan base-package="com.sinosoft">
    </context:component-scan>

    <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置 mvc:annotation-driven默认加载很多的参数绑定方法 -->
    <mvc:annotation-driven />

    <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路径的前缀
        <property name="prefix" value="/jsp/"/> 
         -->
        <!-- 配置jsp路径的后缀
        <property name="suffix" value=".jsp" /> -->
    </bean>

</beans>

代码部分

QueueSenderProducer.java

作用:生产消息,发布 队列消息模式 消息

 1 @Component
 2 public class QueueSenderProducer {
 3 
 4     @Autowired
 5     @Qualifier("jmsQueueTemplate")
 6     private JmsTemplate jmsTemplate;
 7 
 8     /**
 9      * 发送消息
10      * 
11      * @param queueName
12      *            队列名称
13      * @param message
14      *            消息实体
15      * @throws Exception
16      */
17     public void send(String queueName, final String message) {
18         jmsTemplate.send(queueName, new MessageCreator() {
19 
20             public Message createMessage(Session session) throws JMSException {
21                 return session.createTextMessage(message);
22             }
23         });
24     }
25 
26 }

TopicSenderProducer.java与QueueSenderProducer.java 的代码结构相同,用于发布 发布/订阅模式 消息

 

QueueReceiverListener.java  

作用: 负责自动监听qctiveMQ中的队列消息,一旦有新消息后,将自动读取消息!

 

 1 public class QueueReceiverListener implements MessageListener {
 2  
 3      @Override
 4      public void onMessage(Message message) {  
 5          System.out.println("   -------------      接收消息       --------      ");  
 6             try {
 7                 System.out.println("队列         QueueReceiver   接收到消息:"+((TextMessage)message).getText());  
 8             } catch (JMSException e) {  
 9                 e.printStackTrace();  
10             }  
11         }  
12           
13 
14 }

QueueReceiverListener1, TopicReceiverListener,TopicReceiverListener1 与 QueueReceiverListener 的代码结构相同,作用相同,只是类名不同。

注 QueueReceiverListener,QueueReceiverListener1 用于接受消息对列模式消息,一对一,一条消息只能被消费一次

  TopicReceiverListener,TopicReceiverListener1  用于接受发布/订阅模式消息,一对多,一条消息可被多次消费

  俩者具体监听配置在activemq.xml中。

ActivemqController.java

作用:控制器

 1 @Controller
 2 @RequestMapping("mq")
 3 public class ActivemqController {
 4 
 5     @Autowired
 6     private QueueSenderProducer queueSenderProducer;
 7     @Autowired
 8     private TopicSenderProducer topicSenderProducer;
 9     
10     /**
11      * 
12      * @param message
13      * @return
14      */
15     @RequestMapping("queueSender")
16     public String queueSender(String message) {
17        18         try {
19             queueSenderProducer.send("queueTest", message);
20    21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24         return "success";
25     }
26     
27     /**
28      * 
29      * @param message
30      * @return 
31      */
32     @RequestMapping("topicSender")
33     public String TopicSender(String message) {
34      35         try {
36             topicSenderProducer.send("topicTest", message);
37         38         } catch (Exception e) {
39         
40         }
41         return "success";
42     }
43 
44 }

 

测试:

队列消息模式  访问地址

后台

 

 发布订阅模式 访问地址

后台

 

 

 

转载于:https://www.cnblogs.com/cmyxn/p/6438545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值