public override void Run() { Trace.WriteLine("Worker Role Started Running Now"); while (!_isStopped) { try { // Receive the message BrokeredMessage receivedMessage = null; receivedMessage = _clientToMonitor.Receive(); if (receivedMessage != null) { // Process the message Trace.WriteLine("Processing " + receivedMessage.SequenceNumber.ToString()); var theRequest = receivedMessage.GetBody<PrimeSumRequest>(); Trace.WriteLine("Received request to sum up promes between " + theRequest.StartNumber + " and " + theRequest.EndNumber); //start persistance in table storage for aggregator var persistRequest = new PersistPrimeSum(); var sumOfPrimesNew = persistRequest.GetCurrentSumOrPersistNew(theRequest.StartNumber, theRequest.EndNumber); for (int numberToAssign = theRequest.StartNumber; numberToAssign <= theRequest.EndNumber; numberToAssign++) { var findRequest = new PrimeFindRequest() { OriginalRequest = theRequest, TheNumberToCheck = numberToAssign }; _clientToAssign.Send(new BrokeredMessage(findRequest)); Trace.WriteLine("Assigned " + numberToAssign + " to be processed by Primer Number Finders"); } receivedMessage.Complete(); } Trace.WriteLine(">> No message waiting"); } catch (MessagingException e) { if (!e.IsTransient) { Debug.WriteLine("Failed during processing with error: " + e.ToString()); throw; } Thread.Sleep(5000); } catch (OperationCanceledException) { if (!_isStopped) { Debug.WriteLine("Cancelled during processing"); throw; } } } }
private void IfPrimeThenQueueToAggregator(PrimeFindRequest theFindRequest) { if (theFindRequest.TheNumberToCheck.IsPrime()) { Trace.WriteLine(theFindRequest.TheNumberToCheck + " is a primne number"); var thePrimeToAggregateRequest = new PrimeAggregateRequest() { OriginalRequest = theFindRequest.OriginalRequest, ThePrimeToAggregate = theFindRequest.TheNumberToCheck }; _clientToAssign.Send(new BrokeredMessage(thePrimeToAggregateRequest)); } }