我已经使用 Java
和 Stomp.js
实现了 ActiveMQ-Pub/Sub
程序。只有一个生产者是用 Java
编写的,消费者是用 js
编写的。
这是问题场景,
- Producer keep 向主题发布消息。
- 消费者
A
连接并订阅相同的主题。 - 消费者
B
连接并订阅相同的主题。 - 现在
A
和B
同时收听同一个topic,但是A
收到的数据和B
接收,B
跳过一些数据。 - 当我断开
A
时,B
工作正常。 - 当我断开
B
时,A
工作正常。
这是生产者代码
public static Session SESSION;
/**
*
* @return @throws JMSException
*/
public static Session getSessionInstance() throws JMSException, IOException {
if (null == SESSION) {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Context.getSystemProperties().getAmp().getUrl());
Connection connection = connectionFactory.createConnection();
connectionFactory.getPrefetchPolicy().setAll(1);
connection.start();
SESSION = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
return SESSION;
}
/**
*
* @param message
* @throws JMSException
*/
public static void sendMessage(String topic, String message) throws JMSException, IOException {
Session session = getSessionInstance();
Destination destination = session.createQueue(topic);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage txtMessage = session.createTextMessage(message);
producer.send(txtMessage);
producer.close();
}
和消费者
var client = Stomp.client("ws://localhost:61614?consumer.prefetchSize=1", "v11.stomp");
client.debug = null;
var selectedVehicleImei = 741852963123456;
client.connect("", "", function (topic) {
client.subscribe("COO." + selectedVehicleImei, function (message) {
var infodata = JSON.parse(message.body);
console.log(infodata);
})
})
我已经尝试将 pre-fetch
值设置为,
在制作人中,
connectionFactory.getPrefetchPolicy().setAll(1);
在消费者中,
?consumer.prefetchSize=1
但仍然没有运气,这里有什么问题,谁能告诉我如何才能完成这项工作?
最佳答案
我认为您需要使用 createTopic 而不是 CreateQueue。即使您调用了变量主题,它看起来也是一个队列。
队列用于只需要由一个消费者回答的任务(例如发送电子邮件)。
关于javascript - 第二个消费者未收到 ActiveMQ 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49261178/