Beispiel #1
0
        public void SetUp()
        {
            messageProcessorMock    = new Mock <IProcessMessages>();
            subscriptionManagerMock = new NotifyTopicSubscriptionsMock();

            testee = new EventConsumer(subscriptionManagerMock, messageProcessorMock.Object);
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            //string brokerList = "138.91.140.244";//d4
            string brokerList = "40.118.249.252";//b2
            //string brokerList = "localhost";
            List <String> topicList = new List <string>();

            topicList.Add("testBench24");
            var topics = topicList;

            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };
            DateTime      origin    = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            List <Double> latencies = new List <Double>();

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    //Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                    String receivedMessage = text;

                    String[] substrings    = receivedMessage.Split('_');
                    String   id            = substrings[0];
                    String   sentTimestamp = substrings[1];

                    TimeSpan diff = DateTime.Now.ToUniversalTime() - origin;
                    String   receivedTimestamp = String.Format(Math.Floor(diff.TotalMilliseconds).ToString());

                    String finalMessage = String.Format(id + "_" + sentTimestamp + "_" + receivedTimestamp);
                    //Console.WriteLine("Payload on Consumer side == " + finalMessage);

                    Double latency = Convert.ToDouble(receivedTimestamp) - Convert.ToDouble(sentTimestamp);
                    //Console.WriteLine("Payload on Consumer side == " + finalMessage + ", latency == " + latency);

                    latencies.Add(latency);

                    //printing only on the nearest 100th device, since frequent reporting adds to latency
                    if (latencies.Count == 1000000)
                    {
                        PrintLatencyReport(latencies);
                    }
                    //Console.WriteLine("");
                    //Console.WriteLine(latencies.Count);
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset(topics.First(), 0, 5)
                });
                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                //Console.ReadLine();
                //PrintLatencyReport(latencies);
                Console.ReadLine();
            }
        }
Beispiel #3
0
        public KafkaConsumer(string brokerlist, string topic)
        {
            var config = new Config {
                GroupId = "group"
            };

            _consumer  = new EventConsumer(config, brokerlist);
            _topicName = topic;
            _i         = 0;

            _consumer.OnMessage += (sender, message) =>
            {
                if (message.Payload.Length <= 0)
                {
                    return;
                }

                _i++;

                if (_i % 5000 == 0)
                {
                    var msg = common.Message.Deserialize(message.Payload);
                    Console.WriteLine($"Topic {_topicName} received msg: {msg.Title} - {msg.Body} - {msg.DeviceId}");
                }
            };

            //_consumer.Subscribe(new List<string>{topic});
            _consumer.Assign(new List <TopicPartitionOffset> {
                new TopicPartitionOffset(topic, 0, Offset.End)
            });
            _consumer.Start();
        }
        public static void Main(string[] args)
        {
            string brokerList = "localhost";
            var    topics     = args.Skip(1).ToList();

            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset("topicName", 0, 5)
                });
                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }
        }
