首页 > 解决方案 > 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 连接。

任何建议将不胜感激!

标签: javaactivemqstomp

解决方案


您看到的异常是意料之中的,因为您使用的 OpenWire JMS 客户端不支持 WebSocket 连接,而且它并不真正需要。WebSocket 连接实际上只与在 Web 浏览器等有限环境中运行的客户端相关。Web 浏览器不支持运行 Java 客户端。

如果你真的想在 WebSockets 上使用 STOMP,那么你必须使用支持 WebSockets 的 STOMP 客户端实现(大多数都这样做)。

请记住,ActiveMQ 是一个代理。它不为它支持的所有协议提供客户端。它只提供了一个 JMS 客户端,因为它是实现 JMS 所必需的。例如,STOMP 是一种标准化协议,任何人都可以实现一个客户端,该客户端将与任何实现 STOMP 的代理一起工作。有许多 STOMP 客户端实现可用许多不同的语言编写,适用于许多不同的平台。任何好的搜索引擎都应该可以帮助您找到适合您需求的搜索引擎。


推荐阅读