1. 程式人生 > 程式設計 >Springboot Activemq整合過程程式碼圖解

Springboot Activemq整合過程程式碼圖解

這篇文章主要介紹了Springboot Activemq整合過程程式碼圖解,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Springboot+Activemq整合

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

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

2 建立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 讀取配置檔案的佇列名,根據佇列名字建立一個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.建立生產者,可以直接使用提供的模板JmsMessagingTemplate 進行訊息的傳送:

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 = "生產者生產資料:" + System.currentTimeMillis(); jmsMessagingTemplate.convertAndSend(queue,str); logger.info("生產者資料:{}",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的時候 會執行該方法,可根據專案實際情況 選擇合適呼叫訊息傳送方法

  }

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

}

6.啟動專案,控制檯輸出內容:

Springboot Activemq整合過程程式碼圖解

Springboot Activemq整合過程程式碼圖解

7.建立消費者,建立消費者比較容易,只需要監聽佇列就可以:

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("監聽器收到msg:" + msg);
  }

}

8.最後結果:

Springboot Activemq整合過程程式碼圖解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。