Beispiel #5
0
        private static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureHostConfiguration(configurationBuilder => {
            configurationBuilder.AddCommandLine(args);
        })
        .ConfigureAppConfiguration((ctx, builder) =>
        {
            builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
        })
        .UseSerilog((ctx, cfg) =>
        {
            var credentials = new NoAuthCredentials(ctx.Configuration.GetConnectionString("loki"));

            cfg.MinimumLevel.Verbose()
            .Enrich.FromLogContext()
            .Enrich.WithProperty("Application", ctx.HostingEnvironment.ApplicationName)
            .Enrich.WithProperty("Environment", ctx.HostingEnvironment.EnvironmentName)
            .WriteTo.Console(new RenderedCompactJsonFormatter())
            .WriteTo.LokiHttp(credentials);
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.AddSingleton <IEventDeserializer>(new JsonEventDeserializer(new[]
            {
                typeof(CustomerCreated).Assembly
            }));

            services.AddHttpClient <ICustomersApiClient, CustomersApiClient>("customersApiClient", (ctx, httpClient) =>
            {
                var config             = ctx.GetRequiredService <IConfiguration>();
                var endpoint           = config["CustomersApi"];
                httpClient.BaseAddress = new System.Uri(endpoint);
            })
            .AddPolicyHandler(HttpClientPolicies.GetRetryPolicy());

            services.AddSingleton <INotificationsFactory, NotificationsFactory>();
            services.AddSingleton <INotificationsService, FakeNotificationsService>();

            services.AddSingleton(ctx =>
            {
                var kafkaConnStr    = hostContext.Configuration.GetConnectionString("kafka");
                var eventsTopicName = hostContext.Configuration["eventsTopicName"];
                var groupName       = hostContext.Configuration["eventsTopicGroupName"];
                return(new EventConsumerConfig(kafkaConnStr, eventsTopicName, groupName));
            });

            services.AddHostedService(ctx =>
            {
                var logger               = ctx.GetRequiredService <ILogger <EventConsumer <Account, Guid> > >();
                var eventsDeserializer   = ctx.GetRequiredService <IEventDeserializer>();
                var consumerConfig       = ctx.GetRequiredService <EventConsumerConfig>();
                var notificationsFactory = ctx.GetRequiredService <INotificationsFactory>();
                var notificationsService = ctx.GetRequiredService <INotificationsService>();
                var consumer             = new EventConsumer <Account, Guid>(eventsDeserializer, consumerConfig, logger);

                return(new AccountEventsWorker(notificationsFactory, notificationsService, consumer, logger));
            });
        });
        protected virtual void Dispose(bool disposing)
        {
            this.disposing = disposing;

            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                    if (consumer != null)
                    {
                        Logger.Debug("Disposing " + consumer.Name);

                        consumer.Stop().Wait();
                        consumer.OnPartitionsAssigned -= Consumer_OnPartitionsAssigned;
                        consumer.OnPartitionsRevoked  -= Consumer_OnPartitionsRevoked;
                        consumer.OnEndReached         -= Consumer_OnEndReached;
                        consumer.Dispose();
                    }

                    consumer = null;
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
        public static void Main(string[] args)
        {
            //string brokerList = "40.118.249.252";
            string brokerList = "138.91.140.244";
            //string brokerList = "localhost";
            List <String> topicList = new List <string>();

            topicList.Add("testLikeWin");
            var topics = topicList;

            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };

            using (var consumer = new EventConsumer(config, brokerList))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset(topics.First(), 0, 5)
                });
                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }
        }
Beispiel #8
0
 private Task RunConsumer() =>
 EventConsumer.ConsumeBatch(
     0,
     100,
     $"http://localhost:{mocksPort}/specialoffers",
     $"http://localhost:{mocksPort}"
     );
Beispiel #9
0
        public EventConsumerTest()
        {
            this.employeeMock = new Mock <IEmployeeControl>();
            this.employeeMock.Setup(control => control.AssignEmployee(It.Ref <WorkplaceDto> .IsAny, It.Ref <WorkplaceSkill> .IsAny));

            this.eventConsumer = new EventConsumer(this.employeeMock.Object, Configuration);
        }
