SpringBoot整合ActiveMQ消息队列

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/guandongsheng110/article/details/86541314

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 = "guan.dong.sheng")
    public void receive(String message) {
        System.out.println("自动消费消息: " + message);
    }

    @Override
    @JmsListener(destination = "guan.dong.sheng", 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调用

@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

 

展开阅读全文

没有更多推荐了,返回首页