首页 > 编程语言 > 详细

Spring整合ActiveMQ

时间:2020-04-01 21:17:04      阅读:56      评论:0      收藏:0      [点我收藏+]

 

1、引入依赖

pom.xml

 1     <!-- activemq -->
 2         <dependency>
 3             <groupId>org.springframework</groupId>
 4             <artifactId>spring-jms</artifactId>
 5             <version>4.3.23.RELEASE</version>
 6         </dependency>
 7 
 8         <dependency>
 9             <groupId>org.apache.activemq</groupId>
10             <artifactId>activemq-pool</artifactId>
11             <version>5.15.9</version>
12         </dependency>

 

2、配置属性文件

activemq.properties
activemq_url=tcp://192.168.0.102:61616
activemq_username=admin
activemq_password=admin

  

3、编写配置文件(需要把该配置加入Spring管理中,这里不再说明)

spring-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:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 8          ">
 9 
10     <context:annotation-config/>
11     <context:component-scan base-package="com.test"/>
12 
13     <!-- 读取配置文件 -->
14     <bean id="propertyPlaceholderConfigurer"
15           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
16         <property name="locations">
17             <array>
18                 <!-- activemq.properties所在文件路径-->
19                 <value>/WEB-INF/config/activemq.properties</value>
20             </array>
21         </property>
22     </bean>
23 
24     <!--配置生产者-->
25     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
26         <property name="connectionFactory">
27             <!--真正可以产生Connection的ConnectionFactory,由对应的jms服务厂商提供-->
28             <bean class="org.apache.activemq.ActiveMQConnectionFactory">
29                 <property name="brokerURL" value="${activemq_url}"/>
30                 <!--连接的账号密码,没有密码可以不写-->
31                 <property name="userName" value="${activemq_username}"/>
32                 <property name="password" value="${activemq_password}"/>
33             </bean>
34         </property>
35         <!--最大连接数-->
36         <property name="maxConnections" value="100"></property>
37     </bean>
38 
39     <!--这个是队列目的地,点对点的-->
40     <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
41         <!--queue是对应名字,这里根据自己的填写-->
42         <constructor-arg index="0" value="queue"/>
43     </bean>
44 
45     <!--Spring提供的JMS工具类,进行消息发送、接收-->
46     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
47         <property name="connectionFactory" ref="jmsFactory"/>
48         <property name="defaultDestination" ref="destinationQueue"/>
49         <property name="messageConverter">
50             <!--消息类型的转换-->
51             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
52         </property>
53     </bean>
54 
55 </beans>

 

3、队列生产者生产代码

package com.queue.activemq;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.jms.TextMessage;

public class Produce {

    private JmsTemplate jmsTemplate;

    public void send() {
        WebApplicationContext webctx = ContextLoader.getCurrentWebApplicationContext();
        this.jmsTemplate = (JmsTemplate) webctx.getBean("jmsTemplate");
        /*队列生产者*/
        jmsTemplate.send(session1 -> {
            TextMessage textMessage = session1.createTextMessage("发送的消息内容");
            return textMessage;
        });
    }


}

 

4、队列消费者接收代码

 1 package com.queue;
 2 
 3 import org.springframework.jms.core.JmsTemplate;
 4 import org.springframework.web.context.ContextLoader;
 5 import org.springframework.web.context.WebApplicationContext;
 6 
 7 /**
 8  * 消费者
 9  */
10 public class Comsumer {
11     private JmsTemplate jmsTemplate;
12 
13     public void revice(){
14         WebApplicationContext webctx = ContextLoader.getCurrentWebApplicationContext();
15         this.jmsTemplate = (JmsTemplate) webctx.getBean("jmsTemplate");
16         //队列消费者
17         String retValue=jmsTemplate.receiveAndConvert()+"";
18         System.out.println(retValue);
19     }
20 }

 

Spring整合ActiveMQ

原文:https://www.cnblogs.com/pxblog/p/12615773.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!