ActiveMQ入门学习
一.ActiveMQ的简介
Activemq : 是一个实现了JMS的消息中间间。该中间可跨平台,java ,c++ python ,.net都能使用,最近流行websocket ,mqtt 也能实现,但是activemq 是java写的,实现jms的规范写的!
特点:
1、支持多种语言编写客户端
2、对spring的支持,很容易和spring整合
3、支持多种传输协议:TCP,SSL,NIO,UDP等
4、支持AJAX
消息形式:
1、点对点(queue)
2、一对多(topic
二.MQ的原理
三.MQ的作用
1.异步操作
2.流量削峰
在淘宝双11的时候,晚上0点的时候会有大量的订单产生,可能会导致服务器崩溃,我们可以使用ActiveMQ使访问的流量均匀,以恒定的速率
3.解耦合
用户下单成功后,不需要(order-service)依赖(message-api)发起远程调用才能发送,只需要依赖mq就可以了
4.软总线
本质就是远程调用-》进程间的通信-》实现进程的内存共享
使用mq做软总线:
1.所有的内核都必须连接在一个mq上面
2.每个内核都能向mq里面发信息,
3.每个内核都能向mq里面读取信息。
四.ActiveMQ的安装
1.把ActiveMQ的压缩包上传到Linux系统
2.解压
3在bin目录下启动
4.页面
5.创建队列
6.创建主题
五.java操作ActiveMQ
1.导入依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
2.接收队列的消息
public class MQApp {
public static void main(String[] args) {
listener();
//sendQueueMsg();
}
public static void listener() {
String brokerURL = "tcp://120.27.247.208:61616";
// 1 连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
// 2 连接
try {
Connection connection = activeMQConnectionFactory.createConnection("admin", "admin");
connection.start(); // 在监听消息时,需要手动的启动
// 3 会话
/**
* transacted : 是否支持事务
* acknowledgeMode:签收模式,仅仅在事务为false 才起作用,若是true,他没有作用
* 3 种签收模式
* 1 AUTO_ACK(Session.AUTO_ACKNOWLEDGE) 只要客户端拿到消息,就代表它消费成功了,mq 服务器也会删除该消息
* 2 CLIENT_ACK(Session.CLIENT_ACKNOWLEDGE)客户端要拿到消息,而且必须手动确认,该消费消费成功了,mq 服务器才会删除它
* 3 DUPS_ACK(批量的随机签收)和发送顺序相关,当消息的数量达到一次次数后,进行一个签收
*
*/
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
// 5 消息发送的目的地(队列)
Destination destination = new ActiveMQQueue("sxt.queue");
// 6 消息的消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
System.out.println("开始监听");
messageConsumer.setMessageListener(new MessageListener() { // 新启动子线程
@Override
public void onMessage(Message message) {
System.out.println("收到消息了");
ActiveMQTextMessage textMsg = (ActiveMQTextMessage) (message);
try {
String text = textMsg.getText(); // 获取消息的内容
System.out.println("消息为:"+text);
message.acknowledge(); // 手动签收消息
session.commit(); // 消息被消费了,也要提交事务
} catch (JMSException e) {
e.printStackTrace();
}
}
});
System.in.read(); // 挂起主线程
} catch (Exception e) {
e.printStackTrace();
}finally {
// 关闭资源
}
}
然后在运行queue 的接收端,可以看到消息已经发出了:
2.向队列里面发送消息
在运行queue 的接收端,可以看到消息已经发出了:
6.ActiveMQ实际上的使用(整合spring)
Spring-context
ActiveMq
Spring-Jms 在spring 里面简化对activemq的操作
Spring-Jms 对activemq 的java的做了封装,以后操作activemq ,将非常简单
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
1.消息的发送 spring-jms-producer.xml
2.spring-jms-comsumer.xml
发送消息
public class ActiveMQApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-jms-comsumer.xml");
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
//jmsTemplate.send(destination, messageCreator);
jmsTemplate.convertAndSend("boot");
System.out.println("發送成功了");
監聽消息
try {
System.out.println("開始監聽");
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
监听器
一个简单的ActiveMQ就实现了 如果想要复杂使用的话,关于JMS中的生产者和消费者参数可以详细了解一下,比如能配置队列消息存活时间,消费优先级,消费模式。
还没有评论,来说两句吧...