ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
特性
- 多种语言和协议编写客户端。语言: Java,C,C++,C#,Ruby,Perl,Python,PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
- 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
- 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
- 通过了常见J2EE服务器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
- 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
- 支持通过JDBC和journal提供高速的消息持久化
- 从设计上保证了高性能的集群,客户端-服务器,点对点
- 支持Ajax
- 支持与Axis的整合
- 可以很容易的调用内嵌JMS provider,进行测试
更多关于 ActiveMQ 的内容可以点击这里。
Spring-Boot 集成 ActiveMQ
添加maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
-->
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-jms</ artifactId >
</ dependency >
< dependency >
< groupId >org.apache.activemq</ groupId >
< artifactId >activemq-client</ artifactId >
</ dependency >
|
没有直接使用注释的依赖,是因为其含有如下依赖
1
2
3
4
|
< dependency >
< groupId >org.apache.activemq</ groupId >
< artifactId >activemq-broker</ artifactId >
</ dependency >
|
而它的作用是什么呢,会在程序中直接内嵌 ActivityMQ,也就是说不需要安装 ActiveMQ,但是这个如果服务宕机了,内嵌的 ActiveMQ 也就没了。关键,这个内嵌的 ActiveMQ 而无法看到图形化界面,所以这里没有直接使用注释里的依赖。
在application.properties中增加如下配置
1
2
3
4
5
6
|
# 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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);
}
}
|
消息生产者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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);
}
}
|
消息消费者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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);
}
}
|
测试接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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,会在控制台看到如下输出
1
2
|
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,可以看到如下信息
官方示例:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/zl18310999566/article/details/54313464
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 32
-
2025-06-04 52
-
使用VPS搭建小说网站:安装与配置WordPress的最佳实践
2025-05-27 32 -
2025-05-25 36
-
2025-05-25 60