SpringBoot整合ActiveMQ
作者:互联网
整合队列
生产者
- 新建工程
- pox.xml文件配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
- application.yml
server:
port: 7777
spring:
activemq:
broker-url: tcp://192.168.10.100:61616 #自己的mq服务器地址
user: admin
password: admin
jms:
pub-sub-domain: false #false表示队列 true表示主题
#自己定义队列名称
myqueue: boot-activemq-queue
- 配置Bean
@Component
@EnableJms
public class ConfigBean {
@Value("${myqueue}")
private String myQueue;
@Bean
public Queue queue(){
return new ActiveMQQueue(myQueue);
}
}
- Queue_Produce
@Component
public class Queue_Produce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void produceMsg(){
jmsMessagingTemplate.convertAndSend(queue,"******"+ UUID.randomUUID().toString().substring(0,6));
}
}
- 启动类
@SpringBootApplication
public class MainApp_Produce {
public static void main(String[] args) {
SpringApplication.run(MainApp_Produce.class,args);
}
}
- 测试单元
@SpringBootTest(classes = MainApp_Produce.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
@Autowired
private Queue_Produce queue_produce;
@Test
public void testSend() throws Exception{
queue_produce.produceMsg();
}
}
- 每三秒推送消息
修改Queue_Produce新增定时投递方法
//间隔时间定投
@Scheduled(fixedDelay = 3000)
public void produceMsgScheduled(){
jmsMessagingTemplate.convertAndSend(queue,"******Scheduled"+ UUID.randomUUID().toString().substring(0,6));
System.out.println("send ok");
}
修改主启动类
@SpringBootApplication
@EnableScheduling
public class MainApp_Produce {
public static void main(String[] args) {
SpringApplication.run(MainApp_Produce.class,args);
}
}
直接开始主启动类
消费者
和生产者配置类似
Queue_Consumer
@Component
public class Queue_Consumer {
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws JMSException {
System.out.println("*******消费者收到消息"+textMessage.getText());
}
}
直接启动主启动类
标签:queue,SpringBoot,void,public,Queue,Produce,整合,ActiveMQ,class 来源: https://blog.csdn.net/qq_56224861/article/details/115874994