Beispiel #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void awaitingShutdownMustBlockUntilAllMessagesHaveBeenProcessed() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void AwaitingShutdownMustBlockUntilAllMessagesHaveBeenProcessed()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Event specialShutdownObservedEvent = new Event();
            Event specialShutdownObservedEvent = new Event();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch awaitStartLatch = new java.util.concurrent.CountDownLatch(1);
            System.Threading.CountdownEvent awaitStartLatch = new System.Threading.CountdownEvent(1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final EventConsumer consumer = new EventConsumer();
            EventConsumer consumer = new EventConsumer();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final AsyncEvents<Event> asyncEvents = new AsyncEvents<>(consumer, AsyncEvents.Monitor_Fields.NONE);
            AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);

            _executor.submit(asyncEvents);

            // Wait for the background thread to start processing events
            do
            {
                asyncEvents.Send(new Event());
            } while (consumer.EventsProcessed.take().processedBy == Thread.CurrentThread);

            // Start a thread that awaits the termination
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.concurrent.Future<?> awaitShutdownFuture = executor.submit(() ->
            Future <object> awaitShutdownFuture = _executor.submit(() =>
            {
                awaitStartLatch.Signal();
                asyncEvents.AwaitTermination();
                consumer.EventsProcessed.offer(specialShutdownObservedEvent);
            });

            awaitStartLatch.await();

            // Send 5 events
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());

            // Observe 5 events processed
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));

            // Observe no events left
            assertThat(consumer.EventsProcessed.poll(20, TimeUnit.MILLISECONDS), @is(nullValue()));

            // Shutdown and await termination
            asyncEvents.Shutdown();
            awaitShutdownFuture.get();

            // Observe termination
            assertThat(consumer.EventsProcessed.take(), sameInstance(specialShutdownObservedEvent));
        }
        public void Start()
        {
            var config = new Config()
            {
                GroupId = "simple-csharp-consumer"
            };

            using (var consumer = new EventConsumer(config, "")) {
                consumer.OnMessage += (obj, msg) => {
                    string text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Assign(new List <TopicPartitionOffset> {
                    new TopicPartitionOffset(topics.First(), 0, 5)
                });

                consumer.Start();

                Console.WriteLine("Started consumer, press enter to stop consuming");
                Console.ReadLine();
            }


            Console.WriteLine("Starting...");
        }
    static void Main(string[] args)
    {
        var ec = new EventConsumer();
        var wr = new WeakReference <EventHandler <EventArgs> >(ec.EventReceived);
        EventHandler <EventArgs> target;

        if (wr.TryGetTarget(out target))
        {
            Console.WriteLine("Raising event");
            target(null, EventArgs.Empty);
        }
        // Clear the reference
        target = null;
        Console.WriteLine("Calling System.GC.Collect");
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.WaitForFullGCComplete();
        GC.Collect();
        EventHandler <EventArgs> target2;

        if (wr.TryGetTarget(out target2))
        {
            Console.WriteLine("Raising event");
            target2(null, EventArgs.Empty);
        }
        Console.ReadKey();
    }
Beispiel #13
0
        public static void Main(string[] args)
        {
            using (var producer = new Producer("localhost:9092"))
                using (var topic = producer.Topic("test"))
                {
                    var data           = Encoding.UTF8.GetBytes("Hello Kafka");
                    var deliveryReport = topic.Produce(data).Result;
                    Console.WriteLine($"Produced to Partition: {deliveryReport.Partition}, Offset: {deliveryReport.Offset}");
                }

            var config = new Config {
                GroupId = "test-consumer"
            };

            using (var consumer = new EventConsumer(config, "localhost:9092"))
            {
                consumer.OnMessage += (obj, msg) =>
                {
                    var text = Encoding.UTF8.GetString(msg.Payload, 0, msg.Payload.Length);
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {text}");
                };

                consumer.Subscribe(new List <string> {
                    "test"
                });
                consumer.Start();

                Console.ReadLine();
            }
        }
Beispiel #14
0
        public EventConsumerTest()
        {
            this.fairyTaleMock = new Mock <IFairyTaleControl>();
            this.fairyTaleMock.Setup(control => control.HandleVisitorArrivingAtFairyTale(It.Ref <Guid> .IsAny));

            this.eventConsumer = new EventConsumer(this.fairyTaleMock.Object, Configuration);
        }
Beispiel #15
0
 public virtual void OnTrigger(Event triggeredEvent)
 {
     if (EventConsumer != null)
     {
         EventConsumer.Invoke(triggeredEvent);
     }
 }
        public void SetUp()
        {
            messageProcessorMock = new Mock<IProcessMessages>();
            subscriptionManagerMock = new NotifyTopicSubscriptionsMock();

            testee = new EventConsumer(subscriptionManagerMock, messageProcessorMock.Object);
        }
Beispiel #17
0
    public Program()
    {
        EventProducer producer  = new EventProducer();
        EventConsumer consumer1 = new EventConsumer(producer);
        EventConsumer consumer2 = new EventConsumer(producer);

        producer.OnMyEvent("Event fired...");
    }
Beispiel #18
0
        public EventConsumerTest()
        {
            this.visitorMock = new Mock <IVisitorControl>();
            this.visitorMock.Setup(control => control.GetVisitor(It.IsAny <Guid>())).Returns(new Visitors.Entities.Visitor());
            this.visitorMock.Setup(control => control.UpdateVisitorAvailabilityAt(It.IsAny <Guid>(), It.Ref <DateTime> .IsAny));

            this.eventConsumer = new EventConsumer(this.visitorMock.Object, Configuration);
        }
Beispiel #19
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void concurrentlyPublishedEventsMustAllBeProcessed()
        internal virtual void ConcurrentlyPublishedEventsMustAllBeProcessed()
        {
            assertTimeout(ofSeconds(10), () =>
            {
                EventConsumer consumer = new EventConsumer();
                System.Threading.CountdownEvent startLatch = new System.Threading.CountdownEvent(1);
                const int threads               = 10;
                const int iterations            = 2_000;
                AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);
                _executor.submit(asyncEvents);

                ExecutorService threadPool = Executors.newFixedThreadPool(threads);
                ThreadStart runner         = () =>
                {
                    try
                    {
                        startLatch.await();
                    }
                    catch (InterruptedException e)
                    {
                        throw new Exception(e);
                    }

                    for (int i = 0; i < iterations; i++)
                    {
                        asyncEvents.Send(new Event());
                    }
                };
                for (int i = 0; i < threads; i++)
                {
                    threadPool.submit(runner);
                }
                startLatch.countDown();

                Thread thisThread = Thread.CurrentThread;
                int eventCount    = threads * iterations;
                try
                {
                    for (int i = 0; i < eventCount; i++)
                    {
                        Event @event = consumer.Poll(1, TimeUnit.SECONDS);
                        if (@event == null)
                        {
                            i--;
                        }
                        else
                        {
                            assertThat(@event.ProcessedBy, @is(not(thisThread)));
                        }
                    }
                }
                finally
                {
                    asyncEvents.Shutdown();
                    threadPool.shutdown();
                }
            });
        }
 public PretendApplication(
     IShipmentReadModelRepository shipmentReadModelRepository,
     ICommandDispatcher commandDispatcher,
     EventConsumer eventConsumer)
 {
     ShipmentReadModelRepository = shipmentReadModelRepository;
     _commandDispatcher          = commandDispatcher;
     EventConsumer = eventConsumer;
 }
        public async Task Should_fan_out_published_messages()
        {
            var endpointSettings = new EndpointSettings <IEndpointDefinition <EventConsumer> > {
                InstanceId = "27"
            };
            var endpointDefinition = new ConsumerEndpointDefinition <EventConsumer>(endpointSettings);

            var firstHarness  = new RabbitMqTestHarness();
            var firstConsumer = new EventConsumer(firstHarness.GetTask <ConsumeContext <SomeEvent> >());

            firstHarness.OnConfigureRabbitMqBus += configurator =>
            {
                configurator.ReceiveEndpoint(endpointDefinition, KebabCaseEndpointNameFormatter.Instance, e =>
                {
                    e.Consumer(() => firstConsumer);
                });
            };

            await firstHarness.Start();

            try
            {
                endpointSettings = new EndpointSettings <IEndpointDefinition <EventConsumer> > {
                    InstanceId = "42"
                };
                endpointDefinition = new ConsumerEndpointDefinition <EventConsumer>(endpointSettings);

                var secondHarness  = new RabbitMqTestHarness();
                var secondConsumer = new EventConsumer(secondHarness.GetTask <ConsumeContext <SomeEvent> >());
                secondHarness.OnConfigureRabbitMqBus += configurator =>
                {
                    configurator.ReceiveEndpoint(endpointDefinition, KebabCaseEndpointNameFormatter.Instance, e =>
                    {
                        e.Consumer(() => secondConsumer);
                    });
                };

                await secondHarness.Start();

                try
                {
                    await firstHarness.Bus.Publish(new SomeEvent());

                    await firstConsumer.Completed;

                    await secondConsumer.Completed;
                }
                finally
                {
                    await secondHarness.Stop();
                }
            }
            finally
            {
                await firstHarness.Stop();
            }
        }
