Message Selectors

Message selectors are strings that conform to the standard ANSI SQL-92 syntax (found at http://www.ansi.org) after the WHERE criterion, and are used by JMS clients for filtering the messages delivered to them. They are evaluated using JMS properties and standard headers and can be specified using both standard and application-specific properties. Message selectors are specified when the message consumer is created. However, because the provider handles the filtering, the client itself is more efficient, and the messaging traffic imposes less bandwidth use.

The interface javax.jms.QueueSession defines a method to create a queue receiver with a specified message selector:

     public QueueReceiver createReceiver(Queue queue, String selector)                           throws JMSException, InvalidDestinationException,                                                 InvalidSelectorException 

The interface javax.jms.QueueSession defines a method to create a queue receiver with a specified message selector:

     public TopicSubscriber createSubscriber(Topic topic, String selector,            boolean noLocal) throws JMSException, InvalidDestinationException,                                                   InvalidSelectorException 

Example Selectors

This section explains a few examples for JMS message selectors. For a more in depth look at message selectors please refer to Appendix B.

A message consumer with the message selector below will be delivered only those messages having a string property named manager with the value as Vialli:

     manager = 'Vialli' 

A message consumer with the message selector below will be delivered only those messages having a string property named gender with the value as M AND a numeric property named salary with a value greater than 100:

     gender = 'M' AND salary > 100 

A message consumer with the message selector below will be delivered only those messages having a string property named gender with the value as M OR a numeric property named salary with a value greater than 100:

     gender = 'M' OR salary > 100 

A message consumer with the message selector below will be delivered only those messages having a string property named name with the value as either all or Dick:

     name in ('all', 'Dick') 

A message consumer with the message selector below will be delivered only those messages having a string property named name with values not starting with the letter J:

     name NOT LIKE 'J%' 

A message consumer with the message selector below will be delivered only those messages having the JMSType header set to the string XYZ:

     JMSType = 'XYZ' 
Note 

JMS message selectors support all the ANSI SQL-92 compliant combinations. Please refer to an ANSI SQL reference for an exhaustive list.

Message Selector Example

The sample application illustrates the use of message selectors. It is the same chat application discussed in the previous chapter.

The message panel provides an extra section of user interface for marking a message as private and specifying its intended recipient. All the messages carry a property named addressedTo with default value as all. Whenever a message is marked private the value of this property is changed to the intended recipient's name. The topic subscriber is created with the following message selector:

     addressedTo in ('all', '<user_name>') 

The <user_name> is the name of the current user. This means that only those messages with the property addressedTo set as either all or the name of the user using a particular instance of the chat application will be delivered to that instance. Hence the users can see only the messages addressed to all users and those specifically addressed to them.

The different class files used in the application are shown below. All of them are modified examples of those described the previous chapter.

The MessagePanel Class

The user interface now provides facilities to mark a message as private and specify its intended recipient. A checkbox is used to indicate whether a message is private and a text field ID used to specify the name of the intended recipient. All the lines of code listed below should be added to the original MessagePanel.java:

     import javax.swing.JCheckBox;     import javax.swing.JLabel;     private JCheckBox sendPrivate;     private JTextField recipientField;     recipientField = new JTextField(10);     messageField = new JTextField(30);     bottomPanel.add(new JLabel ("Private"));     bottomPanel.add(sendPrivate);     bottomPanel.add(recipientField); 

In the inner class for the action listener, before sending the message, depending on whether the sendPrivate checkbox is checked, either the string ‘all' or the contents of the recipientField text field is passed to the message sender:

     sendButton.addActionListener(new ActionListener() {        public void actionPerformed(ActionEvent e) {           if (!messageField.getText().equals ("")) {              try {                 String recAddr = sendPrivate.isSelected() ?                     recipientField.getText():"all";                 sender.sendMessage (user + ": " +                    messageField.getText(),                       recAddr);                 } catch (JMSException ex) {ex.printStackTrace();                       throw new RuntimeException (ex.getMessage());                 }              }           }        }     ); 

The MessageSender Class

The signature of the sendMessage() method is modified to take an extra argument to specify the intended recipient of the message. This can either be all users or a specific user name depending on whether the message is marked private or not. The method also sets a message property by the name addressedTo whose value is set to that of the argument recpAddr. The message receiver for implementing the message selector will use this property:

        public void sendMessage (String message, String recpAddr)              throws JMSException {           TextMessage outMsg = session.createTextMessage();           outMsg.setStringProperty("addressedTo",recpAddr);           outMsg.setText (message);           publisher.publish (outMsg);        }     } 

The MessageReceiver Class

The constructor of this class is changed to accept the name of the user using the current instance of the chat application. This value is used to define the message selector for the topic subscriber.

If the name of the user is Tom the selector will be evaluated to:

     addressedTo in ('all', 'Tom') 

This means the topic subscriber can receive only those messages having a property named addressedTo whose value is either Tom or all:

        public MessageReceiver(Topic topic, TopicSession session, String user)              throws JMSException {           this.topic = topic;           this.session = session;           String selector = "addressedTo in ('all',' " + user + "')";           subscriber = session.createSubscriber (topic, selector, false);           subscriber.setMessageListener (this);        } 

The ChatApp Class

The only change made to this class is the modified call to the message receiver's constructor:

           MessageReceiver receiver = new MessageReceiver (topic, sess, user); 

Running the Application

Run the configuration script from the previous chapter (config.bat from the download) to store the required Administered objects. Invoke the Java interpreter on the class ChatApp with all the required classes in the classpath.

The screen shots opposite show three users named Tom, Dick, and Harry using different instances of the chat application. Harry sends the message ‘Hi all' to all, which is displayed on everyone's screen, whereas Tom sends the message ‘Hi Dick' marked private to Dick, which is displayed only on Dick's screen:

click to expand



Professional JMS
Professional JMS
ISBN: 1861004931
EAN: 2147483647
Year: 2000
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net