【Java.Web】利用Selector表达式过滤消息

在创建一个Consumer的时候,Session接口还提供了另一个方法:

 MessageConsumer	createConsumer(Destination destination, java.lang.String messageSelector) 
 //Creates a MessageConsumer for the specified destination, using a message selector.

这里messageSelector是一个字符串,用来过滤消息。也就是说,这种方式可以创建一个可以只接收特定消息的一个消费者

messageSelector的格式是类似于SQL-92的一种语法。可以用来比较消息头信息和属性。


使用示例:

在上面的示例中,创建了一个指定messageSelector的Consumer。当Producer发送消息时,会在消息Header中设置不同的消息属性。Consumer根据messageSelector表达式选择拥有指定属性的消息进行处理。

package com.gof.jms.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSSelectorTest {
	public static final String user = "system";
	public static final String password = "manager";
	public static final String url = "tcp://localhost:61616";
	public static final String queueName = "test_queue";
	public static final String messageBody = "Hello Asynchronous JMS!";
	public static final boolean transacted = false;
	public static final boolean persistent = false;
	
    public static void main(String[] args){
    	Connection connection = null;
    	Session session = null;
    	
    	try{
    		// create the connection
    	    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
            connection = connectionFactory.createConnection();
            connection.start();
            
            // create the session
            session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queueName);
            
            // create the producer
            MessageProducer producer = session.createProducer(destination);
            if (persistent){
            	producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            }else{
            	producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            }
            
            // create text message
            for (int i =0; i < 10; i++){
            	Message message = session.createTextMessage(messageBody + " " + Integer.toString(i));
            	String selectorStr = ((i+1) % 2 == 0) ? "myselector":"yourselector";
            	message.setStringProperty("selector", selectorStr);
            	
            	// send the message
                producer.send(message);
                System.out.println("Send message with string property selector =" + selectorStr +" : " + ((TextMessage)message).getText());
            }
            
            // create the consumer with the selector expression
            MessageConsumer consumer = session.createConsumer(destination, "selector='myselector'");
            consumer.setMessageListener(new MessageListener() {
				public void onMessage(Message message) {
					// TODO Auto-generated method stub
					TextMessage recvMessage = (TextMessage)message;
					try{
					System.out.println("Receive message: " + recvMessage.getText());
					}catch (JMSException e){
						e.printStackTrace();
					}
				}
			});
            // To avoid the connection closed before the message listener received the message.
            Thread.sleep(5000);
            
    	}catch (Exception e){
    		e.printStackTrace();
    	}finally{
    		try{
    			// close session and connection
    		    if (session != null){
    			    session.close();
    		    }
    		    if (connection != null){
    			    connection.close();
    		    }
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    }
}


执行的结果如下:

Send message with string property selector =yourselector : Hello Asynchronous JMS! 0
Send message with string property selector =myselector : Hello Asynchronous JMS! 1
Send message with string property selector =yourselector : Hello Asynchronous JMS! 2
Send message with string property selector =myselector : Hello Asynchronous JMS! 3
Send message with string property selector =yourselector : Hello Asynchronous JMS! 4
Send message with string property selector =myselector : Hello Asynchronous JMS! 5
Send message with string property selector =yourselector : Hello Asynchronous JMS! 6
Send message with string property selector =myselector : Hello Asynchronous JMS! 7
Send message with string property selector =yourselector : Hello Asynchronous JMS! 8
Send message with string property selector =myselector : Hello Asynchronous JMS! 9
Receive message: Hello Asynchronous JMS! 1
Receive message: Hello Asynchronous JMS! 3
Receive message: Hello Asynchronous JMS! 5
Receive message: Hello Asynchronous JMS! 7
Receive message: Hello Asynchronous JMS! 9



只有设置了消息头属性“selector”为“myselector”的消息才会被Consumer接收到。




你可能感兴趣的:(java,jms)