Beispiel #22
0
 /// <summary>
 /// Construtor opcional para aplicação fornecer tanto o consumidor quanto o observador da sessão.
 /// <para>Esse construtor é opcional porque a aplicação sempre pode adicionar outros consumidores ou outros observadores no <see cref="CollaborationSession"/> quando inicia a colaboração através do <see cref="EasyCollaboration.StartCollaboration()"/>.</para>
 /// </summary>
 /// <param name="context">contexto do OpenBus, objeto responsável pelo gerenciamento de conexões</param>
 /// <param name="consumer">consumidor do canal de eventos</param>
 /// <param name="observer">observador da sessão de colaboração</param>
 /// <exception cref="ArgumentException">caso o <c>consumer</c> não implemente <see cref="MarshalByRefObject"/> além da interface <see cref="EventConsumer"/></exception>
 /// <exception cref="ArgumentException">caso o <c>observer</c> não implemente <see cref="MarshalByRefObject"/> além da interface <see cref="CollaborationObserver"/></exception>
 public EasyCollaboration(OpenBusContext context, EventConsumer consumer, CollaborationObserver observer)
     : this(context, observer)
 {
     if ((consumer != null) && !(consumer is MarshalByRefObject))
     {
         throw new ArgumentException("EventConsumer object must implement MarshalByRefObject also", "consumer");
     }
     _consumer = consumer;
 }
