activeMq的配置与使用

依赖

pom依赖

      <!-- active-mq -->
      <dependency>
          <groupId>org.apache.activemq</groupId>
          <artifactId>activemq-core</artifactId>
          <version>5.7.0</version>
      </dependency>
      <dependency>
          <groupId>org.apache.activemq</groupId>
          <artifactId>activemq-pool</artifactId>
          <version>5.11.1</version>
      </dependency>
      <!-- spring 对消息队列的支持  -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jms</artifactId>
          <version>4.3.7.RELEASE</version>
      </dependency>

配置文件

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/jms
      http://www.springframework.org/schema/jms/spring-jms-4.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
      ">

    <!-- activeMq  v2.0 2020-05-15  将amq配置文件引入主配置文件中 -->
	<import resource="classpath:applicationContextActiveMq.xml"/>
</beans>

applicationContextActiveMq.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:context="http://www.springframework.org/schema/context" 
       xmlns:amq="http://activemq.apache.org/schema/core"
       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://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

	<context:property-placeholder location="classpath:application.properties" />
	
	<!--1.ActiveMQ 真正产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 也可以用amq:内置标签来定义-->
    <!--<amq:connectionFactory id="amqConnectionFactory"
    		brokerURL="tcp://192.168.1.1:61616"
    		userName="admin"
    		password="admin">
    	</amq:connectionFactory>-->
    <bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    	<property name="brokerURL" value="${activemq_default_broker_url}"/>
        <property name="userName" value="${activemq_default_user}" />
        <property name="password" value="${activemq_default_password}" />
    </bean>
    
    <!--2.配置spring 管理 connectionFactory类-->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="activeMqConnectionFactory" />
        <property name="sessionCacheSize" value="100" />
    </bean>
    
    <!--3.配置消息队列生产者与消费者以及监听者-->
    <!-- 定义消息队列(Queue) -->
    <bean id="BeiJingDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字 -->
        <constructor-arg>
            <value>BeiJingQueue</value>
        </constructor-arg>
    </bean>
    <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="BeiJingDestination" />
        <property name="receiveTimeout" value="10000" />
        <!-- true是topic,false是queue,默认是false,此处显示写出false -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 配置消息队列监听者(Queue) -->
    <bean id="queueMessageListener" class="com.dd.activeMQ.Queue.listener.QueueMessageListenerDemo" />
    
    <!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
    <bean id="queueListenerContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="BeiJingDestination" />
        <property name="messageListener" ref="queueMessageListener" />
    </bean>
    
    <!--4.配置主题消息生产者以及监听者-->
    <!--这个是主题(topic)目的地,一对多的 -->
    <bean id="myTopicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="MyTopic01" />
    </bean>

    <!-- 配置JMS模板(Topic),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="myTopicDestination" />
        <property name="receiveTimeout" value="10000" />
        <!-- true是topic,false是queue,默认是false,此处显示写出false -->
        <property name="pubSubDomain" value="true" />
        <!-- 消息不持久化 -->
        <property name="explicitQosEnabled" value="false"></property>
    </bean>

    <!-- 消息监听类 -->
    <bean id="topicMessageListener1" class="com.dd.activeMQ.Topic.listener.TopicMessageListenerDemo"/>
    <!--目前还没找到DefaultMessageListenerContainer配置多个监听的代码-->
    <!--<bean id="topicMessageListener2" class="com.zy.mq.service.TopicMessageListener2"/>-->

    <!-- 消息监听器 -->
    <bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="destination" ref="myTopicDestination"></property>
        <property name="messageListener" ref="topicMessageListener1"></property>
    </bean> 
</beans>

代码实现类

队列监听者

在配置文件中配置监听bean时,可以看到

package com.**.activeMQ.Queue.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
* @author:	yao_x_x
* @since:	JDK 1.8.0_91
* @Description:
*
*/
public class QueueMessageListenerDemo implements MessageListener{

	@Override
	public void onMessage(Message message) {
		TextMessage tm = (TextMessage) message;
        try {
            System.err.println("QueueMessageListener监听到了文本消息:\t"
                    + tm.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
	}		 
}

队列生产者

package com.**.activeMQ.Queue.producer;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.Message;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

/**

* @author:	yao_x_x
* @since:	JDK 1.8.0_91
* @Description:
*
*/
@Service
public class QueueProducerService {
	 	@Resource(name="jmsQueueTemplate")//配置文件里的模板
	    private JmsTemplate jmsTemplate;
	    
	    public void sendMessage(Destination destination, final String msg){
	        System.out.println(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息---------------------->"+msg);
	        jmsTemplate.send(destination, new MessageCreator() {
	            public TextMessage createMessage(Session session) throws JMSException {
	                return session.createTextMessage(msg);
	            }
	        });
	    }
}

队列消费者

package com.**.activeMQ.Queue.comsumer;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

/**

* @author:	yao_x_x
* @since:	JDK 1.8.0_91
* @Description:
*
*/
@Service
public class QueueComsuerService {
	
	 	@Resource(name="jmsQueueTemplate")
	    private JmsTemplate jmsTemplate;

	    public TextMessage receive(Destination destination){
	        TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
	        try{
	            System.err.println("从队列" + destination.toString() + "收到了消息:\t"
	                    + textMessage.getText());
	        } catch (JMSException e) {
	            e.printStackTrace();
	        }
	        return textMessage;
	    }
}

controller中使用

    @Autowired
    private QueueProducerService mqProducer;
    
    @Resource(name="BeiJingDestination")
    Destination destination;
    
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
		mqProducer.sendMessage(destination, "喂喂喂,喂喂喂");
		return "succ";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值