Content Based Router

Content Based Router can route messages based on a specified list of rules. Each rule has a default set of destinations to send messages too that don't meet the criteria of the specified rule. Each rules has a routing predicate. The routing predicate makes the decision as too where the incoming message should be routed too. When a rule is matched the router sends the incoming message "as is" to the specified list of target destinations.

contentbasedrouter.gif

example: Routes messages to a list of specified destination queues based on the content of the incoming message.
  // Send a message to the "justa.messaging.routing.source" queue.
  IMessageGateway<string> gateway = new MessageGateway<string>("justa.messaging.routing.source");
  gateway.Send("route");

  // Setup a router with a simple rule that sends messages from the "justa.messaging.routing.source"
  // queue to the "justa.messaging.routing.source.routed" queue when the contents of the message are 
  // equal to "route" otherwise; send the message to the "justa.messaging.routing.source.notrouted" queue.
  //
  ContentBasedRouter<string> router = new ContentBasedRouter<string>("justa.messaging.routing.source");
  router.AddRule(new RoutingRule<string>(
          delegate(string message)
          {
              if (message.Equals("route"))
              {
                  return new string[] { "justa.messaging.routing.source.routed" };
              }
              
              return new string[] { "justa.messaging.routing.source.notrouted" };             
          }
      )
  );

  // Start the router and let it run for 1 second.
  router.Start();
  System.Threading.Thread.Sleep(1000);
  router.Stop();

  // Receive the expected message from the target destination "justa.messaging.routing.source.routed" 
  // defined in the routing rule.
  //
  IMessageGateway<string> receiver = new MessageGateway<string>("justa.messaging.routing.source.routed");
  string response = receiver.Receive(1000);

Last edited May 11, 2008 at 8:44 AM by aperez, version 3

Comments

No comments yet.