在第二个消费者中不接收ActiveMQ消息

时间:2018-03-13 16:25:02

标签: javascript java websocket activemq

我已使用ActiveMQ-Pub/SubJava实施了Stomp.js计划。只有一个生成器用Java编写,消费者用js编写。

这是问题情景,

  • 制作人不断向主题发布消息。
  • 消费者A连接并订阅同一主题。
  • 消费者B连接并订阅同一主题。
  • 现在AB同时收听同一主题,但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

但仍然没有运气,这里的问题是什么,谁能告诉我怎样才能让这项工作成功?

1 个答案:

答案 0 :(得分:1)

我认为您需要使用createTopic而不是CreateQueue。即使您调用了变量主题,它看起来像是一个队列。

队列适用于只需要由一个消费者回答的任务(如发送电子邮件)。

相关问题