SpringBoot - 整合ActiveMQ

簡介

Apache ActiveMQ是一個開源的消息中間件,它不僅完全支持JMS1.1規範,而且支持多種編
程語言,例如C/C++、C#、Delphi 、Erlang 、Adobe Flash 、Haskell 、Java 、JavaScript 、Perl 、 PHP 、Pike 、Python 和 Ruby 等,也支持多種協議,例如 OpenWire 、REST 、STOMP 、WS-Notification 、MQTT 、 XMPP 以及 AMQP 。 Apache ActiveMQ 也提供了對Spring框架的支持,可以非常容易地嵌入Spring中,同時它也提供了集羣支持。

一、引入依賴

<!-- ActiveMQ -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- ActiveMQ連接池 -->
<dependency>
	<groupId>org.messaginghub</groupId>
	<artifactId>pooled-jms</artifactId>
</dependency>

特別說明

使用springboot2.0+及以下版本時候,連接池依賴如下:

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency>

二、yml配置文件

spring:
  activemq:
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
    packages:
      trust-all: true        # 信任所有包,支持對象消息
    pool:
      enabled: true          # 使用連接詞
      max-connections: 10    # 連接池最大連接數
      idle-timeout: 30000ms  # 空閒連接過期時間,默認30

三、ActiveMQ的配置文件

@EnableJms
@Configuration
public class ActiveMQConfig {
    /**
     * Queue消息隊列
     */
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("active.queue");
    }

    /**
     * Topic消息隊列
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic("active.topic");
    }

    /**
     * Topic類型的消息必須配置Factory
     */
    @Bean
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }
    
    @Bean(name = "test")
    public Queue queueTest(){
        return new ActiveMQQueue("active.test");
    }
    
}

特別說明

上面的配置文件裏,配置了一個@Bean(name = "test"),是因爲我在看其他博文的時候,評論裏有說:這就兩個兩個隊列啊,其他名的隊列怎麼辦?我想可能是Spring沒學好或是忘記了怎麼注入帶name@Autowired默認是按照類型注入,如果你想按name注入,只需要再加一個@Qualifier("name")註解即可。或是使用@Resource(name = "name")代替@Autowired@Qualifier就行。

四、創建消息Pojo類

要想能發送和接收對象消息,Pojo類需要實現序列化接口。

@Data
public class Message implements Serializable {
    private String content;
    private Date date;

    public Message(String content, Date date) {
        this.content = content;
        this.date = date;
    }
}

五、發送與監聽消息

JmsMessagingTemplate是由 Spring 提供的一個JMS 消息發送模板,可以用來方便地進行消息
的發送,消息發送方法convertAndSend的第一個參數是消息隊列 ,第二個參數是消息內容。

@Component
public class JmsComponent {
    @Autowired
    private JmsMessagingTemplate template;
    @Autowired
    private Queue queue;
    @Autowired
    private Topic topic;

//    @Autowired
//    @Qualifier("test")
    @Resource(name = "test")
    private Queue queueTest;

    /**
     * 發送Queue消息
     */
    public void sendQueueMessage(Message msg){
        template.convertAndSend(this.queue,msg);
    }

    /**
     * 發送Topic消息
     */
    public void sendTopicMessage(Message msg){
        template.convertAndSend(this.topic,msg);
    }

    /**
     * 監聽active.queue隊列的消息
     */
    @JmsListener(destination = "active.queue")
    public void receiveQueueMessage(Message msg){
        System.out.println("收到消息:" + msg);
    }

    /**
     * 監聽active.topic隊列的消息
     */
    @JmsListener(destination = "active.topic",containerFactory = "jmsTopicListenerContainerFactory")
    public void receiveTopicMessage(Message msg){
        System.out.println("收到消息:" + msg);
    }

}

六、測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestActiveMQ {
    @Autowired
    private JmsComponent jmsComponent;

    @Test
    public void testQueueMessage(){
        Message message = new Message("發送Queue消息",new Date());
        jmsComponent.sendQueueMessage(message);
    }

    @Test
    public void testTopicMessage(){
        Message message = new Message("發送Topic消息",new Date());
        jmsComponent.sendTopicMessage(message);
    }
}

測試截圖

在這裏插入圖片描述
在這裏插入圖片描述
更多關於ActiveMQ,可以自行百度,或是可以參考從0開始Web開發實戰】SpringBoot集成ActiveMQ,詳細代碼手把手操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章