• Azure Event hub usage


    1. create event hub on azure




    2. create a prj , event hub sender, install nuget pkg - azure service bus



    3. check connection string



    4. sender sample code


    static void Main(string[] args)
            {
                Console.WriteLine("Press Ctrl-C to stop the sender process");
                Console.WriteLine("Press Enter to start now");
                Console.ReadLine();
                SendingRandomMessages();
            }
    
    
            static string eventHubName = "get from event hub connection information";
            static string connectionString = "get from event hub connection information";
    
    
            static void SendingRandomMessages()
            {
                var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
                while (true)
                {
                    try
                    {
                        var message = Guid.NewGuid().ToString();
                        Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
                        eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
                    }
                    catch (Exception exception)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                        Console.ResetColor();
                    }
    
    
                    Thread.Sleep(200);
                }
            }
    



    5. create a storage account

    6. install nuget for receiver prj



    7. check keys



    8. sample code for receiver


    class SimpleEventProcessor : IEventProcessor
        {
            Stopwatch checkpointStopWatch;
    
    
            async Task IEventProcessor.CloseAsync(PartitionContext context, CloseReason reason)
            {
                Console.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason);
                if (reason == CloseReason.Shutdown)
                {
                    await context.CheckpointAsync();
                }
            }
    
    
            Task IEventProcessor.OpenAsync(PartitionContext context)
            {
                Console.WriteLine("SimpleEventProcessor initialized.  Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset);
                this.checkpointStopWatch = new Stopwatch();
                this.checkpointStopWatch.Start();
                return Task.FromResult<object>(null);
            }
    
    
            async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
            {
                foreach (EventData eventData in messages)
                {
                    string data = Encoding.UTF8.GetString(eventData.GetBytes());
    
    
                    Console.WriteLine(string.Format("Message received.  Partition: '{0}', Data: '{1}'",
                        context.Lease.PartitionId, data));
                }
    
    
                //Call checkpoint every 5 minutes, so that worker can resume processing from 5 minutes back if it restarts.
                if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5))
                {
                    await context.CheckpointAsync();
                    this.checkpointStopWatch.Restart();
                }
            }
        }
    
    
    static void Main(string[] args)
            {
                string eventHubConnectionString = "get from azure event hub connection information";
                string eventHubName = "get from azure event hub connection information";
                string storageAccountName = "get from azure storage keys";
                string storageAccountKey = "get from azure storage keys";
                string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccountName, storageAccountKey);
    
    
                string eventProcessorHostName = Guid.NewGuid().ToString();
                EventProcessorHost eventProcessorHost = new EventProcessorHost(eventProcessorHostName, eventHubName, EventHubConsumerGroup.DefaultGroupName, eventHubConnectionString, storageConnectionString);
                Console.WriteLine("Registering EventProcessor...");
                var options = new EventProcessorOptions();
                options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
                eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(options).Wait();
    
    
                Console.WriteLine("Receiving. Press enter key to stop worker.");
                Console.ReadLine();
                eventProcessorHost.UnregisterEventProcessorAsync().Wait();
            }
    



  • 相关阅读:
    NOIP2010普及组T3 接水问题 ——S.B.S.
    【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx
    【NOIP合并果子】uva 10954 add all【贪心】——yhx
    #include &lt;NOIP2009 Junior&gt; 细胞分裂 ——using namespace wxl;
    #include &lt;NOIP2008 Junior&gt; 双栈排序 ——using namespace wxl;
    NOIP2010普及组 三国游戏 -SilverN
    NOIP2009 提高组T3 机器翻译 解题报告-S.B.S
    NOIP2010提高组乌龟棋 -SilverN
    NOIP2010提高组 机器翻译 -SilverN
    UVa 297 Quadtrees -SilverN
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7269992.html
Copyright © 2020-2023  润新知