问题描述
我对此很纠结。
我想订阅一个 ActiveMQ 主题。 ActiveMQ 适用于 Centos 机器,非本地主机。我可以使用 tcp、http 协议使用消息。代码;
jacocoTestCoverageVerification {
violationRules {
rule {
includes = ['com/myapp/*']
excludes = [
'com/myapp/folderToExclude1/*','com/myapp/folderToExclude2/*',]
limit {
minimum = 0.85
}
}
}
}
我想使用 wss 协议。这对我来说是必须的。当我使用 wss://host:port getting;
更改 url 时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();
}
所以我检查了替代方案。人们通过 Stomp over WS 来解决这个问题。我的第一个成就是 wss 连接。
任何推荐将不胜感激!
解决方法
您看到的异常是意料之中的,因为您使用的 OpenWire JMS 客户端不支持 WebSocket 连接,而且实际上并不需要。 WebSocket 连接实际上只与在有限环境(如 Web 浏览器)中运行的客户端相关。 Web 浏览器不支持运行 Java 客户端。
如果您真的想在 WebSockets 上使用 STOMP,那么您必须使用支持 WebSockets 的 STOMP 客户端实现(大多数情况下)。
请记住,ActiveMQ 是一个代理。它不为它支持的所有协议提供客户端。它只提供了一个 JMS 客户端,因为它是实现 JMS 所必需的。例如,STOMP 是一种标准化协议,任何人都可以实现一个客户端,该客户端将与任何实现 STOMP 的代理一起工作。有许多可用的 STOMP 客户端实现可以用多种不同的语言为许多不同的平台编写。任何好的搜索引擎都会帮助您找到适合您需求的搜索引擎。