第一步:創建Maven項目
Maven依賴:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.12</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.12</version>
</dependency>
application.yml:
server:
port: 80
servlet:
context-path: /am
spring:
activemq:
broker-url: tcp://hcmaster:61616 #ActiveMQ服務器地址及端口
user: admin
password: admin
close-timeout: 5000
send-timeout: 3000
# 下面五行配置加上程序報錯,程序啓動不起來
# in-memory: false # true表示使用內置的MQ,false表示連接服務器
# pool:
# enabled: true # true表示使用連接池,false表示每發送一條數據就創建一個連接
# max-connections: 10 #連接池最大連接數
# idle-timeout: 30000 #空閒的連接過期時間,默認爲30s
jms:
pub-sub-domain: false # 默認值false表示Queue,true表示Topic
topicName: springboot-activemq-topic
# debug: true #顯示Debug信息
第二步:配置主題Bean
@Component
public class ActiveMQTopicConfig {
@Value("${topicName}")
private String topicName;
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
}
第三步:創建Producer
@Component
public class TopicProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
public void publishTopic(String msg) {
System.out.println("TopicProvider發送了消息 : " + msg);
jmsMessagingTemplate.convertAndSend(topic, msg);
}
@Scheduled(fixedDelay = 3000)
public void publishTopicScheduled(){
String msg ="xixi";
System.out.println("TopicProvider定時發送了消息 : " + msg+" "+System.currentTimeMillis());
jmsMessagingTemplate.convertAndSend(topic, msg+System.currentTimeMillis());
}
}
第三步:創建Consumer
@Component
public class TopicConsumer {
@JmsListener(destination = "${topicName}" , containerFactory="jmsListenerContainerTopic")
public void receiveQueue(String text) {
System.out.println("TopicConsumer消費了消息: "+text);
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
}
3.測試代碼
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableScheduling
public class TopicTests {
@Autowired
private TopicProducer topicProducer;
@Test
public void testPublishTopic() {
topicProducer.publishTopic("hehe");
}
@Test
public void testPublishTopicScheduled(){
topicProducer.publishTopicScheduled();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
結果