Springboot中如何整合Activemq-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“Springboot中如何整合Activemq”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

專注于為中小企業(yè)提供網(wǎng)站制作、網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)新沂免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

1 導入整合所需要的依賴:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>

2 創(chuàng)建application.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8080
queue=myqueue

3.自定義配置文件QueueConfig 讀取配置文件的隊列名,根據(jù)隊列名字創(chuàng)建一個Queue

package com.example.demo;

import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class QueueConfig {

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

  @Bean
  public Queue logQueue() {
    return new ActiveMQQueue(queue);
  }}

4.創(chuàng)建生產(chǎn)者,可以直接使用提供的模板JmsMessagingTemplate 進行消息的發(fā)送:

package com.example.demo.producter;

import javax.jms.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import com.example.demo.SpringbootActivemqApplication;

@Component
public class Producter {
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
  @Autowired
  private Queue queue;
  private static Logger logger = LoggerFactory.getLogger(
Producter 
.class); public void send() { String str = "生產(chǎn)者生產(chǎn)數(shù)據(jù):" + System.currentTimeMillis(); jmsMessagingTemplate.convertAndSend(queue, str); logger.info("生產(chǎn)者數(shù)據(jù):{}", str); } }

5.啟動類:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.example.demo.producter.Producter;
import com.example.demo.producter.consumer.Consumer;

@SpringBootApplication
@EnableScheduling
public class SpringbootActivemqApplication implements ApplicationListener<ContextRefreshedEvent> {
  @Autowired
  public Producter producter;
  @Autowired
  public Consumer consumer;

  public static void main(String[] args) {
    SpringApplication.run(SpringbootActivemqApplication.class, args);
    //onApplicationEvent方法 在啟動springboot的時候 會運行該方法,可根據(jù)項目實際情況 選擇合適調(diào)用消息發(fā)送方法

  }

  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
    producter.send();
  }

}

6.啟動項目,控制臺輸出內(nèi)容:

Springboot中如何整合Activemq

Springboot中如何整合Activemq

7.創(chuàng)建消費者,創(chuàng)建消費者比較容易,只需要監(jiān)聽隊列就可以:

package com.example.demo.producter.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

  @JmsListener(destination = "${queue}")
  public void receive(String msg) {
    System.out.println("監(jiān)聽器收到msg:" + msg);
  }

}

8.最后結(jié)果:

Springboot中如何整合Activemq

“Springboot中如何整合Activemq”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

文章名稱:Springboot中如何整合Activemq-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article46/dihseg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、小程序開發(fā)、網(wǎng)站營銷、關(guān)鍵詞優(yōu)化、自適應網(wǎng)站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站