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

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

  • pom.xml
 <dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-activemqartifactId>
        dependency>
        
        <dependency>
            <groupId>org.messaginghubgroupId>
            <artifactId>pooled-jmsartifactId>
            <version>1.0.5version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        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】
    SpringBoot 2.1.x整合ActiveMQ消息中间件服务_第1张图片
  • queues消息(推送/接收)测试【需要修改yml配置中pub-sub-domain属性为false】
    SpringBoot 2.1.x整合ActiveMQ消息中间件服务_第2张图片

你可能感兴趣的:(SpringBoot)