SpringBoot 2.1.x整合ActiveMQ消息中间件服务

#SpringBoot 2.x整合ActiveMQ消息中间件服务

  • pom.xml
 <dependencies>

        <!--ActiveMQ 启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!--ActiveMQ 链接池-->
        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
            <version>1.0.5</version>
        </dependency>

        <!--WEB模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--Jetty-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • application.yml
server:
  port: 8093
spring:
  activemq:
    broker-url: tcp://192.168.44.113:61616
    user: admin
    password: admin
    pool:
      enabled: true
      max-connections: 200
  jms:
    pub-sub-domain: true  #false为queues  true 为topics
  #服务名称
  application:
    name: boot-activemq
  • ActiveMQConfig.java
package com.hf.boot.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import javax.jms.Topic;


/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: ActiveMQConfig
 * @Author: hf
 * @Date: 2019/10/17 12:19
 * @Description: ActiveMQ 配置Bean
 */
@Component
@EnableJms //开启Jms注解
public class ActiveMQConfig {

    @Bean
    public Queue queue() {

        return new ActiveMQQueue("boot-activemq-queue-01");
    }

    @Bean
    public Topic topic() {

        return new ActiveMQTopic("boot-activemq-topic-01");
    }
}

  • ActiveMQServiceImpl.java
package com.hf.boot.service.impl;

import com.hf.boot.service.ActiveMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.jms.Queue;
import javax.jms.Topic;
import java.util.UUID;

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: ActiveMQServiceImpl
 * @Author: hf
 * @Date: 2019/10/17 12:40
 * @Description: ActiveMQ 服务实现层
 */
@Service
@Transactional
public class ActiveMQServiceImpl implements ActiveMQService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    //单点触发  推送queues消息
    @Override
    public void sendQueueMsg() {
        jmsMessagingTemplate.convertAndSend(queue, UUID.randomUUID().toString().replace("-", "").substring(0, 6));
        System.out.println("send queue msg over . . .");
    }

    //单点触发 推送topics消息
    @Override
    public void sendTopicMsg() {
        jmsMessagingTemplate.convertAndSend(topic, UUID.randomUUID().toString().replace("-", "").substring(0, 6));
        System.out.println("send topic msg over . . .");
    }


    // 定时推送queues消息
    //@Scheduled(fixedDelay = 3000)
    @Override
    public void timeSendQueueMsg() {
        String str = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
        System.out.println("Scheduled推送Queue消息:" + str);
        jmsMessagingTemplate.convertAndSend(queue, str);
    }

    //定时推送topics消息
    @Scheduled(fixedDelay = 8000)
    @Override
    public void timeSendTopicMsg() {
        String str = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
        System.out.println("Scheduled推送Topic消息:" + str);
        jmsMessagingTemplate.convertAndSend(topic, str);
    }
}

  • ActiveMQListener.java
package com.hf.boot.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: QueueListener
 * @Author: hf
 * @Date: 2019/10/17 12:57
 * @Description: ActiveMQ 消息监听
 */
@Component
public class ActiveMQListener {

    //queues消息监听
    @JmsListener(destination = "boot-activemq-queue-01")
    public void receiveQueue(TextMessage textMessage) throws JMSException {
        System.out.println("消费者接收到queues消息:"+textMessage.getText());
    }

    //topic消息监听
    @JmsListener(destination = "boot-activemq-topic-01")
    public void receiveTopic(TextMessage textMessage) throws JMSException {
        System.out.println("消费者接收到topics消息:"+textMessage.getText());
    }
}

  • BootActivemqApplication.java
package com.hf.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableScheduling //开启定时任务
@SpringBootApplication
public class BootActivemqApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootActivemqApplication.class, args);
    }

}

  • topics消息(推送/接收)测试【需要修改yml配置中pub-sub-domain属性为true】
    在这里插入图片描述
  • queues消息(推送/接收)测试【需要修改yml配置中pub-sub-domain属性为false】
    在这里插入图片描述
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值