如何使用正确的协议从基于 java 的程序接收活动的 mq web 套接字 stomp
How to use the correct protocol for receiving active mq web socket stomp from java based program
我有一个Java基于脚本的活动 mq 侦听器,它正在使用 stomp 和 web sockets.I 能够将测试消息发送到活动 mq 并接收它们。
我真正想要的是它需要从基于 Java 的代码发送。
- 让Java脚本在网络上监听是否可以sockets/stomp并且
java 代码正在使用 tcp?
- 如果可以,所有的端口应该都一样吗?
我在 Java 脚本中接收数据时遇到问题。但是,我看到该主题已在活动 mq.thanks
中排队
function subscribeEndpoint(endpoint){
var client = Stomp.client("ws://localhost:61614/stomp", "v11.stomp");
var headers = { id:'JUST.FCX', ack: 'client'};
client.connect("admin", "admin", function () {
client.subscribe(endpoint,
function (message) {
alert(message);
}, headers);
});
}
Java:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616/stomp");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic)
Destination destination = session.createTopic("vrwrThreat");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage(text);
producer.send(message);
// Clean up
session.close();
connection.close();
客户端可以毫无问题地跨协议和传输机制相互通信。 WebSocket 或任何其他端口(TCP、SSL 等)上的 STOMP 客户端可以发送到主题,只要协议支持消息的内容,AMQP、MQTT 或 OpenWire 客户端就可以接收消息,反之亦然AMQP、MQTT 或 OpenWire 发送和 STOMP 接收的情况。
你说的这个案例很标准。您发布的代码似乎正在使用 OpenWire 客户端发送到主题 "vrwrThreat"。连接工厂中使用的 URI 略有偏差,因为您添加了无意义的“/stomp”。由于您要发送到主题,因此您需要确保接收消息的客户端在传输时处于活动状态,否则消息将被丢弃。您还需要确保两者都在您的代码片段中不清楚的同一主题上运行。
我有一个Java基于脚本的活动 mq 侦听器,它正在使用 stomp 和 web sockets.I 能够将测试消息发送到活动 mq 并接收它们。
我真正想要的是它需要从基于 Java 的代码发送。
- 让Java脚本在网络上监听是否可以sockets/stomp并且 java 代码正在使用 tcp?
- 如果可以,所有的端口应该都一样吗?
我在 Java 脚本中接收数据时遇到问题。但是,我看到该主题已在活动 mq.thanks
function subscribeEndpoint(endpoint){
var client = Stomp.client("ws://localhost:61614/stomp", "v11.stomp");
var headers = { id:'JUST.FCX', ack: 'client'};
client.connect("admin", "admin", function () {
client.subscribe(endpoint,
function (message) {
alert(message);
}, headers);
});
}
Java:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616/stomp");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic)
Destination destination = session.createTopic("vrwrThreat");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage(text);
producer.send(message);
// Clean up
session.close();
connection.close();
客户端可以毫无问题地跨协议和传输机制相互通信。 WebSocket 或任何其他端口(TCP、SSL 等)上的 STOMP 客户端可以发送到主题,只要协议支持消息的内容,AMQP、MQTT 或 OpenWire 客户端就可以接收消息,反之亦然AMQP、MQTT 或 OpenWire 发送和 STOMP 接收的情况。
你说的这个案例很标准。您发布的代码似乎正在使用 OpenWire 客户端发送到主题 "vrwrThreat"。连接工厂中使用的 URI 略有偏差,因为您添加了无意义的“/stomp”。由于您要发送到主题,因此您需要确保接收消息的客户端在传输时处于活动状态,否则消息将被丢弃。您还需要确保两者都在您的代码片段中不清楚的同一主题上运行。