一、ActiveMQ简介
ActiveMQ是什么
ActiveMQ是Apache推出的一款开源的、完全支持JMS1.1和J2EE1.4规范的JMS Provider实现的消息中间件(MOM)
ActiveMQ能干什么
最主要的功能就是:实现JMS Provider,用来帮助实现高可用、高性能、可伸缩、易用和安全的企业级面向服务的系统
ActiveMQ特点
1、完全支持JMS1.1和J2EE 1.4规范(持久化,XA消息,事务)
2、支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
3、可插拔的体系结构,可以灵活定制,如:消息存储方式、安全管理等
4、很容易和Application Server集成使用
5、从设计上保证了高性能的集群,客户端-服务器,点对点
6、可以很容易的和Spring结合使用
7、支持多种语言的客户端
8、支持通过JDBC和journal提供高速的消息持久化
9、支持与Axis的整合
二、消息中间件
MOM基本功能
将信息以消息的形式,从一个应用程序传送到另一个或多个应用程序。
MOM的主要特点
1、消息异步接受,类似手机短信的行为,消息发送者不需要等待消息接受者的响应,减少软件多系统集成的耦合度
2、消息可靠接收,确保消息在中间件可靠保存,只有接收方收到后才删除消息,多个消息也可以组成原子事务
消息中间件的主要应用场景
在多个系统间进行整合和通讯的时候,通常会要求:
1、可靠传输,数据不能丢失,有的时候,也会要求不能重复传输
2、异步传输,否则各个系统同步发送接受数据,相互等待,造成系统瓶颈
三、简单示例
配置maven,如下
1
2
3
4
5
6
7
8
9
10
11
12
|
<
dependencies
>
<
dependency
>
<
groupId
>org.apache.activemq</
groupId
>
<
artifactId
>activemq-all</
artifactId
>
<
version
>5.9.0</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.xbean</
groupId
>
<
artifactId
>xbean-spring</
artifactId
>
<
version
>3.16</
version
>
</
dependency
>
</
dependencies
>
|
基本的Queue消息发送:JmsSend.java
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
33
34
35
36
|
package
com.liuy.queues;
import
javax.jms.Connection;
import
javax.jms.ConnectionFactory;
import
javax.jms.Destination;
import
javax.jms.MessageProducer;
import
javax.jms.Session;
import
javax.jms.TextMessage;
import
org.apache.activemq.ActiveMQConnectionFactory;
/**
* 基本的Queue消息发送
* @description 基本的Queue消息发送
* @author liuy
* @version 1.0
* @date:2017年4月11日下午12:05:27
*/
public
class
JmsSend {
public
static
void
main(String[] args)
throws
Exception {
ConnectionFactory connectionFactory =
new
ActiveMQConnectionFactory(
"liuy"
,
"123456"
,
"tcp://192.168.91.8:61616"
);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(
"my-queue"
);
MessageProducer producer = session.createProducer(destination);
for
(
int
i =
0
; i <
3
; i++) {
TextMessage message = session.createTextMessage(
"message--"
+ i);
Thread.sleep(
1000
);
// 通过消息生产者发出消息
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
|
运行发送代码后,效果如下:
基本的Queue消息接收:JmsReceiver.java
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
33
34
35
36
37
|
package
com.liuy.queues;
import
javax.jms.Connection;
import
javax.jms.ConnectionFactory;
import
javax.jms.Destination;
import
javax.jms.MessageConsumer;
import
javax.jms.Session;
import
javax.jms.TextMessage;
import
org.apache.activemq.ActiveMQConnectionFactory;
/**
* 基本的Queue消息接收
* @description 基本的Queue消息接收
* @author liuy
* @version 1.0
* @date:2017年4月11日下午12:21:40
*/
public
class
JmsReceiver {
public
static
void
main(String[] args)
throws
Exception {
ConnectionFactory cf =
new
ActiveMQConnectionFactory(
"liuy"
,
"123456"
,
"tcp://192.168.91.8:61616"
);
Connection connection = cf.createConnection();
connection.start();
final
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(
"my-queue"
);
MessageConsumer consumer = session.createConsumer(destination);
int
i=
0
;
while
(i<
3
) {
i++;
TextMessage message = (TextMessage) consumer.receive();
session.commit();
System.out.println(
"收到消 息:"
+ message.getText());
}
session.close();
connection.close();
}
}
|
运行接收代码后,效果如下:
本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1914818如需转载请自行联系原作者
我爱大金子