IDEA 创建 SpringBoot 项目,
1.打开IDEA,创建新项目,选择Spring Initializr
2.输入Artifact
3.勾选Web
4.点击finish完成
5.进入项目,可以将以下内容删除
因为 SpringBoot 已经内置了对 ActiveMQ 的支持,所以直接引入依赖 spring-boot-starter-activemq
就行。整体项目结构如下:
1、pom.xml 文件
4.0.0
com.yuezhong
activemq
0.0.1-SNAPSHOT
jar
activemq
Demo project for Spring Boot ActiveMQ
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-activemq
org.springframework.boot
spring-boot-maven-plugin
2、
配置文件 application.properties
spring.activemq.broker-url=tcp://127.0.0.1:61616
# 在考虑结束之前等待的时间
#spring.activemq.close-timeout=15s
# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
spring.activemq.in-memory=true
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 密码
spring.activemq.password=admin
# 等待消息发送响应的时间。设置为0等待永远。
spring.activemq.send-timeout=0
spring.activemq.user=admin
# 是否信任所有包
#spring.activemq.packages.trust-all=
# 要信任的特定包的逗号分隔列表(当不信任所有包时)
#spring.activemq.packages.trusted=
# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。
#spring.activemq.pool.block-if-full=true
# 如果池仍然满,则在抛出异常前阻塞时间。
#spring.activemq.pool.block-if-full-timeout=-1ms
# 是否在启动时创建连接。可以在启动时用于加热池。
#spring.activemq.pool.create-connection-on-startup=true
# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。
#spring.activemq.pool.enabled=false
# 连接过期超时。
#spring.activemq.pool.expiry-timeout=0ms
# 连接空闲超时
#spring.activemq.pool.idle-timeout=30s
# 连接池最大连接数
#spring.activemq.pool.max-connections=1
# 每个连接的有效会话的最大数目。
#spring.activemq.pool.maximum-active-session-per-connection=500
# 当有"JMSException"时尝试重新连接
#spring.activemq.pool.reconnect-on-exception=true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
#spring.activemq.pool.time-between-expiration-check=-1ms
# 是否只使用一个MessageProducer
#spring.activemq.pool.use-anonymous-producers=true
3、
发送消息类
package com.yuezhong.activemq.client;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* Created by yz.shi on 2018/4/9.
*/
@Component
public class ActiveMQClient {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
if(!StringUtils.isEmpty(text)){
System.out.println("ActiveMQServer收到的报文为:"+text);
System.out.println("");
}
}
}
4、消息接收类
package com.yuezhong.activemq.server;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Destination;
/**
* Created by yz.shi on 2018/4/9.
*/
@Component
@EnableScheduling
public class ActiveMQServer {
@Autowired
private JmsTemplate jmsTemplate;
static int index = 0;
Destination destination = new ActiveMQQueue("mytest.queue");
@Scheduled(fixedDelay = 5000) // 5s执行一次 只有无参的方法才能用该注解
public void sendMessage() {
index++;
String message = "shiyuezhong" + index;
this.jmsTemplate.convertAndSend(destination, message);
}
}
5、ActivemqApplication
package com.yuezhong;
import com.yuezhong.queue.producer.Producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import javax.jms.Destination;
@SpringBootApplication
public class ActivemqApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqApplication.class, args);
}
}
另: Producer
package com.yuezhong.queue.producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
import javax.annotation.Resource;
/**
* Created by yz.shi on 2018/4/9.
*/
@Service("producer")
public class Producer {
@Resource
private JmsTemplate jmsTemplate;
/**
* 发送消息(主方法设置队列)
*
* @param destination 发送到的队列
* @param message 待发送的消息
*/
public void convertAndSend(Destination destination, final String message) {
jmsTemplate.convertAndSend(destination, message);
}
/**
* 发送消息(定时发送队列)
*/
static int i = 100;
Destination destination = new ActiveMQQueue("yzshi_queue");
@Scheduled(fixedDelay = 5000) // 5s执行一次 只有无参的方法才能用该注解
public void convertAndSend() {
i++;
String message = "这是定时任务queueProducer发送的第" + i + "个消息!";
jmsTemplate.convertAndSend(destination, message);
}
}
ConsumerB
package com.yuezhong.queue.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
/**
* Created by yz.shi on 2018/4/9.
*/
@Component
public class ConsumerB {
/**
* 使用JmsListener配置消费者监听的队列
*
* @param text 接收到的消息
*/
@JmsListener(destination = "yzshi_queue")
@SendTo("out.queue")
public String receiveQueue(String text) {
System.out.println("----------------Consumer-B : 收到的报文为:" + text);
return text;
}
@JmsListener(destination = "yzshi_queue")
public void printReceiveQueue(String text) {
System.out.println("+++++++++++++++++Consumer-B : 收到的报文为:" + text);
}
}
ConsumerC
package com.yuezhong.queue.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* Created by yz.shi on 2018/4/9.
*/
@Component
public class ConsumerC {
/**
* 使用JmsListener配置消费者监听的队列
*
* @param text 接收到的消息
*/
@JmsListener(destination = "out.queue")
public void consumerMessage(String text) {
System.out.println("Consumer-C : 从out.queue队列收到的回复报文为:" + text);
}
}
package com.yuezhong;
import com.yuezhong.queue.producer.Producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import javax.jms.Destination;
@SpringBootApplication
public class ActivemqApplication {
@Autowired
private Producer producer;
@PostConstruct
public void init() {
int num = 10;
try {
Destination destinationQueue = new ActiveMQQueue("yzshi_queue");
for (int i = 1; i <= num; i++) {
producer.convertAndSend(destinationQueue, "这是主queueProducer发送的第" + i + "个消息!");
}
System.out.println("activeMQ生产成功!");
} catch (Exception e) {
System.out.println("activeMQ生产失败!");
}
}
public static void main(String[] args) {
SpringApplication.run(ActivemqApplication.class, args);
}
}