您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在SpringBoot中整合ActiveMQ,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
目錄結構
引入 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
創建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);// 進行持久化配置 1表示非持久化,2表示持久化</span> jmsTemplate.setConnectionFactory(activeMQConnectionFactory); jmsTemplate.setDefaultDestination(queue); // 此處可不設置默認,在發送消息時也可設置隊列 jmsTemplate.setSessionAcknowledgeMode(4);// 客戶端簽收模式</span> return jmsTemplate; } // 定義一個消息監聽器連接工廠,這里定義的是點對點模式的監聽器連接工廠 @Bean(name = "jmsQueueListener") public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory( ActiveMQConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(activeMQConnectionFactory); // 設置連接數 factory.setConcurrency("1-10"); // 重連間隔時間 factory.setRecoveryInterval(1000L); factory.setSessionAcknowledgeMode(4); return factory; } }
創建生產者:
@SpringBootApplication @Component @EnableScheduling public class Producer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Scheduled(fixedDelay=3000) public void send() { String result = System.currentTimeMillis()+"---測試"; System.out.println("result"+result); jmsMessagingTemplate.convertAndSend(queue,result); } public static void main(String[] args) { SpringApplication.run(Producer.class, args); } }
創建消費者的application.yml
spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: admin queue: springboot-queue server: port: 8081
創建消費者:
@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("消費:"+text+"第幾次獲取消息count:"+(++count)); System.out.println(); String jmsMessageID = textMessage.getJMSMessageID(); } public static void main(String[] args) { SpringApplication.run(consumer.class,args); } }
結果顯示:
關于如何在SpringBoot中整合ActiveMQ就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。