2012-04-17 64 views
1

我正在创建一个在队列上侦听的JMS侦听器应用程序。我正在使用TIBCO JMS 实现,并面临一个问题,即我的多个侦听器线程 间歇性地选取相同的消息并导致重复处理。使用多个MessageListener线程处理重复的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配置来处理它?它甚至有可能吗?或者我将不得不求助于一些程序化解决方案?任何建议 将不胜感激。谢谢。

回答

0

尝试Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE改变javax.jms.Session.AUTO_ACKNOWLEDGE

,或者确保您监听承认有人receiveing消息。

2

当消息传递给应用程序时,该消息从队列中隐藏(或对其他消费者不可用),直到应用程序确认为止。只有在应用程序确认该消息后,消息才会从队列中删除。如果消费者未经确认就消失,消息将重新出现在队列中。所以当一条消息被隐藏时,那个消息就不会被其他消费者使用。

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