SpringBoot集成ActiveMQ使用


寫在前邊:

概述: 消息隊列中間件是分布式系統中重要的組件,主要解決應用解耦,異步消息,流量削鋒等問題,實現高性能,高可用,可伸縮和最終一致性架構。目前使用較多的消息隊列有ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ 應用場景: 異步處理,應用解耦,流量削鋒和消息通訊四個場景。
一、下載安裝ActiveMQ

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

  • 解壓到D:盤 D:\apache-artemis

  • 創建 myartemis
    cd /d D:\Dapache-artemis\bin
    artemis create D:\apache-artemis\myartemis

  • 需要設置賬號密碼
    Please provide the default username:

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

  • 需要設置是否允許匿名登錄

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

  • 切換目錄 cmd

    cd D:\apache-artemis\myartemis\bin

  • 啟動實例

(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 # 消息組件的連接主機信息

  2. 注入Queue

    @Configuration
    @EnableJms
    public class ActiveMQConfig {
    @Bean
    public Queue queue(){
    return new ActiveMQQueue("study.msg.queue");
    }
    }

  3. 編寫消息接受類

    @Service
    public class MessageConsumerService {
    @JmsListener(destination="study.msg.queue")
    public void receiveMessage(String text) { // 進行消息接收處理
    System.err.println("【*** 接收消息 ***】" + text);
    }
    }

  4. 定義消息發布方法

    public interface IMessageProducerService {
    public void sendMessage(String msg) ;
    }

  5. 實現消息發布

    @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);
    }
    

    }

  6. 編寫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";
    }
    

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM