Java SpringBoot如何整合ActiveMQ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
一、 如果要想在项目之中去使用 ActiveMQ 组件,则应该为项目添加依赖支持库,修改 pom.xml 配置文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
二、修改 application.yml 配置文件进行 activemq 的配置;
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
jms:
pub-sub-domain: false # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
activemq:
user: studyjava # 连接用户名
password: hello # 连接密码
broker-url: tcp://activemq-server:61616 # 消息组件的连接主机信息
三、 随后定义一个消息的消费者,消费者主要是进行一个监听控制,在 SpringBoot 里面可以直接利用注解@JmsListener进行监听:
package cn.study.microboot.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class MessageConsumerService {
@JmsListener(destination="study.msg.queue")
public void receiveMessage(String text) { // 进行消息接收处理
System.err.println("【*** 接收消息 ***】" + text);
}
}
四、 随后建立消息的发送者服务,一般而言如果进行消息的发送往往会准备出一个业务接口来:
package cn.study.microboot.producer;
public interface IMessageProducerService {
public void sendMessage(String msg) ;
}
五、随后建立一个配置程序类,定义 ActiveMQ 的消息发送模版处理类:
package cn.study.microboot.config;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
@Configuration
@EnableJms
public class ActiveMQConfig {
@Bean
public Queue queue() {
return new ActiveMQQueue("study.msg.queue") ;
}
}
六、创建消息发送的子类实现消息发送处理:
package cn.study.microboot.producer.impl;
import javax.annotation.Resource;
import javax.jms.Queue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import cn.study.microboot.producer.IMessageProducerService;
@Service
public class MessageProducerServiceImpl implements IMessageProducerService {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource
private Queue queue;
@Override
public void sendMessage(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
}
七、编写测试类来观察消息的处理:
package cn.study.microboot.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import cn.study.microboot.StartSpringBootMain;
import cn.study.microboot.producer.IMessageProducerService;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
@Resource
private IMessageProducerService messageProducer;
@Test
public void testSend() throws Exception {
for (int x = 0; x < 10; x++) {
this.messageProducer.sendMessage("study - " + x);
}
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69952307/viewspace-2677363/