1、pom
<!--聚合工程集成關係-->
<!--統一整合第三方框架依賴信息-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- -springboot 整合Web組件 整合SpringMVC 就會把傳統方式的SpringMVC依賴的jar所有給下載來 -->
<!-- 引入spring-boot-starter-web 幫你整合好全部相關的依賴jar包 原理 maven依賴傳遞 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot整合activeMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
2、配置文件(application.yml)
spring:
activemq:
broker-url: tcp://192.168.44.135:61616
user: admin
password: admin
jms:
#此處若是爲false表示爲隊列(queue)true表示爲主題(topic)
pub-sub-domain: false
#隊列名稱
myqueue: boot-queue
3、配置類
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.stereotype.Component;
import javax.jms.Queue;
/**
* @ProjectName: springbootActiveMQDemo
* @Package: cn.**.config
* @Author: huat
* @Date: 2020/1/22 15:25
* @Version: 1.0
*/
@Configuration
public class ActiveMQConfig {
//經過配置文件獲取隊列名稱
@Value("${myqueue}")
private String queueName;
@Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
}
4、消息生產者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
/**
* @ProjectName: springbootActiveMQDemo
* @Package: cn.**.queue
* @Author: huat
* @Date: 2020/1/22 15:29
* @Version: 1.0
*/
@Component
public class QueueProduce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void produce(){
//發送消息
jmsMessagingTemplate.convertAndSend(queue,"****bootDemo");
}
@Scheduled(fixedDelay = 3000)//定時任務 3秒
public void produceScheduled(){
//發送消息
jmsMessagingTemplate.convertAndSend(queue,"Scheduled****bootDemo");
}
}
5、消費者
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* @ProjectName: springbootActiveMQDemo
* @Package: cn.**.queue
* @Author: huat
* @Date: 2020/1/22 16:35
* @Version: 1.0
*/
@Component
public class QueueConsumer {
@JmsListener(destination = "${myqueue}")//監聽註解,裏面的值爲隊列名稱
public void receive(TextMessage textMessage) throws JMSException {
System.out.println("接受的消息是"+textMessage.getText());
}
}
6、啓動類
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
/**
* @ProjectName: springbootActiveMQDemo
* @Package: cn.**
* @Author: huat
* @Date: 2020/1/22 14:46
* @Version: 1.0
*/
@SpringBootApplication
@EnableJms//開啓jms的註解以及適配
@EnableScheduling//開啓定時任務
public class SpringbootAppStart {
public static void main(String[] args) {
SpringApplication.run(SpringbootAppStart.class,args);
}
}