Beispiel #23
0
        public EventConsumerTest()
        {
            this.rideMock = new Mock <IRideControl>();
            this.rideMock.Setup(control => control.HandleVisitorSteppingInRideLine(It.Ref <Guid> .IsAny, It.Ref <Guid> .IsAny));
            this.rideMock.Setup(control => control.HandleEmployeeChangedWorkplace(It.Ref <WorkplaceDto> .IsAny, It.Ref <Guid> .IsAny, It.Ref <WorkplaceSkill> .IsAny));
            this.rideMock.Setup(control => control.OpenRides());
            this.rideMock.Setup(control => control.CloseRides());

            this.eventConsumer = new EventConsumer(this.rideMock.Object, Configuration);
        }
Beispiel #24
0
 private static void Main(string[] args)
 {
     using (EventProducer producer = new EventProducer(TimeSpan.FromMilliseconds(250.0d)))
         using (EventConsumer consumer = new EventConsumer(producer, TimeSpan.FromSeconds(1.0d)))
         {
             Console.WriteLine("Press ENTER to stop.");
             Console.ReadLine();
         }
     Console.WriteLine("Done.");
 }
Beispiel #25
0
        /// <summary>
        /// Processes events received from FreeSWITCH and dispatches them to the
        /// application event handlers.  This is called from <see cref="SwitchApp" />
        /// immediately after <see cref="SwitchApp.Main" /> returns on the
        /// application thread.
        /// </summary>
        internal static void EventLoop()
        {
            // Enlist in low-level global events if the application requested.

            var options = (switch_xml_section_enum_t)0;

            if (dialPlanEvent != null)
            {
                options |= switch_xml_section_enum_t.SWITCH_XML_SECTION_DIALPLAN;
            }

            if (userDirectoryEvent != null)
            {
                options |= switch_xml_section_enum_t.SWITCH_XML_SECTION_DIRECTORY;
            }

            if (options != (switch_xml_section_enum_t)0)
            {
                eventBinding = SwitchXmlSearchBinding.Bind(OnSwitchXmlSearchEvent, options);
            }

            // Decided whether we're going to actually consume NeonSwitch events based
            // on whether the application enlisted in the [EventReceived] event within
            // its [Main()] method.  Note that we we'll exit the event loop (and the
            // application's background thread) immediately if this is the case.

            if (eventReceived == null)
            {
                return;
            }

            eventConsumer = new EventConsumer("all", string.Empty, 0);

            // Loop, waiting for the application to terminate.

            while (!stopPending)
            {
                try
                {
                    var fsEvent     = eventConsumer.pop(1, 0);
                    var switchEvent = new SwitchEvent(fsEvent.InternalEvent);

                    RaiseEventReceived(new SwitchEventArgs(switchEvent));

                    if (switchEvent.EventType == SwitchEventCode.BackgroundJob)
                    {
                        RaiseJobCompletedEvent(new JobCompletedEventArgs(switchEvent.Headers.Get("Job-ID", Guid.Empty)));
                    }
                }
                catch (Exception e)
                {
                    SysLog.LogException(e);
                }
            }
        }
