Un, la dépendance pom
<?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 https://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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>springboot-activemq</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Deuxièmement, le fichier de configuration
server:
port: 8001
spring:
activemq:
broker-url: tcp://192.168.56.10:61616 #用自己的MQ服务器地址
user: admin
password: admin
jms:
pub-sub-domain: true #默认开启的是false 即为queue模式 / true为topic模式
mytopic: active-produce-topic
Trois, configuration
package activemq.produce.config;
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;
import javax.jms.Queue;
import javax.jms.Topic;
@Component
@EnableJms
public class Beanconfig {
@Value("${mytopic}")
private String mytopic;
@Bean
public Topic topic(){
return new ActiveMQTopic(mytopic);
}
}
Quatrièmement, le producteur
package activemq.produce.Bean;
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.Topic;
import java.util.UUID;
@Component //定义组件
public class Produce_topic {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
@Scheduled(fixedDelay = 3000) //设置定时发布
public void produce(){
jmsMessagingTemplate.convertAndSend(topic,"-----"+ UUID.randomUUID().toString().substring(0,6));
}
}
Cinq, les consommateurs
package activemq.produce.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
@Component
public class consumer_topic {
@JmsListener(destination = "${mytopic}") //消费者设置监听程序
public void consume(TextMessage textMessage) throws JMSException {
System.out.println("接收的消息"+textMessage.getText());
}
}
Six, le programme principal
@EnableScheduling: activer la reconnaissance de @Scheduled
package activemq.produce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class PTopicApplication {
public static void main(String[] args) {
SpringApplication.run(PTopicApplication.class);
}
}
Sept, catégorie de test
Ouvrez d'abord la console activemq
-
Démarrez le programme principal
-
On constate que les consommateurs continuent de recevoir des données au fur et à mesure que les informations sont diffusées