springboot整合activeMQ


  消息隊列中間件是分布式系統中重要的組件,主要解決應用耦合,異步消息,流量削鋒等問題。實現高性能,高可用,可伸縮和最終一致性架構。是大型分布式系統不可缺少的中間件。消息形式支持點對點和訂閱-發布。

ActiveMQ是什么
    1、ActiveMQ是消息隊列技術,為解決高並發問題而生
    2、ActiveMQ生產者消費者模型(生產者和消費者可以跨平台、跨系統)
    3、ActiveMQ支持如下兩種消息傳輸方式

    • 點對點模式,生產者生產了一個消息,只能由一個消費者進行消費

    • 發布/訂閱模式,生產者生產了一個消息,可以由多個消費者進行消費

1、安裝activeMQ

  • 下載地址
  • 解壓
  • 進入解壓后的目錄運行 ./bin/activemq start
  • 啟動后activemq會啟動兩個端口:
    • 8161是activemq的管理頁面,默認的賬號密碼都是admin
    • 61616是程序連接activemq的通訊地址

2、項目結構

3、引入依賴

       <!-- activemq依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        
        <!--消息隊列連接池-->
        <!-- 使用springboot2.0+及以下版本時候 -->
        <!-- <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.15.0</version>
        </dependency> -->
        <!-- 使用springboot2.1+時候 -->
        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
        </dependency>

3、修改application.properties

#服務端口號
server.port=8080
server.servlet.context-path=/activemqsb

#activemq配置
#ActiveMQ通訊地址
spring.activemq.broker-url=tcp://localhost:61616
#用戶名
spring.activemq.user=admin
#密碼
spring.activemq.password=admin
#是否啟用內存模式(就是不安裝MQ,項目啟動時同時啟動一個MQ實例)
spring.activemq.in-memory=false
#信任所有的包
spring.activemq.packages.trust-all=true
#是否替換默認的連接池,使用ActiveMQ的連接池需引入的依賴
spring.activemq.pool.enabled=false

4、配置activeMQ

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

    //springboot默認只配置queue類型消息,如果要使用topic類型的消息,則需要配置該bean
    @Bean
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //這里必須設置為true,false則表示是queue類型
        factory.setPubSubDomain(true);
        return factory;
    }

    @Bean
    public Topic topic() {
        return new ActiveMQTopic("springboot.topic") ;
    }
}

5、創建消費者

@Service
//消費者
public class Consumer {
    //接收queue類型消息
    //destination對應配置類中ActiveMQQueue("springboot.queue")設置的名字
    @JmsListener(destination="springboot.queue")
    public void ListenQueue(String msg){
        System.out.println("接收到queue消息:" + msg);
    }

    //接收topic類型消息
    //destination對應配置類中ActiveMQTopic("springboot.topic")設置的名字
    //containerFactory對應配置類中注冊JmsListenerContainerFactory的bean名稱
    @JmsListener(destination="springboot.topic", containerFactory = "jmsTopicListenerContainerFactory")
    public void ListenTopic(String msg){
        System.out.println("接收到topic消息:" + msg);
    }
}

6、創建生產者

@RestController
//生產者
public class Producer {
    
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    //發送queue類型消息
    @GetMapping("/queue")
    public void sendQueueMsg(String msg){
        jmsTemplate.convertAndSend(queue, msg);
    }

    //發送topic類型消息
    @GetMapping("/topic")
    public void sendTopicMsg(String msg){
        jmsTemplate.convertAndSend(topic, msg);
    }
}

7、啟動類

@SpringBootApplication
@EnableJms //啟動消息隊列
public class ActivemqsbApplication {

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

}

8、啟動程序測試

  • 瀏覽器中輸入http://localhost:8080/activemqsb/queue?msg=hello

    控制台:接收到queue消息:hello

  • 瀏覽器中輸入http://localhost:8080/activemqsb/topic?msg=hello
    控制台:接收到topic消息:hello

參考鏈接:


免責聲明!

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



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