spring整合activeMQ

导包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-client</artifactId>
    <version>5.14.5</version>
</dependency>

消息生产者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"
       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">

    <context:component-scan base-package="com.offcn.sellergoods.controller"/>

    <!--获取ActiveMQ的factory-->
    <bean class="org.apache.activemq.ActiveMQConnectionFactory" id="targetConnectionFactory">
        <!--activeMQ的地址-->
        <property name="brokerURL" value="tcp://192.168.188.128:61616"/>
    </bean>

    <!--通过ActiveMQ的Factory获取spring整合的Factory-->
    <bean class="org.springframework.jms.connection.SingleConnectionFactory" id="singleConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
    </bean>

    <!--通过SingleConnectionFactory获取jmsTemplate-->
    <bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
        <property name="connectionFactory" ref="singleConnectionFactory"/>
    </bean>

    <!--设置 发送方的 Queue类型的消息队列-->
    <bean class="org.apache.activemq.command.ActiveMQQueue" id="mqQueue">
        <!--为消息队列起一个名字,接收方要与其一致-->
        <constructor-arg value="spring-queue"/>
    </bean>

</beans>

java代码中生产者发送数据
1.注入属性

@Autowired
private JmsTemplate jmsTemplate;
//获取消息队列
@Autowired
private ActiveMQQueue mqQueue;

2.发送数据

//发送数据的消息队列(接收方要一致),匿名内部类来发送消息
jmsTemplate.send(mqQueue, new MessageCreator() {
    @Override
    public Message createMessage(Session session) throws JMSException {
        /*可以创建多种类型的消息:TextMessage()、ObjectMessage()、MapMessage(),
        还有不常用的BytesMessage()和StreamMessage()
        其中的参数就是我们需要传递的内容 */
        return session.createTextMessage(jsonString);
    }
});

消费者的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--获取ActiveMQ的factory-->
    <bean class="org.apache.activemq.ActiveMQConnectionFactory" id="targetConnectionFactory">
        <!--activeMQ的地址-->
        <property name="brokerURL" value="tcp://192.168.188.128:61616"/>
    </bean>

    <!--通过ActiveMQ的Factory获取spring整合的Factory-->
    <bean class="org.springframework.jms.connection.SingleConnectionFactory" id="singleConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
    </bean>
    
    <!--设置 接收方的 Queue类型的消息队列-->
    <bean class="org.apache.activemq.command.ActiveMQQueue" id="mqQueue">
        <constructor-arg value="spring-queue"/>
    </bean>

    <!--设置监听  用来接收消息的-->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="singleConnectionFactory"/>
        <property name="destination" ref="mqQueue"/>
        <property name="messageListener" ref="queueListener"/>
    </bean>

    <!--自定义监听类  实现了MessageListener接口-->
    <bean class="com.offcn.search.listener.QueueListener" id="queueListener"/>

</beans>

java代码中接收数据,配置监听类

package com.offcn.search.listener;

import com.alibaba.fastjson.JSON;
import com.offcn.pojo.TbItem;
import com.offcn.search.service.ItemSearchService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.List;

public class QueueListener implements MessageListener {

    @Autowired
    private ItemSearchService itemSearchService;

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage)message;
        try {
            String jsonString = textMessage.getText();
            List<TbItem> itemList = JSON.parseArray(jsonString, TbItem.class);
            //接收到消息后调用想要执行的方法即可
            itemSearchService.importList(itemList);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值