-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks.Dataflow; | ||
|
||
namespace Apache.NMS.AMQP | ||
{ | ||
internal class SessionDispatcher | ||
{ | ||
private ActionBlock<NmsMessageConsumer.MessageDeliveryTask> actionBlock; | ||
private int dispatchThreadId; | ||
private readonly CancellationTokenSource cts; | ||
|
||
public SessionDispatcher() | ||
{ | ||
cts = new CancellationTokenSource(); | ||
actionBlock = new ActionBlock<NmsMessageConsumer.MessageDeliveryTask>(HandleTask, new ExecutionDataflowBlockOptions | ||
{ | ||
EnsureOrdered = true, | ||
MaxDegreeOfParallelism = 1, | ||
CancellationToken = cts.Token | ||
}); | ||
} | ||
|
||
public void Post(NmsMessageConsumer.MessageDeliveryTask task) => actionBlock.Post(task); | ||
|
||
public bool IsOnDeliveryThread() => dispatchThreadId == Thread.CurrentThread.ManagedThreadId; | ||
|
||
private void HandleTask(NmsMessageConsumer.MessageDeliveryTask messageDeliveryTask) | ||
{ | ||
try | ||
{ | ||
dispatchThreadId = Thread.CurrentThread.ManagedThreadId; | ||
messageDeliveryTask.DeliverNextPending(); | ||
} | ||
finally | ||
{ | ||
dispatchThreadId = -1; | ||
} | ||
} | ||
|
||
public void Stop() | ||
{ | ||
cts.Cancel(); | ||
cts.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters