1. 程式人生 > 實用技巧 >SpringBoot整合ActiveMQ訂閱和主題(Topic)

SpringBoot整合ActiveMQ訂閱和主題(Topic)

1.建立Maven專案,在pom.xml中匯入相應jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion
>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent> <groupId>
org.example</groupId> <artifactId>activemq_springboot_demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source
> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!--spring-boot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--activemq--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>2.1.5.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.在resources目錄下建立application.yml

server:
  port: 8888

spring:
  activemq:
    broker-url: tcp://192.168.231.128:61616  #自己的MQ地址
    user: admin
    password: admin
  jms:
    pub-sub-domain:true   #預設是:false = 佇列(Queue) true = Topic

#定義自己的佇列名稱
myTopic: boot-activemq-Topic

3.建立主程式啟動類

/**
 * @author 主啟動類
 */
@SpringBootApplication
@EnableScheduling  //開啟定時任務
public class MainApp_Produce {

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

4.建立配置類讀取application.yml檔案中的Topic

/**
 * @author 讀取配置的Topic
 */
@Component
@EnableJms   //開啟Jms
public class ConfigBean {

    @Value("${myTopic}")
    private String myTopic;

    @Bean //<bean id="" class=""/>
    public Topic topic()
    {
        return new ActiveMQTopic(myTopic);
    }
}

5.建立佇列的生產者

@Component
public class Topic_Produce {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Topic topic;

    @Scheduled(fixedDelay = 3000)
    public void produceTopic()
    {
        jmsMessagingTemplate.convertAndSend(topic,"主題訊息"+ UUID.randomUUID().toString().substring(0,6));
    }
}

6.建立佇列的消費者

@Component
public class Topic_Consumer {
    @JmsListener(destination = "${myTopic}") //開啟自動監聽
    public void receive(TextMessage textMessage) throws JMSException
    {
        System.out.println("消費者收到訂閱的主題"+ textMessage.getText());
    }
}