SpringBoot整合ActiveMQ消息队列

CentOS6安装ActiveMQ服务

1. 官网下载安装包 http://activemq.apache.org/activemq-5158-release.html

2. 解压文件 tar -zxvf apache-activemq-5.15.8-bin.tar.gz

3. 进入bin目录, 启动activemq ./activemq start, 需要java环境

4. 开放端口

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8161 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 61616 -j ACCEPT

5. 重启iptables服务 service iptables restart

6. 访问管理后台 http://192.168.64.131:8161/admin/ 用户名密码为admin

整合SpringBoot项目

1. 配置pom.xml文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

2. 配置application.yml文件

spring:
  jms:
    pub-sub-domain: true   
# 配置消息的类型,如果是true则表示为topic消息(订阅,多对多),如果为false表示Queue消息(点对点,一对一)
  activemq:
    broker-url: tcp://192.168.64.131:61616
    in-memory: true
    pool:
      enabled: false

3. 创建ActiveMQService接口

public interface ActiveMQService {

    void sendMessage(Destination destination, String message);

    String receiveQueue(Destination destination);

    void receive(String message);

    void receiveMessageTopic(String message);
}

4. 编写ActiveMQServiceImpl实现类

package com.example.demo.service.impl;

import com.example.demo.service.ActiveMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Repository;

import javax.jms.Destination;

/**
 * Created by GuanDS on 2019/1/18.
 */
@Repository("activeMQService")
public class ActiveMQServiceImpl implements ActiveMQService {

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Override
    public void sendMessage(final Destination destination, final String message) {
        System.out.println("生产消息: " + message);
        jmsTemplate.convertAndSend(destination, message);
    }

    @Override
    public String receiveQueue(Destination destination) {
        Message message = jmsTemplate.receive(destination);
        try {
            System.out.println("消费消息: " + message.getPayload());
            return message.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @JmsListener(destination = "com.test.mq")
    public void receive(String message) {
        System.out.println("自动消费消息: " + message);
    }

    @Override
    @JmsListener(destination = "com.test.mq", containerFactory = "topicListenerFactory")
    public void receiveMessageTopic(String message) {
        System.out.println("订阅消息: " + message);
    }
}

5. 配置topicListenerFactory

@Bean(name = "topicListenerFactory")
public JmsListenerContainerFactory<DefaultMessageListenerContainer> topicListenerFactory(ConnectionFactory activeMQConnectionFactory){
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(activeMQConnectionFactory);
    return factory;
}

6. Controller调用

private Destination destination = new ActiveMQQueue("com.test.mq");

@RequestMapping(value = "/activemq/send", method = RequestMethod.GET)
public String ActiveMQSend(HttpServletRequest request) {
    String name = request.getParameter("name");
    if (StringUtils.isEmpty(name)) {
        return "error";
    }
    activeMQService.sendMessage(destination, name);
    return "success";
}

@RequestMapping("/activemq/receive")
@ResponseBody
public String ActiveMQReceive() {
    return activeMQService.receiveQueue(destination);
}

7. 测试成功

生产消息: test
订阅消息: test

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值