2012-04-17 71 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

當消息傳遞給應用程序時,該消息從隊列中隱藏(或對其他消費者不可用),直到應用程序確認爲止。只有在應用程序確認該消息後,消息纔會從隊列中刪除。如果消費者未經確認就消失,消息將重新出現在隊列中。所以當一條消息被隱藏時,那個消息就不會被其他消費者使用。

我想在這裏指出的一點是:一條消息不應該同時給予多個消費者。您可能需要重新檢查您的偵聽器代碼。