目录
1:导包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot和activemq整合的启动器-->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2:队列
2.1:写配置
server.port=8081 spring.activemq.broker-url=tcp://192.168.43.122:61616 spring.activemq.user=admin spring.activemq.password=damin #是否为主题;现在为队列,所以为false,默认也是false spring.jms.pub-sub-domain=false myqueue=boot-activemq-queue
2.2:测试生产者
1:设置队列等信息的config类
package com.wkl.springboot_activemq.activemq;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
/**
* Description:写springboot下的mq的配置
* Date: 2020/9/7 - 下午 8:42
* author: wangkanglu
* version: V1.0
*/
@Component
public class config {
@Value("${myqueue}")
private String myqueue;
@Bean
public Queue queue(){
return new ActiveMQQueue(myqueue);
}
}
2:发送队列消息的程序方法类
package com.wkl.springboot_activemq.activemq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
/**
* Description:队列的生产者
* Date: 2020/9/7 - 下午 8:45
* author: wangkanglu
* version: V1.0
*/
@Component
public class Produce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void send(){
jmsMessagingTemplate.convertAndSend(queue,"boot发送消息---");
}
}
3:测试:
package com.wkl.springboot_activemq;
import com.wkl.springboot_activemq.activemq.Produce;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
class SpringbootActivemqApplicationTests {
@Autowired
private Produce produce;
@Test
void contextLoads() {
produce.send();
System.out.println("----end----");
}
}
2.3:测试消费者
package com.wkl.springbootactivemqqueueconsumer.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import sun.plugin2.message.TextEventMessage;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* Description:队列的消费者,直接配置监听器
* Date: 2020/9/8 - 上午 9:39
* author: wangkanglu
* version: V1.0
*/
@Component
public class MQconsumer {
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage message){
try {
System.out.println("消费者收到消息:"+message.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
结果:
3:主题
3.1:配置
server.port=8083
spring.activemq.broker-url=tcp://192.168.43.122:61616
spring.activemq.user=admin
spring.activemq.password=damin
#是否为主题;现在为主题,所以为true,默认也是false
spring.jms.pub-sub-domain=true
mytopic=boot-activemq-topic
3.2:测试生产者
配置:
package com.wkl.springboot_activemq_topicproduce.activemq;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.jms.Topic;
/**
* Description:主题的配置类,将主题放进容器中
* Date: 2020/9/8 - 上午 10:53
* author: wangkanglu
* version: V1.0
*/
@Component
public class config {
@Value("${mytopic}")
private String topicName;
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
}
发布消息:
package com.wkl.springboot_activemq_topicproduce.activemq;
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;
/**
* Description:主题的发布者
* Date: 2020/9/8 - 上午 10:55
* author: wangkanglu
* version: V1.0
*/
@Component
public class Produce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
/*让这个主题发布者,每3秒中发布一条消息*/
@Scheduled(fixedDelay = 3000)
public void send(){
jmsMessagingTemplate.convertAndSend(topic, UUID.randomUUID().toString().substring(0,7));
}
}
3.3:测试消费者
package com.wkl.springboot_activemq_topicconsumer.activemq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* Description:主题的消费者
* Date: 2020/9/8 - 上午 11:00
* author: wangkanglu
* version: V1.0
*/
@Component
public class Consumer {
@JmsListener(destination = "${mytopic}")
public void recive(TextMessage message) throws JMSException {
System.out.println("收到消息:"+message.getText());
}
}
3.4:先开启订阅后再发布消息