2012-11-29 93 views
1

我收到了下面的jms消息。我構建了一個簡單的主要接收JMS消息,但問題是我無法過濾使用「選擇器」接收的JMS。JMS客戶端選擇器不工作

Message sent format 
<eventmsg> 
<event ucaname="UCA_Message" processApp="PDWEB">Message</event> 
<parameters> 
<parameter> 
<key>sessionKey</key> 
<value>123123</value> 
</parameter> 
</parameters> 
</eventmsg> 



import java.util.Hashtable; 

import javax.jms.JMSException; 
import javax.jms.Message; 
import javax.jms.Queue; 
import javax.jms.QueueConnection; 
import javax.jms.QueueConnectionFactory; 
import javax.jms.QueueReceiver; 
import javax.jms.QueueSession; 
import javax.jms.Session; 
import javax.jms.TextMessage; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 

public class JMSClient 
{ 

    public static void main(String[] args) 
    { 
     QueueSession qs = null; 
     QueueConnection qc = null; 
     QueueReceiver queueReceiver = null; 

     try 
     { 
      String JNDI_URL = "Myserver-server:2929"; 
      // The QUEUE_NAME is the name of the queue that receives the JMS message 
      String QUEUE_NAME = "jms/eventqueue"; 
      // USER and PASS to connect to the server, this can be removed by disabling security in 
      // the bus 
      String USER = "admin12"; 
      String PASSWORD = "admin12"; 

      String jndiUrl = "corbaname:iiop:" + JNDI_URL; 
      String initialContextFactory = "com.ibm.websphere.naming.WsnInitialContextFactory"; 
      String qcfName = "javax.jms.QueueConnectionFactory"; 
      String queueName = QUEUE_NAME; 

      // Kept as Hashtable, as the InitialContext constructor does not accept HashMap. 
      Hashtable<String, String> props = new Hashtable<String, String>(); 
      props.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 
      props.put(Context.PROVIDER_URL, jndiUrl); 
      Context ctx = new InitialContext(props); 

      // Lookup JMS queue 
      Queue errorQ = (Queue) ctx.lookup(queueName); 
      // Lookup QueueConnectionFactory and create QueueSession 
      QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup(qcfName); 
      // Creates the connection to the server using the admin and password set 
      qc = qcf.createQueueConnection(USER, PASSWORD); 
      qs = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 
      qc.start(); 
      String selector = "sessionKey = '123123'"; 

      queueReceiver = qs.createReceiver(errorQ,selector); 
      Message inMessage = queueReceiver.receive(); 

      String replyString = ((TextMessage) inMessage).getText(); 
      System.out.println(replyString); 
     } 

     catch (JMSException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (NamingException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try 
      { 
       queueReceiver.close(); 
       qs.close(); 
       qc.close(); 
      } 
      catch (JMSException e) 
      { 
       // log.error("Exception occured while Releasing JMS connection", e); 
      } 
     } 
    } 
} 
+0

什麼是sessionKey?這是一個自定義字段或什麼? – tjg184

+0

是的,這是一個自定義字段 –

回答

1

here

Message Selectors 

提取如果您的郵件應用程序需要過濾收到的消息,您可以使用JMS API消息選擇,它允許一個消息消費者指定它感興趣的消息消息選擇器將過濾消息的工作分配給JMS提供者而不是應用程序。有關使用消息選擇器的應用程序的示例,請參閱使用JMS API和Session Bean的J2EE應用程序。

消息選擇器是一個包含表達式的字符串。表達式的語法基於SQL92條件表達式語法的子集。

The message selector in the example selects any message that has a NewsType property that is set to the value 'Sports' or 'Opinion': 

NewsType = 'Sports' OR NewsType = 'Opinion' 

的createConsumer和createDurableSubscriber方法允許在創建消息消費者您作爲參數指定消息選擇。

The message consumer then receives only messages whose headers and properties match the selector. (See Message Headers, and Message Properties.) A message selector cannot select messages on the basis of the content of the message body. 
1

如果將sessionKey設置爲數字,您可以嘗試不帶引號的選擇器嗎?即sessionKey = 12345

+1

這似乎比第一個答案更容易成爲問題的原因。 –