ActiveMQ——传输协议

一、ActiveMQ支持的协议
 除了TCP协议之外,ActiveMQ还支持其他的很多协议,比如:AMQP、UDP、SSL、HTTP(S)、VM、MQTT、NIO等,具体可参考官网协议配置选项,也可以根据官网进行调优参数设置。

协议描述
TCP默认的协议,性能相对可以
NIO基于TCP协议,进行了扩展和优化
UDP性能比TCP更好,但是不具有可靠性
SSL安全链接
HTTP(S)基于HTTP或HTTPS
VMVM本身不是协议,当客户端和代理在同一个Java虚拟机(VM)中运行时,它们之间需要通信,但不想占用网络通道,而是直接通信时可以使用该方式

二、配置ActiveMQ的协议和端口
 在ActiveMQ安装目录的conf目录下有个activemq.xml配置文件,该文件中有个<transportConnectors>标签用来配置支持的协议,各个协议使用的端口也是在这里配置。默认配置内容如下:

<transportConnectors>
	<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
	<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

 ActiveMQ并不是只能同时支持一种协议,而是可以同时支持多种,也就是说,连接到ActiveMQ服务器的生产者和消费者可以是不同的协议。
 生产中为了提高MQ服务器的性能,我们大多会采用NIO的方式,因此我们可以将NIO的协议添加到支持的协议中,配置如下:

<transportConnectors>
	<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
	<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<!-- 新加的NIO协议,器端口为61618 -->
	<transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/>
</transportConnectors>

 在管理控制台可以看到当前支持的协议:
在这里插入图片描述
 配置了NIO后,在使用时我们只需要将协议的前缀改为nio即可:但并不是所有的协议都是如此,有些协议还需要改变编码

spring:
  activemq:
    broker-url: nio://192.168.2.107:61618

三、配置ActiveMQ同时支持NIO和多种协议
 默认情况下,除了NIO协议外其他协议都是采用BIO的网络IO模型,我们可以通过配置的方式使NIO同时支持其他多种协议,即使用其他协议时,采用的网络IO模型也是NIO。
 ActiveMQ支持自动探测协议类型的配置,配置时采用“auto+nio”的形式,示例如下:后面还可以跟一些参数,具体参看官网

<transportConnectors>
	<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
	<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
	<transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/>
	<transportConnector name="auto+nio" uri="auto+nio://0.0.0.0:61608"/>
</transportConnectors>

 此时只要客户端连接时使用的端口是61608,那么具体使用的协议就可以被侦测到,这种方式可同时支持多种协议,且这些协议都是使用的NIO网络IO模型。
在这里插入图片描述

spring:
  activemq:
    broker-url: tcp://192.168.2.107:61608 #协议可以是tcp、nio、STOMP、AMQP、MQTT,只要端口是61608即可
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你想了解如何使用Java连接ActiveMQ并查看topic消息,可以按照以下步骤操作: 1. 下载并安装ActiveMQ。可以从官网下载最新的ActiveMQ安装包,并按照安装向导进行安装。 2. 创建Java项目并导入ActiveMQ相关的jar包。可以使用Maven管理项目依赖,添加以下依赖: ```xml <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.16.2</version> </dependency> ``` 3. 创建连接和会话。可以使用以下代码创建连接和会话: ```java // 创建连接工厂 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创建连接 Connection connection = connectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ``` 4. 创建topic和消息消费者。可以使用以下代码创建topic和消息消费者: ```java // 创建topic Topic topic = session.createTopic("test.topic"); // 创建消息消费者 MessageConsumer consumer = session.createConsumer(topic); ``` 5. 接收消息。可以使用以下代码接收消息: ```java // 接收消息 Message message = consumer.receive(); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("Received message: " + textMessage.getText()); } ``` 这样就可以使用Java连接ActiveMQ并查看topic消息了。当然,还有其他更详细的操作可以参考ActiveMQ官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值