Regular Expression Subscriptions

Regular Expression (Regex) subscriptions allow a regular expression to be supplied in the place of a topic name. When you supply a regular expression, it is as if a subscription is made to every topic that matches your expression, including topics that do not yet exist at the time of creating the subscription.

To use a regular expression, simply supply the regular expression in place of the topic name in the subscribe() call. For example:


// Provided for reference
public class MessagePrinter implements MessageHandler {
  public void invoke(Message m) {
      System.out.println(m.getTopic() + ": " + m.getData());
  }

}


class MyApp
{
   public static void main(String[] args)
   {

    // Assumes client is a connected and logged on
    // Client or HAClient.

    ...
    CommandId id = client.subscribe(
       new MessagePrinter, // MessageHandler implementation
       "orders.*", // Topic with a Regex
       5000); // Timeout
      ...
   }

}

In this example, messages on topics orders-north-america, orders-europe, and new-orders would match the regular expression. Messages published to any of those topics will be sent to the MessagePrinter passed to the subscribe command. As in the example, you can use the getTopic() method to determine the actual topic of the message sent to the function.

Last updated