protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
 {
     configurator.Handler <PingMessage>(x =>
     {
         return(x.RespondAsync <PongMessage>(x.Message));
     });
 }
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            _received = GetTask <ConsumeContext <PingMessage> >();

            configurator.Handler <PingMessage>(async context =>
            {
                if (_timer == null)
                {
                    _timer = Stopwatch.StartNew();
                }

                if (_count++ < 2)
                {
                    Console.WriteLine("{0} now is not a good time", DateTime.UtcNow);

                    await context.Defer(TimeSpan.FromMilliseconds(1000), (consumeContext, sendContext) =>
                    {
                        _hit = true;
                    });

                    return;
                }

                _timer.Stop();

                Console.WriteLine("{0} okay, now is good (retried {1} times)", DateTime.UtcNow, context.Headers.Get("MT-Redelivery-Count", default(int?)));

                // okay, ready.
                _receivedTimeSpan = _timer.Elapsed;
                _received.TrySetResult(context);
            });
        }
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            _received = GetTask <ConsumeContext <PingMessage> >();

            configurator.Handler <PingMessage>(context =>
            {
                if (_timer == null)
                {
                    _timer = Stopwatch.StartNew();
                }

                if (_count++ < 2)
                {
                    Console.WriteLine("{0} now is not a good time", DateTime.UtcNow);
                    throw new IntentionalTestException("I'm so not ready for this jelly.");
                }

                _timer.Stop();

                Console.WriteLine("{0} okay, now is good (retried {1} times)", DateTime.UtcNow, context.Headers.Get("MT-Redelivery-Count", default(int?)));

                // okay, ready.
                _receivedTimeSpan = _timer.Elapsed;
                _received.TrySetResult(context);

                return(TaskUtil.Completed);
            }, x => x.UseDelayedRedelivery(r => r.Intervals(1000, 2000)));
        }
Beispiel #4
0
 protected void ConsumerTo <TEvent, TConsumer>(IRabbitMqReceiveEndpointConfigurator cfg, Type handlerType)
     where TConsumer : Subscription.IConsumer <TEvent>
     where TEvent : class
 {
     cfg.Handler <TEvent>(async evnt =>
     {
         try
         {
             await Task.Run(() =>
             {
                 var source = new TaskCompletionSource <TEvent>();
                 if (_beforeConsumer != null)
                 {
                     _beforeConsumer.Excuete(evnt.Message);
                 }
                 MethodInfo method = handlerType.GetMethod("HandleEvent", new Type[] { evnt.Message.GetType() });
                 method.Invoke(Activator.CreateInstance(handlerType, new object[] { }), new object[] { evnt.Message });
                 if (_afterConsumer != null)
                 {
                     _afterConsumer.Excuete(evnt.Message);
                 }
                 return(Task.FromResult(true));
             });
         }
         catch (Exception ex)
         {
             throw ex;
         }
     });
 }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            _received = GetTask<ConsumeContext<PingMessage>>();

            configurator.Handler<PingMessage>(async context =>
            {
                if (_timer == null)
                    _timer = Stopwatch.StartNew();

                if (_count++ < 2)
                {
                    Console.WriteLine("{0} now is not a good time", DateTime.UtcNow);
                    throw new IntentionalTestException("I'm so not ready for this jelly.");
                }

                _timer.Stop();

                Console.WriteLine("{0} okay, now is good (retried {1} times)", DateTime.UtcNow, context.Headers.Get("MT-Redelivery-Count", default(int?)));

                // okay, ready.
                _receivedTimeSpan = _timer.Elapsed;
                _received.TrySetResult(context);
            }, x => x.UseDelayedRedelivery(Retry.Intervals(1000, 2000)));
        }
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            configurator.Handler<MyMessage>(async context =>
            {
                await Console.Out.WriteLineAsync($"Received: {context.Message.Value}");

                await context.Publish(new MyMessage2 {Value = context.Message.Value + "  2"});
                await context.Publish(new MyMessage2()); // an exception will be caused by this action
            });
        }
Beispiel #7
0
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            base.ConfigureRabbitMqReceiveEndpoint(configurator);

            _consumer = new ReconnectConsumer(TestTimeout);

            _consumer.Configure(configurator);

            configurator.Handler <PingMessage>(context => context.RespondAsync(new PongMessage(context.Message.CorrelationId)));
        }
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            configurator.Handler <PingMessage>(context =>
            {
                Interlocked.Increment(ref _count);

                throw new IntentionalTestException();
            }, x => x.UseDelayedRedelivery(r => r.None()));
        }
Beispiel #9
0
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            configurator.ConfigureConsumeTopology = false;

            _count = 0;

            configurator.Handler <PingMessage>(context =>
            {
                Interlocked.Increment(ref _count);

                throw new IntentionalTestException();
            }, x => x.UseDelayedRedelivery(r => r.Intervals(100, 200)));
        }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _completed = GetTask<int>();
            int count = 0;

            configurator.PrefetchCount = 1000;

            configurator.Handler<PingMessage>(async context =>
            {
                if (Interlocked.Increment(ref count) == 100000)
                    _completed.TrySetResult(count);
            });
        }
