示例:要求每隔3s往MQ发送一条消息。
第一步:创建Maven项目
Maven依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!--Spring事务处理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.12</version>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.12</version>
</dependency>
application.yml:
server:
port: 80
servlet:
context-path: /am
spring:
activemq:
broker-url: tcp://hcmaster:61616 #ActiveMQ服务器地址及端口
user: admin
password: admin
close-timeout: 5000
send-timeout: 3000
# 下面五行配置加上程序报错,程序启动不起来
# in-memory: false # true表示使用内置的MQ,false表示连接服务器
# pool:
# enabled: true # true表示使用连接池,false表示每发送一条数据就创建一个连接
# max-connections: 10 #连接池最大连接数
# idle-timeout: 30000 #空闲的连接过期时间,默认为30s
jms:
pub-sub-domain: false # 默认值false表示Queue,true表示Topic
queueName: springboot-activemq-queue
# debug: true #显示Debug信息
第二步:配置队列Bean
@Component
public class ActiveMQQueueConfig {
@Value("${queueName}")
private String queueName;
@Bean //在Spring中注入一个名称为activeMQConfig的Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
}
第三步:生产者
@Component
public class QueueProducer {
//注入springboot封装的工具类,它是Jmstemplate的封装类
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void sendQueue(String msg) {
System.out.println("QueueProvider发送了消息 : " + msg);
//方法一:添加消息到消息队列
jmsMessagingTemplate.convertAndSend(queue, msg);
}
@Scheduled(fixedDelay = 3000) //该注解修改的方法不能有参数
public void sendQueueScheduled() {
String msg ="xixi";
System.out.println("QueueProvider定时发送了消息 : " + msg+" "+System.currentTimeMillis());
jmsMessagingTemplate.convertAndSend(queue, msg+System.currentTimeMillis());
}
}
第四步:消费者
@Component
public class QueueConsumer {
@JmsListener(destination = "${queueName}")
public void receiveQueue(String text) {
System.out.println("QueueConsumer消费了消息: "+text);
}
}
测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableScheduling //开启定时发送消息功能
public class QueueTests {
//注入springboot封装的工具类,它是Jmstemplate的封装类
@Autowired
private QueueProducer queueProducer;
@Test
public void testSendQueue() {
queueProducer.sendQueue("haha");
}
@Test
public void testScheduledSendQueue() {//间隔时间定投
queueProducer.sendQueueScheduled();
try {
System.in.read(); //让程序一直执行下去
} catch (IOException e) {
e.printStackTrace();
}
}
}