Beispiel #26
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Dictionary <string, List <YuanXinLogContext> > dictionary = new Dictionary <string, List <YuanXinLogContext> >();

            //配置消费者组
            try
            {
                var config = new Config()
                {
                    GroupId = "example-csharp-consumer"
                };
                using (var consumer = new EventConsumer(config, ""))
                {
                    //注册一个事件
                    consumer.OnMessage += (obj, msg) =>
                    {
                        var myclient = msg.Payload.DeSerialize <YuanXinLogContext>();
                        var key      = myclient.ContextId;
                        if (dictionary.ContainsKey(key))
                        {
                            List <YuanXinLogContext> list;
                            dictionary.TryGetValue(key, out list);
                            list.Add(myclient);
                            dictionary.Remove(key);
                            dictionary.Add(key, list);
                        }
                        else
                        {
                            dictionary.Add(key, new List <YuanXinLogContext> {
                                myclient
                            });
                        }
                        Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset}");
                    };

                    //订阅一个或者多个Topic
                    var tops = new List <string>();
                    tops.Add("LoggerTopic");
                    consumer.Subscribe(tops);

                    //启动
                    consumer.Start();
                    Console.WriteLine("Started consumer, press enter to stop consuming");
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
            }
        }
        public override ITrackingEventStream OpenStream(ITrackingToken trackingToken)
        {
            if (trackingToken != null)
            {
                throw new NotSupportedException("The simple event bus does not support non-null tracking tokens");
            }


            var eventStream = new EventConsumer(_queueCapacity, this);

            _eventStreams.TryAdd(eventStream, null);
            return(eventStream);
        }
Beispiel #28
0
        /// <summary>
        /// 启动消息监听
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="topicNames"></param>
        /// <param name="groupId">消费者组</param>
        /// <param name="host"></param>
        /// <param name="consumerAction">处理该类消息的逻辑类</param>
        public void SubscribeAt <T>(List <string> topicNames, string groupId, string host, IEventConsumer <T> consumerAction)
        {
            var config = new Config()
            {
                GroupId = groupId
            };
            var consumer = new EventConsumer(config, $"{host}:9092");

            consumer.OnMessage       += (obj, msg) => { consumerAction.Consumer(msg); };
            consumer.OnConsumerError += (obj, error) => { Log.Write($"消费消息异常:{error.ToString()}", MessageType.Error); };
            consumer.Subscribe(topicNames);
            consumer.Start();
        }
            private bool disposedValue = false; // To detect redundant calls

            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        EventConsumer.Dispose();
                        EventConsumer = null;
                    }

                    disposedValue = true;
                }
            }
Beispiel #30
0
        private async Task ProcessBatchesAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"Sleeping for {_configuration.BatchFrequencyInMilliseconds} ms.".AddTimestamp());
                await Task.Delay(_configuration.BatchFrequencyInMilliseconds, cancellationToken);

                List <Bet> batch;
                ulong      deliveryTag;
                lock (_betsBatch)
                {
                    batch       = _betsBatch;
                    _betsBatch  = new List <Bet>();
                    deliveryTag = _lastDeliveryTag;
                }

                if (!batch.Any())
                {
                    _logger.LogInformation("No batch to process.".AddTimestamp());
                    continue;
                }

                _logger.LogInformation($"Processing batch of {batch.Count} bets.".AddTimestamp());

                var stakeIds       = batch.Select(b => b.StakeId);
                var betsByStakeIds = batch.GroupBy(b => b.StakeId).ToDictionary(g => g.Key, g => g.Count());

                using (var dbContext = new BetsDbContext(_configuration.ConnectionString))
                    using (var transaction = await dbContext.Database.BeginTransactionAsync(IsolationLevel.RepeatableRead, cancellationToken))
                    {
                        var matches = await dbContext.Matches.Include(m => m.Stakes).Where(m => m.Stakes.Any(s => stakeIds.Contains(s.Id)))
                                      .ToListAsync(cancellationToken);

                        _logger.LogInformation($"Got {matches.Count} matches".AddTimestamp());

                        var newStakes = matches.SelectMany(m => m.Stakes.Where(s => s.IsBettable).Select(s => CalculateNewStake(s, m, betsByStakeIds))).ToList();

                        foreach (var oldStake in matches.SelectMany(m => m.Stakes.Where(s => s.IsBettable)))
                        {
                            oldStake.IsBettable = false;
                        }
                        _logger.LogInformation("Calculated stakes.");
                        dbContext.Stakes.AddRange(newStakes);
                        await dbContext.SaveChangesAsync(cancellationToken);

                        transaction.Commit();
                    }

                EventConsumer.Ack(deliveryTag);
            }
        }
