Class MessageCollector<T1, T2, T3, T4>
This is a helper class that collects thread safe all messages until all message types are collected.
Inherited Members
Namespace: Agents.Net
Assembly: Agents.Net.dll
Syntax
public class MessageCollector<T1, T2, T3, T4> : MessageCollector<T1, T2, T3> where T1 : Message where T2 : Message where T3 : Message where T4 : Message
Type Parameters
Name | Description |
---|---|
T1 | |
T2 | |
T3 | |
T4 |
Remarks
The collector collects messages until all message types are collected. Afterwards it executes an action with the collected MessageCollection. In the following different scenarios are described, which explain the different aspects of this class.
Overriding messages
Assuming the message of type T1
was collected and T2
was not. When another message of type T1
is collected it replaces the old T1
message with the new message. No action is executed.
Considering message domains
Assuming the following MessageDomains and the collected messages:
--------------- ------------
| DefaultDomain | ----> | SubDomain1 |
--------------- | ------------
T1 Message1 | T1 Message2
| T2 Message4
|
| ------------
----> | SubDomain2 |
------------
T2 Message3
T2 Message5
Here is what happens in this scenario. Message1 is collected. Message2 is collected. This does not override Message1 because it is from a different MessageDomain. It is stored parallel. Message3 is collected. Now SubDomain2 has a complete set of Message1 + Message3. Message 4 is collected. Now there is another completed set in SubDomain1 of Message2 + Message4. Message5 is collected. This overrides Message3 as it is in the same MessageDomain. A new set is executed with Message1 + Message5. In the end the following sets were executed inorder:
- Message1 + Message3
- Message2 + Message4
- Message1 + Message5
Consumed vs not consumed messages
Messages can be consumed during execution with the method MarkAsConsumed(Message). The consumed message is removed from the message collector immediately. Looking at the example above the Message1 is used twice. Assuming during the first execution of Message1 + Message3 the agent executed collection.MarkAsConsumed(Message1)
. In this case the third execution Message1 + Message5 would not have happened, as Message1 would have been cleared from the collector instance.
Constructors
| Improve this Doc View SourceMessageCollector(Action<MessageCollection<T1, T2, T3, T4>>)
Declaration
public MessageCollector(Action<MessageCollection<T1, T2, T3, T4>> onMessagesCollected = null)
Parameters
Type | Name | Description |
---|---|---|
Action<MessageCollection<T1, T2, T3, T4>> | onMessagesCollected |
Properties
| Improve this Doc View SourceMessages4
Store for messages of type T4
.
Declaration
protected ConcurrentDictionary<MessageDomain, MessageStore<T4>> Messages4 { get; }
Property Value
Type | Description |
---|---|
ConcurrentDictionary<MessageDomain, MessageStore<T4>> |
Methods
| Improve this Doc View SourceAggregate(Message, Boolean)
Overridden by inheriting classes to try to add the message to the collector dictionaries.
Declaration
protected override bool Aggregate(Message message, bool throwError)
Parameters
Type | Name | Description |
---|---|---|
Message | message | The message to add |
Boolean | throwError | If set to |
Returns
Type | Description |
---|---|
Boolean |
|
Overrides
Execute(MessageCollection)
Overridden by inheriting classes to execute the typeless message collection cast to the specific collector type..
Declaration
protected override void Execute(MessageCollection messageCollection)
Parameters
Type | Name | Description |
---|---|---|
MessageCollection | messageCollection | The typeless collection. |
Overrides
IsCompleted(MessageDomain, out MessageCollection)
Overridden by inheriting classes to see if there is a completed set for the specified domain.
Declaration
protected override bool IsCompleted(MessageDomain domain, out MessageCollection messageCollection)
Parameters
Type | Name | Description |
---|---|---|
MessageDomain | domain | The domain which should be completed. |
MessageCollection | messageCollection | The message collection with the complete set. |
Returns
Type | Description |
---|---|
Boolean |
|
Overrides
PushAndContinue(Message, Action<MessageCollection<T1, T2, T3, T4>>, CancellationToken)
Add a message to the collector and continue with the specified action once when the set is found.
Declaration
public void PushAndContinue(Message message, Action<MessageCollection<T1, T2, T3, T4>> onCollected, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
Message | message | The message which is added to the collector. |
Action<MessageCollection<T1, T2, T3, T4>> | onCollected | The action which is executed when the complete set is found. |
CancellationToken | cancellationToken | Cancellation token to stop the continue operation. |
PushAndExecute(Message, Action<MessageCollection<T1, T2, T3, T4>>, CancellationToken)
Add a message to the collector and wait of the complete set to execute the specified action.
Declaration
public bool PushAndExecute(Message message, Action<MessageCollection<T1, T2, T3, T4>> onCollected, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
Message | message | The message which is added to the collector. |
Action<MessageCollection<T1, T2, T3, T4>> | onCollected | The action which is executed when the complete set is found. |
CancellationToken | cancellationToken | Cancellation token to stop the wait operation. |
Returns
Type | Description |
---|---|
Boolean |
|
Remarks
This method is helpful for InterceptorAgents where the agent in the InterceptCore(Message) method must wait for a set of message before returning the InterceptionAction.
Another example is when the InterceptorAgent wants to wait on a single message. In this case the first message is the message that is intercepted. The second message is the message the agent needs. The advantage is, that the collector respects message domains.
RemoveMessage(Message)
Overriden by inheriting classes to remove the message passed to the method.
Declaration
protected override void RemoveMessage(Message message)
Parameters
Type | Name | Description |
---|---|---|
Message | message | The message to remove from the collector. |