queue生产者
源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_produce
- 新建项目
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.at.boot.activemq</groupId>
<artifactId>boot_mq_produce</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--spring boot整合activemq的jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.yml
#web占用的端口
server:
port: 7777
spring:
activemq:
#activemq的broker的url
broker-url: tcp://192.168.17.3:61616
#连接activemq的broker所需的账号和密码
user: admin
password: admin
jms:
#目的地是queue还是topic,false(默认) = queue true = topic
pub-sub-domain: false
#自定义队列名称。这只是个常量
myqueue: boot-activemq-queue
- 配置目的地的bean
/*
* 配置目的地的bean和开启springboot的jms功能
*/
package com.at.boot.activemq.config;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;
//让spring管理的注解,相当于spring中在xml 中写了个bean
@Component
//开启jms适配
@EnableJms
public class ConfigBean {
//注入配置文件中的myqueue
@Value("${myqueue}")
private String myQueue ;
@Bean //bean id="" class=""
public ActiveMQQueue queue(){
return new ActiveMQQueue(myQueue);
}
}
- 队列生产者代码
/*
* 队列生产者代码
* 发送消息
*/
package com.at.boot.activemq.produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import java.util.UUID;
@Component
public class Queue_Produce {
//JMS模板
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate ;
//这个是我们配置的队列目的地
@Autowired
private Queue queue ;
//发送消息
public void produceMessage(){
//一参是目的地,二参是消息的内容
jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6));
}
//定时任务,每3秒执行一次。非必须代码,仅为演示。
@Scheduled(fixedDelay = 3000)
public void produceMessageScheduled(){
produceMessage();
}
}
- 主启动类(非必须)
package com.at.boot.activemq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
//是否开启定时任务调度功能
@EnableScheduling
public class MainApp_Produce {
public static void main(String[] args) {
SpringApplication.run(MainApp_Produce.class,args);
}
}
- 单元测试(非必须)
package com.at.boot.activemq;
import com.at.boot.activemq.produce.Queue_Produce;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
//加载主类
@SpringBootTest(classes = MainApp_Produce.class)
//加载spring的junit
@RunWith(SpringJUnit4ClassRunner.class)
//加载web
@WebAppConfiguration
public class TestActiveMQ {
@Resource //这个是java的注解,而Autowried是spring的
private Queue_Produce queue_produce ;
//这个是java的注解,而Autowried是spring的
@Test
public void testSend() throws Exception{
queue_produce.produceMessage();
}
}
queue消费者
源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_consummer
pom.xml和application.yml文件和前面一样。唯一不同就是下面代码:
/*
* 注册一个消息监听器
* 项目开启后监听某个主题的消息
*/
package com.at.boot.activemq.consummer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.TextMessage;
@Component
public class Queue_consummer {
//注册一个监听器,destination指定监听的主题。
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws Exception{
System.out.println(" *** 消费者收到消息 ***"+textMessage.getText());
}
}
topic生产者
源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_topic_produce
- pom.xml(同上)
- application.yml
server:
port: 6666
spring:
activemq:
broker-url: tcp://192.168.17.3:61616
user: admin
password: admin
jms:
#目的地是queue还是topic,false(默认) = queue true = topic
pub-sub-domain: true
#自定义主题名称
mytopic: boot-activemq-topic
- 配置目的地的bean和开启JMS功能
package com.at.boot.activemq.topic.config;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;
import javax.jms.Topic;
@Component
@EnableJms
public class ConfigBean {
@Value("${mytopic}")
private String topicName ;
@Bean
public Topic topic() {
return new ActiveMQTopic(topicName);
}
}
- 生产者代码
package com.at.boot.activemq.topic.produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Topic;
import java.util.UUID;
@Component
public class Topic_Produce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate ;
@Autowired
private Topic topic ;
@Scheduled(fixedDelay = 3000)
public void produceTopic(){
jmsMessagingTemplate.convertAndSend(topic,"主题消息"+ UUID.randomUUID().toString().substring(0,6));
}
}
topic消费者
源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_topic_produce
- pom.xml(同上)
- application.yml(同上)
- 消费者代码
package com.at.boot.activemq.consummer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.TextMessage;
@Component
public class Topic_Consummer {
@JmsListener(destination = "${mytopic}")
public void receive(TextMessage textMessage) throws Exception{
System.out.println("消费者受到订阅的主题:"+textMessage.getText());
}
}