Beispiel #31
0
        /// <summary>
        /// Called by <see cref="SwitchApp" /> at th very end of the application
        /// shut down process to give this class the chance to release any resources
        /// it holds.
        /// </summary>
        internal static void Shutdown()
        {
            if (eventBinding != null)
            {
                eventBinding.Dispose();
                eventBinding = null;
            }

            if (eventConsumer != null)
            {
                eventConsumer.Dispose();
                eventConsumer = null;
            }
        }
Beispiel #32
0
        static void Main(string[] args)
        {
            var messageBrokerConnections = new Dictionary<string, MessageBrokerConnection>();

            PopulateMessageBrokerConnections(messageBrokerConnections);
            var eventConsumers = new Dictionary<string, IEventConsumer>();

            foreach (var environment in messageBrokerConnections.Keys)
            {
                var consoleWriteLine = new Action<IBasicProperties, IEvent>((props, evnt) => Console.WriteLine("{0}: {1:G}->{2}", environment, evnt.Created.ToLocalTime(), evnt.RoutingKey));
                var consoleWriteStatus = new Action<IBasicProperties, IEvent>((props, evnt) =>
                {
                    var scs = evnt as ServiceComponentStatus;
                    if (scs != null)
                        Console.WriteLine("{0}: Status: {1} for {2}ms, SubStatus: {3} for {4}ms", environment, scs.Status, scs.TimeInStatus.TotalMilliseconds, scs.SubStatus, scs.TimeInSubStatus.TotalMilliseconds);
                    else
                        Console.WriteLine("{0}: {1:G}->{2}", environment, evnt.Created.ToLocalTime(), evnt.RoutingKey);
                });

                var ec2 = new EventConsumer(messageBrokerConnections[environment].Connection);
                eventConsumers.Add(environment, ec2);

                #region Service Host events
                ec2.RegisterEventHandler<ServiceHostStateChange>(
                    new ServiceHostStateChange() { Environment = "#", Machine = "#", Process = "#", State = "#" }.RoutingKey,
                    consoleWriteLine
                    );

                ec2.RegisterEventHandler<ServiceHostStateChangeFailed>(
                    new ServiceHostStateChangeFailed() { Environment = "#", Machine = "#", Process = "#", State = "#" }.RoutingKey,
                    consoleWriteLine
                    );
                #endregion

                #region Service Host Component events
                ec2.RegisterEventHandler<ServiceHostComponentStateChange>(
                    new ServiceHostComponentStateChange() { Environment = "#", Machine = "#", Process = "#", ServiceComponent = "#", State = "#" }.RoutingKey,
                    consoleWriteLine
                    );

                ec2.RegisterEventHandler<ServiceHostComponentStateChangeFailed>(
                    new ServiceHostComponentStateChangeFailed() { Environment = "#", Machine = "#", Process = "#", ServiceComponent = "#", State = "#" }.RoutingKey,
                    consoleWriteLine
                    );
                #endregion

                #region Service Component events
                ec2.RegisterEventHandler<ServiceComponentStatus>(
                    new ServiceComponentStatus() { Machine = "#", ServiceComponent = "#", Status = "#" }.RoutingKey,
                    consoleWriteStatus
                    );

                ec2.RegisterEventHandler<ServiceComponentStateChangeFailed>(
                    new ServiceComponentStateChangeFailed() { Machine = "#", Process = "#", ServiceComponent = "#", Status = "#" }.RoutingKey,
                    consoleWriteStatus
                    );
                #endregion
            }

            var exit = false;
            Console.WriteLine("Press x to exit");
            do
            {
                var cki = Console.ReadKey(true);
                if (string.Compare("x", new string(cki.KeyChar, 1), true, CultureInfo.InvariantCulture) == 0)
                    exit = true;
            }
            while (!exit);

            eventConsumers.Values.ToList().ForEach(ec => ec.Close());
            eventConsumers.Clear();
            messageBrokerConnections.Values.ToList().ForEach(mbc => mbc.Close());
            messageBrokerConnections.Clear();

            Console.WriteLine("Exiting.");
        }
Beispiel #33
0
		public void AddConsumer_ToTail( EventConsumer EventConsumer )
		{
			ConsumerList.AddLast( EventConsumer );
		}
Beispiel #34
0
		public void RemoveConsumer( EventConsumer EventConsumer )
		{
			ConsumerList.Remove( EventConsumer );
		}