首先创建一个SpringBoot工程,以下是依赖包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
ActiveMQ 队列 生产者代码
application.yml
server:
port: 8080
spring:
activemq:
broker-url: tcp://192.168.253.132:61616 # activeMQ服务器地址
user: admin
password: admin
jms:
pub-sub-domain: false # false = Queue, true = Topic
#自己队列的名称
queue-name: boot-activemq-queue
创建一个配置类,注册Queue队列
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.annotation.EnableJms;
import javax.jms.Queue;
/**
* @Author: lgz
* @Date: 2019/11/30 1:15
*/
@Configuration
@EnableJms
public class QueueConfigBean {
@Value("${queue-name}")
private String queueName;
@Bean
public Queue getQueue() {
return new ActiveMQQueue(queueName);
}
}
producer类代码,每隔5秒发送一条消息
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;
/**
* @Author: lgz
* @Date: 2019/11/30 1:18
*/
@Component
public class QueueProducer {
@Autowired
private Queue queue;
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Scheduled(fixedDelay = 5000)
public void sendMessage() {
jmsMessagingTemplate.convertAndSend(queue, "hello activemq --> " + System.currentTimeMillis());
System.out.println("send successfully......");
}
}
因为加了定时器,所以要在application启动类加上开启注解
@EnableScheduling
ActiveMQ 队列 消费者代码
依赖包和配置不变,只需要一个类和一个注解即可
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* @Author: lgz
* @Date: 2019/11/30 1:28
*/
@Component
public class QueueConsumer {
@JmsListener(destination = "${queue-name}")
public void getMessage(TextMessage textMessage) throws JMSException {
String text = textMessage.getText();
System.out.println("consumer 收到消息 --> " + text);
}
}
ActiveMQ 主题 生产者代码
application.yml
server:
port: 8080
spring:
activemq:
broker-url: tcp://192.168.253.132:61616 # activeMQ服务器地址
user: admin
password: admin
jms:
pub-sub-domain: true # false = Queue, true = Topic
#自己主题的名称
topic-name: boot-activemq-topic
创建一个配置类,注册Topic主题
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.annotation.EnableJms;
import javax.jms.Topic;
/**
* @Author: lgz
* @Date: 2019/11/30 1:15
*/
@Configuration
@EnableJms
public class TopicConfigBean {
@Value("${topic-name}")
private String topicName;
@Bean
public Topic getTopic() {
return new ActiveMQTopic(topicName);
}
}
producer类代码,每隔5秒发送一条消息
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;
/**
* @Author: lgz
* @Date: 2019/11/30 1:18
*/
@Component
public class TopicProducer {
@Autowired
private Topic topic;
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Scheduled(fixedDelay = 5000)
public void sendMessage() {
jmsMessagingTemplate.convertAndSend(topic, "hello activemq topic --> " + System.currentTimeMillis());
System.out.println("send successfully......");
}
}
因为加了定时器,所以要在application启动类加上开启注解
@EnableScheduling
ActiveMQ 主题 消费者代码
依赖包和配置不变,只需要一个类和一个注解即可
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* @Author: lgz
* @Date: 2019/11/30 1:28
*/
@Component
public class TopicConsumer {
@JmsListener(destination = "${topic-name}")
public void getMessage(TextMessage textMessage) throws JMSException {
String text = textMessage.getText();
System.out.println("consumer 收到消息 --> " + text);
}
}