Storing Received Messages in a Port Queue

Sometimes you may want your port to have a queue storing incoming messages. It is frequently needed, when messages are processed not just on arrival, but later on. Such sort of a port queue can be easily implemented using AnyLogic  collection, which acts as a container for incoming messages.

 To make a port store incoming messages

  1. Create a collection. Name it, say, messages. If your messages are of some particular type, specify this class as the collection's Elements class. 
  2. Select the port, whose incoming messages you want to store for further processing. To place the just arrived message in our messages collection, type the following code in the On receive property of the port:

    messages.add( msg );

    return false;

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. If you need to send it further, type return true; instead. 

  1. Incoming messages will be placed into our "port queue". You can work with the queue contents using standard Collection API.