Пример #1
0
        /// <summary>
        /// Handles a response for the distributed task.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task Handle(QueueContext context, SampleDistributedTaskResponse message)
        {
            _log.Info($"Distributed Task Complete: {message.A} {message.Operation} {message.B} = {message.Result}");

            // update our saga data, keeping track of how much work is remaining.
            Data.PendingTasks--;
            _log.Info($"{Data.PendingTasks} Tasks Remaining.");

            if (Data.PendingTasks == 0)
            {
                // all tasks are complete, so we can inform the application
                // that this saga has finished.
                _log.Info($"Completing SagaId {message.AppId}");
                await _queueWriter.WriteMessage(context.SourceQueue,
                                                new SampleSagaComplete { AppId = message.AppId });

                SagaComplete = true;
            }
            else
            {
                // there is more work to do, so dispatch another distributed task.
                _log.Info($"Distributing more work for SagaId {message.AppId}");
                await _queueWriter.WriteMessage(context.SourceQueue,
                                                new SampleDistributedTaskRequest
                {
                    AppId     = message.AppId,
                    B         = (Data.PendingTasks - 1) * 3,
                    A         = (Data.PendingTasks - 1) * 4,
                    Operation = "+"
                });
            }

            // demonstrate interaction with our application database.
            await _dataAccess.Audit($"Pending Tasks {Data.PendingTasks}");
        }
Пример #2
0
        public async Task Handle(QueueContext context, SampleDistributedTaskRequest message)
        {
            // This handler does math on two values.
            // Not a realistic example, in that you wouldn't go through all the trouble of sending
            // a message to add numbers. The point is to demontrate assigning a task to be completed
            // by code which may be running in a different process or even on a different machine.

            _log.Info("Processing Distributed Task.");
            if (message.Operation != "+")
            {
                throw new ApplicationException($"I only know how to add!. I don't know how to {message.Operation}.");
            }

            // compute the response.
            var response = new SampleDistributedTaskResponse
            {
                AppId     = message.AppId,
                A         = message.A,
                B         = message.B,
                Operation = message.Operation,
                Result    = message.A + message.B
            };
            // send a response message
            await _queueWriter.WriteMessage(context.SourceQueue, response);

            // demonstrate interacting with our application data.
            var auditMessge = $"Distrbuted Task Result {response.A} { response.Operation} {response.B} =  {response.Result}";

            _log.Info(auditMessge);
            await _dataAccess.Audit(auditMessge);
        }
Пример #3
0
        public async Task Run()
        {
            const string QueueName = "SampleQueue";

            // Sends a few Saga Start messages to kick off the processing of messages in the example program.
            for (var i = 0; i < 10; i++)
            {
                _dataAccess.BeginTransaction();
                for (var j = 0; j < 10; j++)
                {
                    await _queueWriter.WriteMessage(QueueName, new SampleSagaStart { AppId = Guid.NewGuid() });
                }
                _dataAccess.CommitTransaction();
            }
        }
Пример #4
0
 /// <summary>
 /// Writes a message to a queue
 /// </summary>
 public static Task WriteMessage <T>(this IQueueWriter writer, string queueName, T message, DateTime?NotBefore = null)
 {
     return(writer.WriteMessage(queueName, typeof(T), message, NotBefore));
 }