1

我收到以下 jms 消息。我构建了一个简单的 main 来接收 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);
            }
        }
    }
}
4

2 回答 2

1

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

于 2012-11-29T23:37:29.733 回答
1

从这里提取

 Message Selectors

如果您的消息传递应用程序需要过滤它收到的消息,您可以使用 JMS API 消息选择器,它允许消息使用者指定它感兴趣的消息。消息选择器将过滤消息的工作分配给 JMS 提供者,而不是分配给应用程序。有关使用消息选择器的应用程序示例,请参阅将 JMS API 与会话 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. 
于 2012-11-29T18:51:06.557 回答