版本信息:activemq5.9 jdk 1.8 springboot 2.0.2
生产者相关配置 provider
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>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<artifactId>java-learn-examples-activemq-spring-boot-provider</artifactId>
<groupId>com.julong</groupId>
<name>java-learn-examples-activemq-spring-boot-provider</name>
<description>java-learn-examples-activemq-spring-boot-provider</description>
<version>0.0.1-SNAPSHOT</version>
<url>http://projects.spring.io/spring-boot/</url>
<packaging>jar</packaging>
<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-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
debug=true
## 配置广播地址
spring.activemq.broker-url=tcp://192.168.10.222:61616
spring.jms.listener.auto-startup=true
## 配置是否为发布订阅模式 false 默认为队列模式
spring.jms.pub-sub-domain=false
## 默认队列名称
spring.jms.template.default-destination=springQueue
## 配置是否为发布订阅模式 false 默认为队列模式
#spring.jms.pub-sub-domain=true
## 默认队列名称
#spring.jms.template.default-destination=springTopic
QueueServiceImpl.java
package com.julong.queue.service.impl;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import com.julong.queue.service.QueueService;
/**
* 队列消息模式
* @author julong
* @date 2021年11月10日 下午9:25:50
* @desc
*/
@Service
public class QueueServiceImpl implements QueueService {
@Autowired
private JmsTemplate jmsTemplate;
@Override
public void sendQueueMessage(final String message) throws Exception {
// TODO Auto-generated method stub
this.jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
return session.createTextMessage(message);
}
});
}
}
provider 启动类
package com.julong.queue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.annotation.EnableJms;
import com.julong.queue.service.QueueService;
/**
* 队列模式消息
* @author julong
* @date 2021年11月15日 上午11:12:06
* @desc
*/
@SpringBootApplication
@EnableJms //启用jsm消息
public class ActiveMQProviderQueueApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(ActiveMQProviderQueueApplication.class, args);
//根据容器获取bean对象
QueueService queueService = context.getBean(QueueService.class);
//调用bean对象的方法, 发送消息
queueService.sendQueueMessage("Hello spring boot Message !");
}
}
消费端 consumer 的 相关引入的依赖 一样,配置有点区别 以下位 consumer 端的调用
QueueServiceImpl.java
package com.julong.queue.service.impl;
import javax.jms.Message;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import com.julong.queue.service.QueueService;
/**
* 点对点消息接收类
* @author julong
* @date 2021年11月10日 下午10:32:06
* @desc
*/
@Service
public class QueueServiceImpl implements QueueService {
@Autowired
private JmsTemplate jmsTemplate;
@Override
public String receiveQueueMessage() throws Exception {
// TODO Auto-generated method stub
Message message = this.jmsTemplate.receive();
TextMessage textMessage = (TextMessage) message;
String result = textMessage.getText();
return result;
}
}
application.properties
debug=true
## 配置广播地址
spring.activemq.broker-url=tcp://192.168.10.222:61616
spring.jms.listener.auto-startup=true
spring.jms.listener.acknowledge-mode=auto
## 配置是否为发布订阅模式 false 默认为队列模式
spring.jms.pub-sub-domain=false
## 默认队列名称
spring.jms.template.default-destination=springQueue
## 配置是否为发布订阅模式 false 默认为队列模式
#spring.jms.pub-sub-domain=true
## 默认队列名称
#spring.jms.template.default-destination=springTopic
consumer 启动类
package com.julong.queue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.annotation.EnableJms;
import com.julong.queue.service.QueueService;
/**
* 队列模式消息
* @author julong
* @date 2021年11月15日 上午11:13:20
* @desc
*/
@SpringBootApplication
@EnableJms //启用 java message
public class ActiveMQConsumerQueueApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(ActiveMQConsumerQueueApplication.class, args);
//根据容器获取bean对象
QueueService queueService = context.getBean(QueueService.class);
//调用bean对象的方法, 发送消息
String message = queueService.receiveQueueMessage();
System.out.println("服务端发送的消息是:"+message);
}
}