Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From what I understand you want to constrain the number of threads used for processing a certain type of message at the same time.</p> <p>One approach would be to simply wrap the message processor, invoked on a new thread with something like</p> <pre><code>try { Interlocked.Increment(ref count) Process(message); } finally { Interlocked.Decrement(ref count) } </code></pre> <p>Before invoking the wrapper, simply check if the ‘count’ is less than your threshold count; and stop polling/handling more messages till the count is sufficiently lower.</p> <p><strong>EDIT</strong> Added more information based on comment</p> <p>Frans, not sure why you see the infrastructure and business code being coupled. Once you place your business process to be serviced as a task on a new thread to run asynchronously, you need not worry about performing additional IO bound calls asynchronously. This is a simpler model to program in.</p> <p>Here is what I am thinking.</p> <pre><code>// semi - pseudo-code // Infrastructure – reads messages from the queue // (independent thread, could be a triggered by a timer) while(count &lt; maxCount &amp;&amp; (message = Queue.GetMessage()) != null) { Interlocked.Increment(ref count); // process message asynchronously on a new thread Task.Factory.StartNew(() =&gt; ProcessWrapper(message)); } // glue / semi-infrastructure - deals with message deletion and exceptions void ProcessWrapper(Message message) { try { Process(message); Queue.DeleteMessage(message); } catch(Exception ex) { // Handle exception here. // Log, write to poison message queue etc ... } finally { Interlocked.Decrement(ref count) } } // business process void Process(Message message) { // actual work done here ; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload