Let's say I have objects:
ObjectA(
int ID,
List<ObjectB> items
)
ObjectB(
int ItemId,
string value
)
From RabbitMQ I fetch maximum of 10 messages, prefetchCount = 10
, where after deserialization every message becomes object of type ObjectA
.
One ObjectA
can have multiple ObjectB
with same ItemId
.
All that is done in .NET Core 3.1 background worker:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// code omitted for brevity
// register event that handles message received
consumer.Received += MessageReceived;
}
}
After deserialization for every ObjectA
, I run two tasks, taskA
and taskB
, which is not currently important.
Important is TaskA
that processes all items of ObjectA
- List of ObjectB
. taskA
also runs multiple tasks for every ObjectB
.
How can I ensure that across all tasks taskA
, or all RMQ messages currently received, only one ObjectB
with same ItemId
can be processed at the time?
Example:
ObjectA (Id = 1, Items = {(1, "a"), (2, "b"), (1, "c")})
;
For given object, I must start parallel processing of Items (1, "a")
and (2, "b")
while (1, "c")
should wait until Task that is processing (1, "a")
has done processing because their ItemId
is same.