ActiveMQ使用
ActiveMQ是Apache基金会开源的一个消息队列框架,也叫消息中间件。
预备知识:
- 什么是JMS?
Java消息服务(Java Message Service)即JMS,是一个Java平台中面向中间件的API,用于两个程序间或者分布式系统中发送消息,进行异步通信。 - 什么是AMQP?
AMQP(Advanced Message Queuing Protocol)是一个提供统一消息服务的应用层标准协议,不受语言限制。 - JMS相关概念
- 消费者、订阅者: 接收并处理消息
- 消息:传递数据的内容
- 消息模式:传递消息的方式,JMS中定义了队列和主题两种模式
- 队列模式和主题模式区别?
- 队列模式中,一个消息只能被一个消费者消费
- 主题模式中,一个消息可以被所有订阅者消费,但必须提前订阅
一、Windows安装ActiveMQ
1.下载解压
2.启动服务
双击运行 \bin\win64\activemq.bat,如果失败使用管理员权限运行
默认端口为8161,访问 http://localhost:8161/ 用户名密码都为admin
二、Linux安装ActiveMQ
1.下载解压
1)创建并切换目录 mkdir /usr/soft && cd /usr/soft
2)下载压缩包 wget http://www.apache.org/dyn/closer.cgi?filename=/activemq/5.15.6/apache-activemq-5.15.6-bin.tar.gz&action=download
3)解压 tar -zvxf apache-activemq-5.15.6-bin.tar.gz
2.启动访问
进入bin目录 ./activemq start
,访问虚拟机 ip地址:8161
三、队列模式
1.创建maven项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lyf</groupId>
<artifactId>jms-test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jms-test</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.6</version>
</dependency>
</dependencies>
</project>
2.生产者
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class AppProducer {
private final static String url = "tcp://127.0.0.1:61616";
private final static String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
//1. 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//2. 创建连接
Connection connection = connectionFactory.createConnection();
//3. 启动连接
connection.start();
//4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5. 创建目标
Destination destination = session.createQueue(queueName);
//6. 创建生产者
MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < 100; i++) {
//7. 创建消息
TextMessage textMessage = session.createTextMessage("test_"+i);
//8. 发送消息
producer.send(textMessage);
System.out.println("发送消息: "+textMessage.getText());
}
//9. 关闭连接
session.close();
}
}
查看队列
3.消费者
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class AppConsumer {
private final static String url = "tcp://127.0.0.1:61616";
private final static String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
//1. 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//2. 创建连接
Connection connection = connectionFactory.createConnection();
//3. 启动连接
connection.start();
//4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5. 创建目标
Destination destination = session.createQueue(queueName);
//6. 创建消费者
MessageConsumer consumer = session.createConsumer(destination);
//7. 创建监听器
consumer.setMessageListener((message) -> {
//8. 接收消息
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收消息: " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
});
}
}
四、主题模式
1.修改队列名字
private final static String topicName = "topic-test";
2.修改主题
Destination destination = session.createTopic(topicName);
注意: 先启动消费者再启动生产者
五、Spring集成ActiveMQ
- ConnectionFactory 用于管理连接的连接工厂
- JmsTemplate 用于发送和接收消息的模板类,直接处理JMS消息
- MessageListener 消息监听器,实现onMessage(Message msg)方法
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lyf</groupId>
<artifactId>jms-test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jms-test</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 非集成环境使用 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
</project>
2.消息接口
public interface ProducerService {
void sendMessage(String message);
}
3.生产者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.TextMessage;
@Component
public class ProducerServiceImpl implements ProducerService {
@Autowired
private JmsTemplate jmsTemplate;
// @Resource(name = "topicDestination")
@Resource(name = "queueDestination")
private Destination destination;
@Override
public void sendMessage(String message) {
jmsTemplate.send(destination, session -> {
TextMessage textMessage = session.createTextMessage(message);
System.out.println("发送消息: " + textMessage.getText());
return textMessage;
});
}
}
4.消费监听
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@Component
public class ConsumerMessageListener implements MessageListener{
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接收消息: "+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
5.spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.lyf" />
<!-- ActiveMQ 提供连接工厂-->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://http://192.168.184.200:61616"></property>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<!-- 配置JMS连接 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
</bean>
<!-- 定义消息队列 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg value="queue"></constructor-arg>
</bean>
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 设置消息队列的名字 -->
<constructor-arg value="topic"></constructor-arg>
</bean>
<!-- 配置JMS模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!-- 配置消息队列监听者 -->
<bean id="consumerMessageListener" class="com.lyf.spring.ConsumerMessageListener" />
<!-- 消息监听容器-->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<!--<property name="destination" ref="topicDestination" />-->
<property name="messageListener" ref="consumerMessageListener" />
</bean>
</beans>
6.单元测试
import com.lyf.spring.ProducerServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class App {
@Autowired
private ProducerServiceImpl producerService;
@Test
public void producer(){
for (int i = 0; i < 100; i++) {
producerService.sendMessage("test_"+i);
}
}
}
六、集群环境
高可用:解决单点故障引起的服务中断
负载均衡:多服务器提供服务,避免单台服务器性能问题
1.集群方式
- Broker cluster:实现负载均衡,通过zookper同步多个Broker之间消息
- Master Slave:实现高可用,Master挂掉,Slave节点变为Master提供服务
高可用 | 负载均衡 | |
---|---|---|
Master Slave | 是 | 否 |
Broker Cluster | 否 | 是 |
Master Slave只有一个节点提供服务,Broker Cluster多个节点同时工作
2.高可用和高并发集群
- Node-a 同步消息
- Node-b 和 Node-c 做主从
- 生产者为Node-b和Node-c,三个都可以为消费者
3.集群配置
复制三份activemq分别命名为activemq-a、activemq-b、activemq-c
依次修改jetty.xml中port端口:8161、8162、8163
依次修改activemq.xml中tcp端口:61616、61617、61618,并添加networkConnectors配置:
activemq-a:
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<networkConnectors>
<networkConnector name="local_network" uri="static:(tcp:127.0.0.1:61617,tcp:127.0.0.1:61618)" />
</networkConnectors>
activemq-b:
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61617?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<networkConnectors>
<networkConnector name="network_a" uri="static:(tcp:127.0.0.1:61616)" />
</networkConnectors>
activemq-c:
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61618?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<networkConnectors>
<networkConnector name="network_b" uri="static:(tcp:127.0.0.1:61616)" />
</networkConnectors>
启动服务
4.客户端配置
ActiveMQ失效转移(failover)允许其中一台消息服务器宕机时,重新连接其他服务器。
生产者
private final static String url = "failover:(tcp://192.168.184.200:61617,tcp://192.168.184.200:61618)?randomize=true";
消费者
private final static String url = "failover:(tcp://192.168.184.200:61616,tcp://192.168.184.200:61617,tcp://192.168.184.200:61618)?randomize=true";
参考资料
ActiveMQ使用的更多相关文章
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- (jms)ActiveMQ 安装配置.
前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...
- node(ActiveMq)
简单尝试了node下的ActiveMQ 1.下载apache-activemq-5.9.0,执行bat文件: 2.登录http://localhost:8161/admin可查看其管理后台: 3.安装 ...
- ActiveMQ的集群方案对比及部署
转载:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- JMS学习之路(一):整合activeMQ到SpringMVC
JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到实际的业务需求中的话我们可以 ...
- ActiveMQ消息队列的使用及应用
这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录: 一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- Spring下ActiveMQ实战
MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...
- ActiveMQ(li)
一.ActiveMQ 首先,ActiveMQ不是一个框架,它不是struct,webx,netty这种框架,它更像是tomcat服务器,因为你使用它之前必须启动它,activeMQ和JMS的关系有点类 ...
随机推荐
- bzoj2333 离线 + 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...
- 安装FreeIPA以及应用时报错汇总
安装FreeIPA以及应用时报错汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ERROR DNS zone yinzhengjie.org.cn already exis ...
- java io系列08之 File总结
本文对File的API和常用方法进行介绍. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_08.html File 介绍 File 是“文件”和“目 ...
- 面向对象+jquery实现拖拽功能
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python——安装requests第三方库
使用pip安装 在cmd下cd到这个目录下C:\Python27\Scripts,然后执行pip install requests 在cmd 命令行执行 E: 进入e盘 cd Python\pr ...
- MySQL中innodb_flush_log_at_trx_commit的设置
innodb_flush_log_at_trx_commit=0,在提交事务时,InnoDB不会立即触发将缓存日志写到磁盘文件的操作,而是每秒触发一次缓存日志回写磁盘操作,并调用操作系统fsync刷新 ...
- MysqL_select for update锁详解
先来举一个在某些应用场景下会出现数据不一致的例子,当然存储引擎是InnoDB(至于为什么,后面再告诉你). 电商平台常见的下单场景: 一般商品表(goods)有基本的四个字段,id(主键),goods ...
- vertica系列:时间相关函数
-- * 注意: 本文的SQL是在 2017-09-14 测试的. 所以如果取当前日期, 结果为 2017-09-14* ------------------------------ 相关数据类型 - ...
- JS 样式字符串 转 JSON对象
项目中需要把div 上的样式值转成数据展示 形如: padding: 7px 2px 1px 3px; color: rgb(238, 65, 65); background-color: rgb(2 ...
- Javaweb学习笔记——(十四)—————— 服务器端验证注册登入表单项目
项目:https://download.csdn.net/download/qq_40223688/10463436 项目 功能: *注册 *登录--------------------------- ...