这篇文章给大家介绍使用springboot如何实现对activemq进行集成,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
10年积累的做网站、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有邕宁免费网站建设让你可以放心的选择与我们合作。
ActiveMQ
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
特性
更多关于 ActiveMQ 的内容可以点击这里。
Spring-Boot 集成 ActiveMQ
添加maven依赖
org.springframework
spring-jms
org.apache.activemq
activemq-client
没有直接使用注释的依赖,是因为其含有如下依赖
org.apache.activemq activemq-broker
而它的作用是什么呢,会在程序中直接内嵌 ActivityMQ,也就是说不需要安装 ActiveMQ,但是这个如果服务宕机了,内嵌的 ActiveMQ 也就没了。关键,这个内嵌的 ActiveMQ 而无法看到图形化界面,所以这里没有直接使用注释里的依赖。
在application.properties中增加如下配置
# activemq spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.in-memory=true spring.activemq.pool.enabled=false
这里对 ActiveMQ 的端口做一个简短说明,61616为消息代理接口 ,8161 为管理界面
JAVA代码实现
定义QUEUE
package com.activemq.queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
@Configuration
public class QueueConfig {
@Bean
public Queue logQueue() {
return new ActiveMQQueue(QueueName.LOG_QUEUE);
}
}
消息生产者
package com.activemq.producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
@Component
public class LogProducer implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class);
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue logQueue;
@Override
public void run(String... strings) throws Exception {
send("This is a log message.");
LOGGER.info("Log Message was sent to the Queue named sample.log");
}
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg);
}
}
消息消费者
package com.activemq.consumer;
import com.activemq.queue.QueueName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class LogConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(LogConsumer.class);
@JmsListener(destination = QueueName.LOG_QUEUE)
public void receivedQueue(String msg) {
LOGGER.info("Has received from " + QueueName.LOG_QUEUE + ", msg: " + msg);
}
}
测试接口
@Autowired
private LogProducer logProducer;
@GetMapping("/activemq/send")
public String activemq(HttpServletRequest request, String msg) {
msg = StringUtils.isEmpty(msg) ? "This is Empty Msg." : msg;
try {
logProducer.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
return "Activemq has sent OK.";
}
启动类
增加如下注解@EnableJms
@Configuration//配置控制
@EnableAutoConfiguration//启用自动配置
@ComponentScan//组件扫描
@EnableConfigurationProperties({EmailProp.class})
@EnableJms
public class Bootstrap {
private static final Logger LOGGER = LoggerFactory
.getLogger(Bootstrap.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(Bootstrap.class, args);
LOGGER.info("Server running...");
}
}
测试
运行服务,在浏览器输入 http://127.0.0.1:8080/activemq/send?msg=test%20log,会在控制台看到如下输出
INFO 1498 --- [enerContainer-1] c.j.a.activemq.consumer.LogConsumer : Has received from sample.log, msg: test log [DefaultMessageListenerContainer-1] INFO c.j.a.activemq.consumer.LogConsumer - Has received from sample.log, msg: test log
打开 ActiveMQ 的管理页面,用户名密码都是admin,可以看到如下信息

关于使用springboot如何实现对activemq进行集成就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。