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);
}
}