java - Java WSS 连接无法创建传输异常
问题描述
我很纠结这个。
我想订阅一个 ActiveMQ 主题。ActiveMQ 在 Centos 机器上工作,而不是 LOCALHOST。我可以使用 tcp、http 协议来使用消息。代码;
public static void main(String[] args) throws JMSException {
PropertyUtils.loadPropertyFile();
Properties receiverProperties = PropertyUtils.getreceiverProperties();
// URL of the JMS server
String url = (String) receiverProperties.get("receiver.connection.url");
// Name of the queue we will receive messages from
String subject = (String) receiverProperties.get("receiver.topic.name");
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
// Creating session for getting messages
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Getting the topic
Destination destination = session.createTopic(subject);
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);
Message message = null;
// Here we receive the message.
while (true) {
message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText() + "'");
}
}
// We will be using TestMessage in our example. MessageProducer sent us a
// TextMessage
// so we must cast to it to get access to its .getText() method.
// connection.close();
}
我想使用 wss 协议。这对我来说是必须的。当我用 wss://host:port 更改 url 时;
Could not create Transport. Reason: java.io.IOException: createTransport() method not implemented!
所以我检查了替代方案。人们通过 Stomp over WS 来解决这个问题。我的第一个成就是 wss 连接。
任何建议将不胜感激!
解决方案
您看到的异常是意料之中的,因为您使用的 OpenWire JMS 客户端不支持 WebSocket 连接,而且它并不真正需要。WebSocket 连接实际上只与在 Web 浏览器等有限环境中运行的客户端相关。Web 浏览器不支持运行 Java 客户端。
如果你真的想在 WebSockets 上使用 STOMP,那么你必须使用支持 WebSockets 的 STOMP 客户端实现(大多数都这样做)。
请记住,ActiveMQ 是一个代理。它不为它支持的所有协议提供客户端。它只提供了一个 JMS 客户端,因为它是实现 JMS 所必需的。例如,STOMP 是一种标准化协议,任何人都可以实现一个客户端,该客户端将与任何实现 STOMP 的代理一起工作。有许多 STOMP 客户端实现可用许多不同的语言编写,适用于许多不同的平台。任何好的搜索引擎都应该可以帮助您找到适合您需求的搜索引擎。
推荐阅读
- javascript - Next-Auth - 自定义 OAuth 2.0 提供程序集成
- c# - 如何快速处理 Websocket 数据
- c++ - 为 Flickable 提供 activeFocus
- javascript - 当我在 win 10 中运行脚本 npm run watch 时 laravel Mix 出现问题,我的 laravel mix 版本是 6.0.25
- angular - 如何安全地将 Angular 项目复制到另一台计算机?
- javascript - 有没有更聪明的方法来实现用 Javascript 中的变量替换?
- styled-components - 样式化的组件使用 flowjs 自动完成
- android - 我无法将图像插入到 sqlite 数据库
- kubernetes - Airflow:无法使用 KubernetesExecutor 和 PV 获取持久日志(官方 helm 图)
- mongodb - MongoDB 查询 - 获取数组的频率图