消息中间件
MOM基本功能:
将信息以消息的形式,从一个应用程序传送到另一个或多个应用程序。
MOM主要特点
- 消息异步接收
- 消息可靠性
消息中间件主要的应用场景
在多个系统间进行整合和通讯的时候,通常会要求:
-
- 可靠传输:数据不能丢失,有时候也要求数据不能重复传输;
-
- 异步传输: 否则整个系统同步发送和接收数据,互相等待,造成系统瓶颈
目前比较知名的消息中间件
Weblogic JMS 、 Apache ActiveMQ、 Apache RocketMQ、 OracleAQ、 IBM MQServices、Apache Kafka、Rabbitmq、 ZeroMQ、 RocketMQ
ActiveMQ简介
ActiveMQ是什么
ActiveMQ是Apache推出的,一款开源的,支持JMS1.1和J2EE1.4规范的JMSProvider实现的消息中间件(Message Oriented Middleware,MOM)
ActiveMQ能做什么
实现JMS Provider ,用来帮助实现高可用、高性能、可伸缩、易用和安全的企业面向消息服务的系统
ActiveMQ的特点
为了节省文笔,特梳理如下图例进行说明,也方便理解;
如上图所示,无论高级特性,还是与其他开发框架做整合,还是持久化或是集群、规范支持、协议支持、设计结构等都非常的丰富和成熟
ActiveMQ的安装
资源地址:
资源官网: activemq.apache.org
资源路径: mirrors.tuna.tsinghua.edu.cn/apache//act…
安装命令:
提前安装JDK1.8,并设置环境变量
wget https://mirrors.tuna.tsinghua.edu.cn/apache//activemq/5.15.4/apache-activemq-5.15.4-bin.tar.gz
tar zxf apache-activemq-5.15.4-bin.tar.gz
./activemq start 停止 ./activemq stop
查看控制台: 查看端口:
netstat -an|grep 61616
## 或
ps -ef|grep activemq
http://127.0.0.1:8161/ (默认用户名密码:admin/admin)
入门应用
- 创建一个maven工程,在pom中引入依赖
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
</dependency>
- 发送消息到queue
//创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Constant.BROKER_URL);
Connection connection = null;
try {
//通过工厂获取一个连接
connection = connectionFactory.createConnection();
connection.start();
//通过连接创建一个会话,并默认设开启事务,自动确认消息
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//通过会话构建一个目的地
Destination destination = session.createQueue("my-test-queue");
//再通过session创建一个消息发送者,并设定发送目的地对象
MessageProducer messageProducer = session.createProducer(destination);
for (int i = 0; i < 10; i++) {
TextMessage message = session.createTextMessage("This is TextMessage >>" + i);
messageProducer.send(message);
}
session.commit();
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 接受Queue消息
//创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.0.104:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
//通过连接创建一个会话,并默认设开启事务,自动确认消息
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//通过会话构建一个目的地
Destination destination = session.createQueue("my-test-queue");
//再通过session创建一个消息发送者,并设定发送目的地对象
MessageConsumer consumer=session.createConsumer(destination);
for (int i = 0; i <10 ; i++) {
TextMessage message=(TextMessage)consumer.receive();
session.commit();
System.out.println("Receiver Message >>"+message.getText());
}
session.close();
connection.close();
总结
本章主要介绍了消息中间件概念以及主流的消息中间件,重点介绍了ActiveMQ产品,从简介到安装部署、到入门应用,本文算是自己对于知识点的总结,同时也给与分享,希望有不同的朋友提出自己在使用中的不同见解和经验