Beispiel #11
0
        protected override void ConfigureRabbitMqReceiveEndoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _completed = GetTask <int>();
            int count = 0;

            configurator.PrefetchCount = 1000;

            configurator.Handler <PingMessage>(async context =>
            {
                if (Interlocked.Increment(ref count) == 100000)
                {
                    _completed.TrySetResult(count);
                }
            });
        }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            configurator.PrefetchCount = 100;

            configurator.Handler<PingMessage>(async context =>
            {
                try
                {
                    await context.RespondAsync(new PongMessage(context.Message.CorrelationId));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            });
        }
Beispiel #13
0
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            configurator.PrefetchCount = 100;

            configurator.Handler <PingMessage>(async context =>
            {
                try
                {
                    await context.RespondAsync(new PongMessage(context.Message.CorrelationId));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            });
        }
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            _received = GetTask <ConsumeContext <PingMessage> >();

            configurator.Handler <PingMessage>(async context =>
            {
                if (_count++ < 2)
                {
                    await context.Defer(TimeSpan.FromMilliseconds(2000));
                    return;
                }

                _received.TrySetResult(context);
            });
        }
Beispiel #15
0
        protected override void ConfigureRabbitMqReceiveEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            configurator.ConfigureConsumeTopology = false;

            _count = 0;

            _received = GetTask <ConsumeContext <PingMessage> >();

            configurator.Handler <PingMessage>(async context =>
            {
                if (_count++ == 0)
                {
                    await context.Defer(TimeSpan.FromMilliseconds(100), (consumeContext, sendContext) =>
                    {
                        _hit = true;
                    });

                    return;
                }

                _received.TrySetResult(context);
            });
        }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            base.ConfigureInputQueueEndpoint(configurator);

            configurator.Handler<PingMessage>(context => context.RespondAsync(new PongMessage(context.Message.CorrelationId)));
        }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            _received = GetTask<ConsumeContext<PingMessage>>();

            configurator.Handler<PingMessage>(async context =>
            {
                if (_timer == null)
                    _timer = Stopwatch.StartNew();

                if (_count++ < 2)
                {
                    Console.WriteLine("{0} now is not a good time", DateTime.UtcNow);

                    await context.Defer(TimeSpan.FromMilliseconds(1000));
                    return;
                }

                _timer.Stop();

                Console.WriteLine("{0} okay, now is good (retried {1} times)", DateTime.UtcNow, context.Headers.Get("MT-Redelivery-Count", default(int?)));

                // okay, ready.
                _receivedTimeSpan = _timer.Elapsed;
                _received.TrySetResult(context);
            });
        }
Beispiel #18
0
        /// <summary>
        /// 添加consumer
        // 消费者停止消费消息后,将异常抛出,让消息回滚至原队列中
        // 等待下次处理
        /// </summary>
        /// <typeparam name="TEvent">consumer的接收事件参数</typeparam>
        /// <typeparam name="TConsumer">consumer类型</typeparam>
        /// <param name="cfg">配置对象</param>
        private void ConsumerTo <TEvent, TConsumer>(IRabbitMqReceiveEndpointConfigurator cfg, IRabbitMqBusFactoryConfigurator fcg, Type handlerType)
            where TConsumer : IEventConsumer <TEvent>
            where TEvent : Event, new()
        {
            SetPublishConfig <TEvent>(fcg);

            cfg.Handler <TEvent>(async evnt =>
            {
                try
                {
                    if (evnt.Message == null)
                    {
                        Log.Write(string.Format("消息{0}对象为null,不进入消费逻辑", evnt.MessageId), MessageType.Warn, this.GetType());
                        return;
                    }
                    using (var scope = _reg.BeginLifetimeScope())
                    {
                        try
                        {
                            var beforeConsumer = scope.Resolve <IBeforeConsumer>();
                            if (beforeConsumer != null)
                            {
                                beforeConsumer.Execute(evnt.Message);
                            }
                        }
                        catch
                        {
                        }
                        scope.Resolve <IEventConsumer <TEvent> >().Consume(evnt.Message);
                        try
                        {
                            var afterConsumer = scope.Resolve <IAfterConsumer>();


                            if (afterConsumer != null)
                            {
                                afterConsumer.Execute(evnt.Message);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                catch (StopedConsumeException)
                {
                    await evnt.Publish(evnt.Message);
                }
                catch (RequeueException ex)
                {
                    ++evnt.Message.RetryCount;
                    Log.Write(string.Format("执行{0}错误,RequeueException,messageId:{1}", typeof(TConsumer), evnt.Message.Id), MessageType.Error, this.GetType(), ex);
                    throw;
                }
                catch (Exception ex)
                {
                    Log.Write(string.Format("执行{0}错误,messageId:{1}", typeof(TConsumer), evnt.Message.Id), MessageType.Error, this.GetType(), ex);
                    if (evnt.Message.ExceptionRequeue)
                    {
                        ++evnt.Message.RetryCount;
                        throw new RequeueException();
                    }
                    throw;
                }
            });
        }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            base.ConfigureInputQueueEndpoint(configurator);

            configurator.Handler <PingMessage>(context => context.RespondAsync(new PongMessage(context.Message.CorrelationId)));
        }
        protected override void ConfigureInputQueueEndpoint(IRabbitMqReceiveEndpointConfigurator configurator)
        {
            _count = 0;

            configurator.Handler<PingMessage>(context =>
            {
                Interlocked.Increment(ref _count);

                throw new IntentionalTestException();
            }, x => x.UseDelayedRedelivery(r => r.Intervals(100, 200)));
        }