Esempio n. 1
0
 protected virtual void OnProcess(IUniqueMessage <IDType> item)
 {
     try
     {
         var msg = GetMessageAwaiter(item._UniqueID);
         if (msg == null)
         {
             OnNotfound(item);
         }
         else
         {
             if (item is Exception error)
             {
                 msg.Error(error);
             }
             else
             {
                 msg.Success(item);
             }
         }
     }
     catch (Exception e_)
     {
         OnProcessError(item, e_);
     }
 }
Esempio n. 2
0
        public void Push(IUniqueMessage message)
        {
            this.RecordMethodCall(message);

            this.messages.Add(message);

            if (this.ReceiveMessages && (this.MessageReceived != null))
            {
                ThreadPool.QueueUserWorkItem(state => this.MessageReceived(this, EventArgs.Empty));
            }
        }
Esempio n. 3
0
        public IUniqueMessage PopFirst()
        {
            this.RecordMethodCall();

            if (this.messages.Count == 0)
            {
                return(null);
            }

            IUniqueMessage result = this.messages[0];

            this.messages.Remove(result);

            return(result);
        }
        public void PopMasterCommandsQueue()
        {
            this.MessageBus.Provider.FlushAll();

            Guid taskProcessorId = Guid.NewGuid();

            ConfigurationChangedMasterCommand command1 = new ConfigurationChangedMasterCommand(taskProcessorId);

            this.MessageBus.MasterCommands.Add(command1);

            IUniqueMessage command2 = this.MessageBus.MasterCommands.PopFirst();

            Assert.IsNotNull(command2);
            Assert.IsInstanceOfType(command2, typeof(ConfigurationChangedMasterCommand));
            Assert.AreEqual(taskProcessorId, ((ConfigurationChangedMasterCommand)command2).TaskProcessorId);
        }
Esempio n. 5
0
 protected virtual void OnNotfound(IUniqueMessage <IDType> item)
 {
     try
     {
         if (AwaiterNotFound != null)
         {
             EventAwaiterNotFoundArgs e = new EventAwaiterNotFoundArgs();
             e.Source = item;
             AwaiterNotFound?.Invoke(this, e);
         }
     }
     catch (Exception e_)
     {
         OnError(item, NOTFOUND_PROCESS_ERROR, $"process {item._UniqueID} message notfound event error!", e_);
     }
 }
Esempio n. 6
0
        public Task <T> Request <T>(IUniqueMessage <IDType> item, IUniqueMessagePipe <IDType> messagePipe, int timeout = 10000)
        {
            AwaitMessage <IDType, T> result = new AwaitMessage <IDType, T>(item._UniqueID);

            result.TimeOut = TimeWatch.GetElapsedMilliseconds() + timeout;
            AddMessageAwaiter(result);
            messagePipe.AwaitBlock = this;
            try
            {
                messagePipe.Write(item);
            }
            catch (Exception e_)
            {
                ThrowBlockException(item, e_.Message, e_);
            }
            return(result.Task);
        }
Esempio n. 7
0
        /// <inheritdoc />
        public void Push(IUniqueMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (string.IsNullOrEmpty(message.MessageUniqueId))
            {
                throw new ArgumentException("MessageUniqueId is null or empty.".FormatInvariant(message), "message");
            }

            if (this.isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            Trace.WriteLine("ENTER: Adding message {0}:{1} to list '{2}' ...".FormatInvariant(message.GetType().Name, message.MessageUniqueId, RedisTaskProcessorMessageQueue.MessageQueueListKey));

            byte[] content = this.serializer.Serialize(message);

            if (this.serializer.CanDetermineEntityTypeFromContent)
            {
                this.provider.AddToList(RedisTaskProcessorMessageQueue.MessageQueueListKey, content);
            }
            else
            {
                string messageUniqueId = string.Join("$", message.GetType().Name, message.MessageUniqueId);

                using (IRedisTransaction transaction = this.provider.CreateTransaction())
                {
                    transaction.AddToList(RedisTaskProcessorMessageQueue.MessageQueueListKey, messageUniqueId);

                    transaction.SetHashValue(RedisTaskProcessorMessageQueue.MessageQueueContentKey, messageUniqueId, content);
                    transaction.SetHashValue(RedisTaskProcessorMessageQueue.MessageQueueContentKey, messageUniqueId + "$Type", RedisConverter.ToString(message.GetType(), false));

                    transaction.Commit();
                }
            }

            this.provider.PublishMessage(RedisTaskProcessorMessageQueue.MasterCommandsChannel, string.Empty);

            Trace.WriteLine("EXIT: Message {0}:{1} added to list '{2}'.".FormatInvariant(message.GetType().Name, message.MessageUniqueId, RedisTaskProcessorMessageQueue.MessageQueueListKey));
        }
Esempio n. 8
0
 public void ThrowBlockException(IUniqueMessage <IDType> id, string message, Exception innerError = null)
 {
     ThrowBlockException(id._UniqueID, message, innerError);
 }
Esempio n. 9
0
 private void OnProcessError(IUniqueMessage <IDType> item, Exception error)
 {
     OnError(item, PROCESS_MESSAGE_ERROR, $"process {item._UniqueID} message error!", error);
 }
Esempio n. 10
0
 public void Enqueue(IUniqueMessage <IDType> item)
 {
     mDispatchCenter.Enqueue(item, 3);
 }
Esempio n. 11
0
        /// <inheritdoc />
        public void ProcessMasterCommands()
        {
            Trace.WriteLine("ENTER: Processing master commands ...");

            if (this.isProcessingMasterCommands)
            {
                Trace.WriteLine("EXIT: Master commands are currently processes.");

                return;
            }

            this.isProcessingMasterCommands = true;

            while (true)
            {
                if (!this.isActive)
                {
                    Trace.WriteLine("EXIT: {0} deactivated.".FormatInvariant(this));

                    return;
                }

                IUniqueMessage command = this.messageBus.MasterCommands.PopFirst();

                if (command == null)
                {
                    Trace.WriteLine("EXIT: No more master commands to process.");

                    break;
                }

                Trace.WriteLine("Processing {0} ...".FormatInvariant(command));

                if (command is TaskSubmittedMasterCommand)
                {
                    this.OnTaskSubmittedMasterCommand((TaskSubmittedMasterCommand)command);
                }
                else if (command is TaskCancelCompletedMasterCommand)
                {
                    this.OnTaskCancelCompletedMasterCommand((TaskCancelCompletedMasterCommand)command);
                }
                else if (command is TaskFailedMasterCommand)
                {
                    this.OnTaskFailedMasterCommand((TaskFailedMasterCommand)command);
                }
                else if (command is TaskCompletedMasterCommand)
                {
                    this.OnTaskCompletedMasterCommand((TaskCompletedMasterCommand)command);
                }
                else if (command is ConfigurationChangedMasterCommand)
                {
                    this.OnConfigurationChanged((ConfigurationChangedMasterCommand)command);
                }
                else if (command is TaskProcessorRegisteredMasterCommand)
                {
                    this.OnTaskProcessorRegisteredMasterCommand((TaskProcessorRegisteredMasterCommand)command);
                }
                else
                {
                    Trace.TraceWarning("{0} is not supported and will be ignored.".FormatInvariant(command));
                }
            }

            this.isProcessingMasterCommands = false;

            Trace.WriteLine("EXIT: Master commands processed.");
        }
Esempio n. 12
0
 public IAwaitMessage <IDType> GetAwait(IUniqueMessage <IDType> message)
 {
     return(GetAwait(message._UniqueID));
 }