Filtering Messages by Contents

Sometimes you may need to filter incoming in a port messages by contents – i.e. accept only messages containing the required information, or matching the specified format, and discard other ones. 

In AnyLogic you can easily implement any message contents checking mechanism in the On Receive property of the port. Here you can specify your own contents checking code and ignore or accept the arrived message by writing return false; or return true; statement correspondingly. 

 To make a port filter incoming messages by contents

  1. For instance, we have a client-server model, where a server processes only valid client requests containing customer name and address. (Request is represented with Java class Request with String fields name and address). We want requests with some of the required fields unfilled to be ignored. 
  2. Select the port, whose incoming messages you want to filter by contents. 
  3. Specify Request as port's In message type
  4. Define message contents checking operations in the On Receive property of the port. In our case your code should look like this:

    if ( msg.name==null || msg.address==null )
      return false;
    else {
      // process the request
      return true;
    }

Note that returning false we prohibit the default processing of the message. Thus received messages will not be forwarded further according to the message routing rules