我收到以下 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);
}
}
}
}