SptingBoot使用ActiveMQ

写在前边:

概述:
    消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构。目前使用较多的消息队列有ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ
应用场景:
    异步处理,应用解耦,流量削锋和消息通讯四个场景。

一、下载安装ActiveMQ

官网地址 [http://activemq.apache.org/][1] 这里使用 ActiveMQ Artemis

  • 解压到D:盘 D:apache-artemis

  • 创建 myartemis
    cd /d D:Dapache-artemisin
    artemis create D:apache-artemismyartemis

  • 需要设置账号密码
    Please provide the default username:

    ——————
    Please provide the default password:
    ——————

  • 需要设置是否允许匿名登录

    Allow anonymous access?, valid values are Y,N,True,False

  • 切换目录 cmd

    cd D:apache-artemismyartemisin

启动实例

(1)直接启动 artemis run
(2) 安装然后启动 artemis-service install ; artemis-service start

访问:http://localhost:8161 或者 http://localhost:8161/console

二、使用ActiveMQ:

使用场景:延时操作,比如注册发送邮件

  1. 配置ActiveMQ服务地址(application.yml)
    spring:
      messages:
        basename: i18n/Messages,i18n/Pages
      jms:
        pub-sub-domain: false   # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
      activemq:
        user: studyjava    # 连接用户名
        password: hello   # 连接密码
        broker-url: tcp://192.168.104.17:61616 # 消息组件的连接主机信息
  1. 注入Queue
    @Configuration
    @EnableJms
    public class ActiveMQConfig {
        @Bean
        public Queue queue(){
            return new ActiveMQQueue("study.msg.queue");
        }
    }
  1. 编写消息接受类

   @Service
      public class MessageConsumerService {
      @JmsListener(destination="study.msg.queue")
      public void receiveMessage(String text) {    // 进行消息接收处理
          System.err.println("【*** 接收消息 ***】" + text);
      }
  }
  1. 定义消息发布方法

      public interface IMessageProducerService {
       public void sendMessage(String msg) ;
   }
  1. 实现消息发布
    @Service
    public class MessageProducerServiceImpl implements IMessageProducerService {
        @Resource
        private JmsMessagingTemplate jmsMessagingTemplate;
        @Resource
        private Queue queue;
    
        @Override
        public void sendMessage(String msg) {
            this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
        }
    }
  1. 编写Controller 发送MQ
    @RestController
    public class TestController {
        @Resource
        private IMessageProducerService messageProducer;
    
        @RequestMapping(value="/chufabaojing",method= RequestMethod.GET)
    
        public String chufabaojing(String devicename){
    
            List<String> alarmStrList = new ArrayList<>();
            alarmStrList.add(devicename+"out fence01");
            alarmStrList.add(devicename+"out fence02");
            alarmStrList.add(devicename+"in fence01");
            alarmStrList.add(devicename+"in fence02");
    
            System.out.println("设备"+devicename+"出围栏报警");
            // 报警信息写入数据库
            System.out.println("报警数据写入数据库。。。");
    
            // 写入消息队列
            for (String alarmStr : alarmStrList) {
                this.messageProducer.sendMessage("study - " + alarmStr);
       }
    
            // 消息写进消息队列里就不管了
    
            // 下面两步骤移到activemq消费者里
            // 发送邮件
            // 发送短信
    
            return "success";
        }
    }

原文地址:https://www.cnblogs.com/monkay/p/11235893.html