SpringBoot整合ActiveMQ過(guò)程解析
目錄結(jié)構(gòu)

引入 maven依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
引入 application.yml配置
spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: admin queue: springboot-queue server: port: 8080
創(chuàng)建QueueConfig
@Configuration
public class QueueConfig {
@Value("${queue}")
private String queue;
@Bean
public Queue logQueue() {
return new ActiveMQQueue(queue);
}
@Bean
public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDeliveryMode(2);// 進(jìn)行持久化配置 1表示非持久化,2表示持久化</span>
jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
jmsTemplate.setDefaultDestination(queue); // 此處可不設(shè)置默認(rèn),在發(fā)送消息時(shí)也可設(shè)置隊(duì)列
jmsTemplate.setSessionAcknowledgeMode(4);// 客戶端簽收模式</span>
return jmsTemplate;
}
// 定義一個(gè)消息監(jiān)聽器連接工廠,這里定義的是點(diǎn)對(duì)點(diǎn)模式的監(jiān)聽器連接工廠
@Bean(name = "jmsQueueListener")
public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(
ActiveMQConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory);
// 設(shè)置連接數(shù)
factory.setConcurrency("1-10");
// 重連間隔時(shí)間
factory.setRecoveryInterval(1000L);
factory.setSessionAcknowledgeMode(4);
return factory;
}
}
創(chuàng)建生產(chǎn)者:
@SpringBootApplication
@Component
@EnableScheduling
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Scheduled(fixedDelay=3000)
public void send() {
String result = System.currentTimeMillis()+"---測(cè)試";
System.out.println("result"+result);
jmsMessagingTemplate.convertAndSend(queue,result);
}
public static void main(String[] args) {
SpringApplication.run(Producer.class, args);
}
}
創(chuàng)建消費(fèi)者的application.yml
spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: admin queue: springboot-queue server: port: 8081
創(chuàng)建消費(fèi)者:
@Component
@SpringBootApplication
public class consumer {
private int count =0;
@JmsListener(destination = "${queue}")
public void receive(TextMessage textMessage,Session session) throws JMSException {
String text = textMessage.getText();
System.out.println("消費(fèi):"+text+"第幾次獲取消息count:"+(++count));
System.out.println();
String jmsMessageID = textMessage.getJMSMessageID();
}
public static void main(String[] args) {
SpringApplication.run(consumer.class,args);
}
}
結(jié)果顯示:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
struts2的國(guó)際化實(shí)現(xiàn)網(wǎng)站整體中英文切換實(shí)例代碼
本篇文章主要介紹了struts2的國(guó)際化實(shí)現(xiàn)網(wǎng)站整體中英文切換實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Mybatis-plus獲取雪花算法生成的ID并返回生成ID
本文主要介紹了Mybatis-plus獲取雪花算法生成的ID并返回生成ID,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
Java并發(fā)編程學(xué)習(xí)之Unsafe類與LockSupport類源碼詳析
這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程學(xué)習(xí)之Unsafe類與LockSupport類源碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-06-06
Java8 Instant 時(shí)間戳實(shí)例講解
Instant類是Java8 中補(bǔ)充的一個(gè) 時(shí)間戳類,nstant 可以使用靜態(tài)方法 now()或者of()方法來(lái)創(chuàng)建一個(gè)實(shí)例對(duì)象,本文通過(guò)實(shí)例代碼講解Java8 Instant 時(shí)間戳,感興趣的朋友跟隨小編一起看看吧2022-11-11
SpringBoot集成MQTT實(shí)現(xiàn)交互服務(wù)通信
MQTT非常適用于物聯(lián)網(wǎng)領(lǐng)域,本文主要介紹了SpringBoot集成MQTT實(shí)現(xiàn)交互服務(wù)通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
SpringBoot3整合Nacos?V2.3.2的詳細(xì)過(guò)程
本文介紹了如何在?Spring?Boot?3.2.x?項(xiàng)目中整合?Nacos?2.3.2,包括依賴配置、Nacos?服務(wù)發(fā)現(xiàn)與動(dòng)態(tài)配置的配置方法,通過(guò)整合?Nacos,Spring?Boot?應(yīng)用可以實(shí)現(xiàn)高效的服務(wù)發(fā)現(xiàn)、動(dòng)態(tài)配置管理以及分布式系統(tǒng)中的靈活擴(kuò)展,感興趣的朋友跟隨小編一起看看吧2024-11-11

