ActiveMQ message selector Selector

I. Introduction

  Message to Broker, consumers can subscribe to consume messages within a particular channel by Destination. In some special cases, consumers need to consume again next message filtering, that is, filter out certain messages. ActiveMQ provides SQL92 expression syntax

Custom message filtering capabilities. Very quick and easy to develop applications with message filtering.

  ActiveMQ support:

  1. Numeric expressions:  >, >=, <, <=, BETWEEN, =.
  2. Character expressions: =, <>, IN.
  3. IS NULL Or then  IS NOT NULL.
  4. Logic AND, logic OR, logic NOT.

  Constant Type:

  1. Digital: 3.1415926, 5.
  2. Character: 'a', it must have single quotes.
  3. NULLIn particular constants.
  4. Boolean: TRUE ,FALSE

Second, the program Case

  Producer:

package com.cfang.prebo.activemq.selector;

import java.util.Scanner;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {

    public static void main(String[] args) {
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        Message message = null;
        try {
            Scanner scanner = new Scanner(System.in);
            connectionFactory = new ActiveMQConnectionFactory("tcp://172.31.31.160:61618");
            connection = connectionFactory.createConnection(null, null);
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("TP_Q_TEST_SELECTOR00");
            producer = session.createProducer(destination);
            while(true) {
                String line = scanner.nextLine();
                if("exit".equals(line)) {
                    break;
                }
                message = session.createTextMessage(line);
                message.setIntProperty("applicationName", line.length());
                message.setStringProperty("result", "RT");
                producer.send(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(producer != null){ // 回收消息发送者
                try {
                    producer.close();
                } the catch (a JMSException E) { 
                    e.printStackTrace (); 
                } 
            } 
            IF (! the session = null ) { // recovery session object 
                the try { 
                    Session.close (); 
                } the catch (a JMSException E) { 
                    e.printStackTrace (); 
                } 
            } 
            IF (! connection = null ) { // recovery connection object 
                the try { 
                    Connection.close (); 
                } the catch (a JMSException E) { 
                    e.printStackTrace ();
                } 
            } 
        } 
    } 
    
}

  As above, the producer may be provided additional conditions, the ActiveMQ also provides a full type setXXXXXProperty basic approach to setting conditions.

  consumer:

package com.cfang.prebo.activemq.selector;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ConsumerA {

	public static void main(String[] args) {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageConsumer consumer = null;
		try {
			connectionFactory = new ActiveMQConnectionFactory("tcp://172.31.31.160:61618");
			connection = connectionFactory.createConnection(null, null);
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue("TP_Q_TEST_SELECTOR00");
			consumer = session.createConsumer(destination,"applicationName=2 and result='RT'");
			consumer.setMessageListener(new MessageListener() {
				public void onMessage(Message message) {
					System.out.println(message);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
	}
}

  As above, consumers consuming only applicationName = 2 and result = 'RT' message.

III Summary

  1, provides filtering capabilities, you can reduce the number of destination. It may be used to implement a particular machine, the specific message (gradation?).

  2, if two customers at the same time, then an exception can not be consumed, the message will have a backlog. Another normal for consumers, performance will decline, consumption may take longer.

Guess you like

Origin www.cnblogs.com/eric-fang/p/11433837.html