【问题标题】:Duplicate JMS message processing with multiple MessageListener threads使用多个 MessageListener 线程重复 JMS 消息处理
【发布时间】:2012-04-28 18:57:26
【问题描述】:

我正在创建一个侦听队列的 JMS 侦听器应用程序。我正在使用 TIBCO JMS 实施并面临一个问题,该问题间歇性地超过我的一个侦听器线程 拾取同一条消息并导致重复处理。

这是我创建连接的方式: . .. ...

Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.PROVIDER_URL, url);
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactoryClass);
env.put(Context.SECURITY_PRINCIPAL, userName);

String decryptedPass = null;
//Decryption logic

try {
    // Look up queue connection factory from naming context.
    if (log.isEnabledFor(Level.DEBUG)) {
        log.debug("Attempting to lookup queue connection factory at '" + 
                    this.url + "' as user '" + userName + "'.");
    }

    Context ctx = new InitialContext(env);

    QueueConnectionFactory factory = 
        (QueueConnectionFactory) ctx.lookup(connectionFactoryName);

    // Create JMS connection using the factory we just looked up.
    if (log.isEnabledFor(Level.DEBUG)) {
        log.debug("Creating queue connection as user '" + userName + "'.");
    }
    connection = factory.createQueueConnection(userName, decryptedPass);
    ...
    ..
    .

然后我在这里创建与上面创建的相同连接的侦听器线程

        //This is called in a loop.
                    // Create a JMS session that is non-transacted, but in client
        // acknowledge mode.  This will allow us to control when
        // messages are acknowledged.
        QueueSession session = 
            getQueueConnection().createQueueSession(
                false, Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE);

        // Create a receiver for the queue we are interested in.  Then
        // set the message listener defined on the outer class.  Messages
        // will be delivered on a dispatcher thread created by the
        // JMS provider.
        Queue queue = session.createQueue(getQueueName());
        session.createReceiver(queue).setMessageListener(getListener());
        ...
        ..
        .

现在让我们假设,创建了 5 个侦听器线程,它们在队列中作为接收者进行侦听。我看到了一种行为 有时不止一个侦听器线程/接收器接收到相同的消息,而我最终得到重复处理?我怎样才能 通过JMS配置处理?甚至可能吗?还是我不得不求助于一些程序化的解决方案?任何建议 将不胜感激。谢谢。

【问题讨论】:

    标签: multithreading jms duplicates message


    【解决方案1】:

    当消息被传递到应用程序时,该消息会从队列中隐藏(或对其他消费者不可用),直到应用程序确认为止。只有在应用程序确认该消息后,该消息才会从队列中删除。如果消费者在没有确认的情况下离开,消息将重新出现在队列中。因此,当一条消息被隐藏时,其他消费者将无法使用该消息。

    我在这里要强调的一点是:一条消息不应同时发送给多个消费者。您可能需要重新检查您的侦听器代码。

    【讨论】:

      【解决方案2】:

      尝试将 Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE 更改为 javax.jms.Session.AUTO_ACKNOWLEDGE

      或确保您的 Listener 确认收到消息。

      【讨论】:

        猜你喜欢
        • 2012-06-12
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多