JMS客户端选择器不起作用

时间:2012-11-29 18:44:22

标签: java jms

我收到以下jms消息。我构建了一个简单的main来接收JMS消息,但问题是我无法过滤使用“selector”接收到的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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

摘自here

 Message Selectors

如果您的消息传递应用程序需要过滤它收到的消息,您可以使用JMS API消息选择器,它允许消息使用者指定它感兴趣的消息。消息选择器将过滤消息的工作分配给JMS提供者而不是申请。有关使用消息选择器的应用程序的示例,请参阅使用带有会话Bean的JMS API的J2EE应用程序。

消息选择器是包含表达式的String。表达式的语法基于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 :(得分:1)

如果将sessionKey设置为数字,您可以尝试不带引号的选择器吗?即 sessionKey = 12345