JMS Message Selector Selector__java history

Source: Internet
Author: User
Business scene: A system through a queue (assuming the queue name is QueueA) to the B system to send messages, then the business adjustment, B system split into two systems B1,B2, respectively, processing different business, and each need to obtain different messages from the QueueA. In order not to change the logic of a system, a system also sends different messages to b1,b2 through a queue. B1,B2 to automatically select the message that belongs to you, you can use the message filter. The specific approach is to specify the selector when creating the consumer.

       /** creates a <CODE>MessageConsumer</CODE> for the specified destination, * using a message Sele 
      ctor. * Since <CODE>Queue</CODE> and <CODE>Topic</CODE> * both inherit from <code>destinati On</code&gt, they can be used in * destination parameter to create a &LT;CODE&GT;MESSAGECONSUMER&LT;/CODE&GT
      ;. * <p>a Client uses A <CODE>MessageConsumer</CODE> object to receive * messages that have
      been sent to a destination. * * * @param destination The <CODE>Destination</CODE> to access * @param Messagesel Ector only messages with properties matching the * message selector expression are. 
      A value of NULL or * A empty string indicates that there was no message selector * for the message consumer.    * * * @exception JMSException If the session fails to create a messageconsumer *                     Due to some internal error.
     
      * @exception invaliddestinationexception If Invalid destination * is specified.
      * @exception invalidselectorexception if selector is invalid. * * @since 1.1/Messageconsumer Createconsumer (destination destination, java.lang.String Messa Geselector) throws JMSException;

The second parameter in the method above is the selector (also called a filter), which consists of three parts: identifiers, constants, and comparison operators. Where the identifier is the part of the comparison operator that is compared, the identifier must come from the message header or from the message property, it is case-sensitive, and must exactly match the property or the JMS message header name. In order to use the message selector, the producer of the message must specify a property or JMS message header information before sending the message. The following code is the custom attribute and message header for the producer Settings message, respectively. eg..

            Message.setjmscorrelationid ("123456789");
            Message.setstringproperty ("Tianwang", "Dihu");

The headers that can be used as identifiers have these:


The above Jmscorrelationid is used most frequently as identifiers and is often used as a JMS synchronous messaging communication. About the JMS synchronous communication mechanism can refer to spring's jmstemplate source code.


For a message with an identifier sent by the producer above, the code can write this when we create a consumer in session:

            Messageconsumer receiver = Session.createconsumer (mydest, "tianwang1 = ' Dihu '");
            Messageconsumer receiver1 = Session.createconsumer (mydest, "Jmsmessageid = ' 123456789 '");

About the format of selectors:

Message selectors are part of the syntax for SQL-92 conditional expressions. That means the second parameter of the code above is the one in the back of the SQL statement you normally write. You must know what to do. You can use statements such as =,like,not null.

Note is part of the SQL-92 conditional expression syntax. This means that some grammar is not supported. I found in the test that if you want to write not equal, you must use the "<>" symbol, and ". = "will be the error" Javax.jms.InvalidSelectorException: ". When you use it, you must test it yourself OK. The reason is that "!=" is not a standard SQL statement. Because only in some versions of SQL, operator <> can be written as!=.


Written at the end:

Using the message selector, we can choose the useful information we want, and make a choice before calling OnMessage, less consumer pressure and processing logic. This part of the selection work should be done by the JMS server.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.