ActiveMQ之整合SpringBoot

基礎設施搭建

新建生產者和消費者模塊:
在這裏插入圖片描述
修改pom文件,增加依賴jar包:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.5.RELEASE</version>
    </dependency>
    <!--spring boot整合activemq的jar包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
        <version>2.1.5.RELEASE</version>
    </dependency>
</dependencies>

消息生產者

生產者application.yml

server:
    port: 8081 # 端口號

spring:
    activemq:
        # activemq的broker的url
        broker-url: tcp://192.168.0.166:61616
        # 連接activemq的broker所需的賬號和密碼
        user: admin
        password: admin
    jms:
        # 目的地是queue還是topic, false(默認) = queue    true =  topic
        pub-sub-domain: false

#  自定義隊列名稱。這只是個常量
queueName: boot-activemq-queue

# 主題名稱
topicName: boot-activemq-topic

生產者配置類:

package com.chaytech.producer;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

@Component
@EnableJms // 開啓jms
public class Config {

    @Value("${queueName}")
    private String queueName;
    @Value("${topicName}")
    private String topicName;

    @Bean
    public ActiveMQQueue activeMQQueue(){
        return new ActiveMQQueue(queueName);
    }

    @Bean
    public ActiveMQTopic activeMQTopic(){
        return new ActiveMQTopic(topicName);
    }
}

生產者消息發送Controller:

package com.chaytech.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * 消息生產者
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 20:55
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;
    @Autowired
    private Topic topic;

    /**
     * 發送隊列消息
     *
     * @param msg
     */
    @GetMapping("/sendQueueMessage/{msg}")
    public void sendQueueMessage(@PathVariable String msg){
        jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

    /**
     * 發送主題消息
     *
     * @param msg
     */
    @GetMapping("/sendTopicMessage/{msg}")
    public void sendTopicMessage(@PathVariable String msg){
        jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }
}

啓動類:

package com.chaytech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 生產者啓動類
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 21:04
 */
@SpringBootApplication
public class ProducerApplication {

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

消息消費者

消費者application.yml

server:
  port: 8091 # 端口號
spring:
  activemq:
    # activemq的broker的url
    broker-url: tcp://192.168.0.166:61616
    # 連接activemq的broker所需的賬號和密碼
    user: admin
    password: admin
  jms:
    # 目的地是queue還是topic, false(默認) = queue    true =  topic
    pub-sub-domain: false

#  自定義隊列名稱。這只是個常量
queueName: boot-activemq-queue

# 主題名稱
topicName: boot-activemq-topic

消費者消息監聽器,當生產者有發出新的消息後,此處會自動消費:

package com.chaytech.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * 消費者消息監聽器
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 21:14
 */
@Component
public class ConsumerListener {

    @JmsListener(destination = "${queueName}")
    public void receiveQueueMsg(TextMessage textMessage) throws JMSException {
        System.out.println("消費者監聽到的隊列消息:" + textMessage.getText());
    }

    @JmsListener(destination = "${topicName}")
    public void receiveTopicMsg(TextMessage textMessage) throws JMSException {
        System.out.println("消費者監聽到的主題消息:" + textMessage.getText());
    }
}

啓動類:

package com.chaytech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 消費者啓動類
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 21:11
 */
@SpringBootApplication
public class ConsumerApplication {

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

隊列消息與主題消息切換

前面我們都是往隊列裏面發消息,那如果需要發主題消息,怎麼辦呢?
我們只需要修改生產者和消費者的配置文件以及將發送消息時的目的地指向Topic即可:

# 目的地是queue還是topic, false(默認) = queue    true =  topic
spring.jms.pub-sub-domain: false
package com.chaytech.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * 消息生產者
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 20:55
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic;
    
    /**
     * 發送主題消息
     *
     * @param msg
     */
    @GetMapping("/sendTopicMessage/{msg}")
    public void sendTopicMessage(@PathVariable String msg){
        jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }
}

定時消息投遞

前面我們講的都是觸發式消息投遞,那麼activeMQ還支持定時消息投遞,示例代碼如下:

package com.chaytech.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;

/**
 * 定時投遞消息
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 21:07
 */
@Component
public class ProducerScheduled {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;

    /**
     * 定時投遞消息,間隔3秒
     */
    @Scheduled(fixedDelay = 3000)
    public void scheduledSendQueueMessage(){
        jmsMessagingTemplate.convertAndSend(this.queue, "定時投遞消息 —> " + UUID.randomUUID());
    }
}

修改啓動類,開啓定時投遞:

package com.chaytech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 生產者啓動類
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/25 21:04
 */
@SpringBootApplication
@EnableScheduling // 開啓定時投遞消息
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章