ActiveMQ message selector and asynchronous receive

1. Message selector
   Message selector: Filter messages whose message attributes are equal to the set conditions for consumption. The semantics are consistent with sql.
  
   private final String selector_1 = "sex='w'";
   this.consumer = session.createConsumer(destination, selector_1);
   


2. Asynchronous reception of
   messages: Asynchronous reception of messages: When a message arrives, ActiveMQ notifies the consumer actively. You can register a MessageListener class to implement the onMessage method to monitor the delivery of messages by MQ

Example :

public class Producer {
	// Create a connectionFactory factory object
	private ActiveMQConnectionFactory connectionFactory;
	// connection object
	private Connection connection;
	// session object
	private Session session;
	// producer
	private MessageProducer producer;

	public Producer() {
		this.connectionFactory = new ActiveMQConnectionFactory();
		try {
			this.connection = connectionFactory.createConnection("fu", "fu");
			this.connection.start();
			//Refer to one: open the transaction, refer to the second, manually sign for receipt
			this.session = connection.createSession(Boolean.TRUE, Session.CLIENT_ACKNOWLEDGE);
			this.producer = session.createProducer(null);

		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	public void send() throws Exception {
		Destination destination = this.session.createQueue("first");

		MapMessage map = this.session.createMapMessage();
		//set message
		map.setString("name", "zs");
		map.setString("age", "40");
		// set message properties
		map.setStringProperty("sex", "m");

		MapMessage map1 = this.session.createMapMessage();
		map1.setString("name", "ls");
		map1.setString("age", "20");
		map1.setStringProperty("sex", "w");
		
		MapMessage map2 = this.session.createMapMessage();
		map2.setString("name", "ww");
		map2.setString("age", "35");
		map2.setStringProperty("sex", "w");
		//Refer to the first target, refer to the second data, refer to the third non-persistence, refer to the fourth for priority, refer to the fifth failure time
		this.producer.send(destination, map, DeliveryMode.PERSISTENT, 2, 1000*10);
		this.producer.send(destination, map1, DeliveryMode.PERSISTENT, 2, 1000*10);
		this.producer.send(destination, map2, DeliveryMode.PERSISTENT, 9, 1000*10);

		// commit the transaction
        this.session.commit();
        //close the connection
        this.connection.close();
	}

	public static void main(String[] args) throws Exception {
		Producer p = new Producer();
		p.send();
	}

}




public class Comsumer {

	// private final String selector_0 = "age>30";
	// The message is not filtered by the message itself, but by some attributes attached to the filter
	private final String selector_1 = "sex='w'";

	// Create a connectionFactory factory object
	private ActiveMQConnectionFactory connectionFactory;
	// connection object
	private Connection connection;
	// session object
	private Session session;
	// producer
	private MessageConsumer consumer;
	// target address
	private Destination destination;

	public Comsumer() {
		this.connectionFactory = new ActiveMQConnectionFactory();
		try {
			this.connection = connectionFactory.createConnection("fu", "fu");
			this.connection.start();
			this.session = connection.createSession(Boolean.TRUE, Session.CLIENT_ACKNOWLEDGE);
			this.destination = this.session.createQueue("first");
			// The message is not filtered by the message itself, but by some attributes attached to the filter
			this.consumer = session.createConsumer(destination, selector_1);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	public void recever() throws Exception {
		// Asynchronous reception of messages: When a message arrives, ActiveMQ notifies the consumer actively, you can register a MessageListener class to implement the onMessage method to monitor the MQ delivery of messages
		this.consumer.setMessageListener(new Listener());

	}

	class Listener implements MessageListener {

		public void onMessage(Message message) {
			try {
				if (message instanceof TextMessage) {

				} else if (message instanceof MapMessage) {
					MapMessage m = (MapMessage) message;
					System.out.println(m.toString());
					System.out.println(m.getString("name"));
					System.out.println(m.getString("age"));
					// Manually sign for the message
					m.acknowledge();
				}

			} catch (JMSException e) {
				e.printStackTrace ();
			}
		}

	}

	public static void main(String[] args) throws Exception {
		Comsumer c = new Comsumer();
		c.recever();
	}
}


   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326355539&siteId=291194637