SpringMVC集成ActiveMQ

本文介绍了如何将SpringMVC应用与ActiveMQ集成,包括在pom文件中添加依赖,配置spring-activemq.xml文件,设置消息监听器以及创建消息生产者的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、pom文件配置

      <!-- xbean-spring -->
      <dependency>
        <groupId>org.apache.xbean</groupId>
        <artifactId>xbean-spring</artifactId>
        <version>4.3</version>
      </dependency>
      <!-- spring-jms -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.3.0.RELEASE</version>
      </dependency>
      <!-- activemq -->
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>5.7.0</version>
      </dependency>

二、配置spring-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://activemq.apache.org/schema/core
            http://activemq.apache.org/schema/core/activemq-core.xsd">

    <!-- ActiveMQ 连接工厂 -->
    <amq:connectionFactory id="amqConnectionFactory"
        brokerURL="${mq.broker.url}" userName="${mq.username}" password="${mq.password}"/>

    <!-- Spring 连接工厂 -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="amqConnectionFactory" />
        <property name="sessionCacheSize" value="100" />
    </bean>

    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 定义消息队列(Queue) -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 配置多个消息队列(多个队列名字以逗号分隔) -->
        <constructor-arg value="user_book_queue" />
    </bean>

    <!-- 配置消息队列监听者(Queue) -->
    <bean id="queueConsumerListener" class="com.zmgj.zmxj.mq.QueueConsumerListener" />

    <!-- 配置消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
    <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="queueDestination" />
        <property name="messageListener" ref="queueConsumerListener" />
    </bean>

</beans>

三、配置消息监听

package com.zmgj.zmxj.mq;

import com.zmgj.zmxj.constant.PublicConstant;
import com.zmgj.zmxj.model.MqBean;
import com.zmgj.zmxj.service.IUserBookMarkService;
import com.zmgj.zmxj.service.IUserBookNoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * Created by syk on 2017/9/1.
 */
public class QueueConsumerListener implements MessageListener {

    private final static Logger logger = LoggerFactory.getLogger(QueueConsumerListener.class);
    private final String QUEUE_PREFIX = "queue://";

    @Autowired
    private IUserBookMarkService userBookMarkService;
    @Autowired
    private IUserBookNoteService userBookNoteService;

    @Override
    public void onMessage(Message message) {
        try {
            if (null != message) {
                ObjectMessage objectMessage = (ObjectMessage) message;
                String destination = message.getJMSDestination().toString();
                logger.info("目的消息队列【" + destination + "】");
                switch (destination) {
                    case QUEUE_PREFIX + PublicConstant.QUEUE_USER_BOOK:
                        MqBean mqBean = (MqBean) objectMessage.getObject();
                        // 具体业务处理逻辑
                        break;
                }
                logger.info("------Success------");
            } else {
                logger.info("------No producer at the moment------");
            }
        } catch (JMSException e) {
            logger.error(e.getMessage(), e);
        }
    }

}

四、生产者

package com.zmgj.zmxj.service.impl;

import com.zmgj.zmxj.service.IProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by syk on 2017/9/1.
 */
@Service
public class ProducerServiceImpl implements IProducerService {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public void sendMessage(String destination, Object msg) {
        jmsTemplate.convertAndSend(destination, msg);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值