예제 #1
0
        public IDispatcher RegisterConsumer <T>(IMesssageConsumer <T> consumer)
        {
            var consumerType = consumer.GetType();
            var messageType  = typeof(T);

            logger.Information("starting: adding consumer {0} for type {1}", consumerType.ToString(), messageType.ToString());
            addConsumer(consumer, messageType);
            addConsumerToDispose(consumer, consumerType.ToString());
            logger.Information("finished: adding consumer {0} for type {1}", consumerType.ToString(), messageType.ToString());
            return(this);
        }
예제 #2
0
 private void addConsumerToDispose <T>(IMesssageConsumer <T> consumer, string type)
 {
     if (consumerSet.All(i => i != consumer))
     {
         consumerSet.Add(consumer);
         consumersToDispose.Add(() => {
             try {
                 logger.Information(string.Format("starting: disposing message consumer, {0}", type));
                 consumer.Dispose();
                 logger.Information(string.Format("finished: disposing message consumer, {0}", type));
             } catch (Exception ex) {
                 logger.Error("An error occurred when disposing {0}. Error={1}", type, ex.ToString());
             }
         });
     }
 }
예제 #3
0
 private void addConsumer <T>(IMesssageConsumer <T> consumer, Type messageType)
 {
     if (!consumers.ContainsKey(messageType))
     {
         consumers.Add(messageType, new List <Action <object> >());
     }
     consumers[messageType].Add(async(object input) => {
         var json = JsonConvert.SerializeObject(input);
         try {
             logger.Information(string.Format("starting: Processing message: {0}", json));
             await consumer.ConsumeMessage((T)input);
             logger.Information(string.Format("finished: Processing message: {0}", json));
         } catch (Exception ex) {
             logger.Error(string.Format("Error when processing message <<{0}>> with error <<{1}>>", json, ex.ToString()));
         }
     });
 }