コード例 #1
0
        static void Main(string[] args)
        {
            NamespaceManager namespaceManager      = NamespaceManager.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]);
            EventHubClient   eventHubReceiveClient = EventHubClient.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"], EventHubName);
            var consumerGroup            = eventHubReceiveClient.GetDefaultConsumerGroup();
            EventHubDescription eventHub = namespaceManager.GetEventHub(EventHubName);

            // Register event processor with each shard to start consuming messages
            foreach (var partitionId in eventHub.PartitionIds)
            {
                consumerGroup.RegisterProcessor <CustomEventProcessor>(new Lease()
                {
                    PartitionId = partitionId
                }, new CustomCheckpointManager());
            }

            // Wait for the user to exit this application.
            Console.WriteLine("\nPress ENTER to exit...\n");
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Engine.cs プロジェクト: ninocrudele/EventHubs
        /// <summary>
        /// Receive message in direct pattern
        /// </summary>
        public void Direct()
        {
            //DA FINIRE
            ConsoleWriteLine("Start Receiver [DIRECT MESSAGE]", ConsoleColor.Yellow);

            //Single
            bool   receive = true;
            string myOffset;
            string connectionString = "Endpoint=sb://[NAMESPACE].servicebus.windows.net/;SharedAccessKeyName=All;SharedAccessKey=[SharedAccessKey]";

            //CONNN---
            //Create the connection string
            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString)
            {
                TransportType = TransportType.Amqp
            };

            //Create the EH Client
            string         eventHubName   = "test123";
            EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);

            //muli partition sample
            NamespaceManager    namespaceManager = NamespaceManager.CreateFromConnectionString(builder.ToString());
            EventHubDescription ehDescription    = namespaceManager.GetEventHub("test123");

            //Use the default consumer group

            foreach (var partitionId in ehDescription.PartitionIds)
            {
                Thread myNewThread = new Thread(() => ReceiveDirectFromPartition(eventHubClient, partitionId));
                myNewThread.Start();
            }
            //////////////////////



            ConsoleWriteLine("Direct Receiver polling done...", ConsoleColor.DarkYellow);
        }
コード例 #3
0
        public void Run(string connectionString, string hubName, string measureNameFilter)
        {
            NamespaceManager    nsmgr = NamespaceManager.CreateFromConnectionString(connectionString);
            EventHubDescription desc  = nsmgr.GetEventHub(hubName);

            string consumerGroupName = _consumerGroupPrefix + DateTime.UtcNow.Ticks.ToString();
            ConsumerGroupDescription consumerGroupDesc = nsmgr.CreateConsumerGroupIfNotExists(new ConsumerGroupDescription(hubName, consumerGroupName));

            EventHubClient client = EventHubClient.CreateFromConnectionString(connectionString, hubName);

            int numPartitions = desc.PartitionCount;

            _receivers = new EventHubReceiver[numPartitions];
            //_receiversLastUpdate = new DateTime[numPartitions];
            //for (int i = 0; i < numPartitions; i++)
            //{
            //    _receiversLastUpdate[i] = DateTime.UtcNow;
            //}

            _tasks             = new Task[numPartitions];
            _buffers           = new Dictionary <string, CircularBuffer <SensorDataContract> >();
            _measureNameFilter = measureNameFilter;

            for (int iPart = 0; iPart < desc.PartitionCount; iPart++)
            {
                EventHubReceiver receiver = client.GetConsumerGroup(consumerGroupName).CreateReceiver(
                    desc.PartitionIds[iPart], DateTime.UtcNow - TimeSpan.FromMinutes(2));
                _receivers[iPart] = receiver;

                Task <IEnumerable <EventData> > task = receiver.ReceiveAsync(1000, TimeSpan.FromSeconds(1));

                int thisPart = iPart;
                task.ContinueWith(new Action <Task <IEnumerable <EventData> > >((t) => OnTaskComplete(t, thisPart)));
                _tasks[iPart] = task;
            }
        }
コード例 #4
0
        private bool CreateWeb(CloudWebDeployInputs inputs)
        {
            Console.WriteLine("Retrieving namespace metadata...");
            // Create Namespace
            ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient(inputs.Credentials);

            var    nsDescription      = sbMgmt.Namespaces.GetNamespaceDescription(inputs.SBNamespace);
            string nsConnectionString = nsDescription.NamespaceDescriptions.First(
                (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization")
                ).ConnectionString;

            NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString);

            EventHubDescription ehDevices = nsManager.GetEventHub(inputs.EventHubNameDevices);
            EventHubDescription ehAlerts  = nsManager.GetEventHub(inputs.EventHubNameAlerts);

            StorageManagementClient stgMgmt = new StorageManagementClient(inputs.Credentials);
            var keyResponse = stgMgmt.StorageAccounts.GetKeys(inputs.StorageAccountName.ToLowerInvariant( ));

            if (keyResponse.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Error retrieving access keys for storage account {0} in Location {1}: {2}",
                                  inputs.StorageAccountName, inputs.Location, keyResponse.StatusCode);
                return(false);
            }

            var storageKey = keyResponse.PrimaryKey;

            string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString)
            {
                SharedAccessKeyName = "WebSite",
                SharedAccessKey     = (ehDevices.Authorization.First((d)
                                                                     => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey,
            }.ToString( );

            string ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString)
            {
                SharedAccessKeyName = "WebSite",
                SharedAccessKey     = (ehAlerts.Authorization.First((d)
                                                                    => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey,
            }.ToString( );

            Console.WriteLine("Started processing...");
            // Write a new web.config template file
            var doc = new XmlDocument {
                PreserveWhitespace = true
            };

            //var inputFileName = ( inputs.Transform ? "\\web.PublishTemplate.config" : "\\web.config" );
            string inputFileName = "web.PublishTemplate.config";
            //var outputFileName = ( inputs.Transform ? String.Format("\\web.{0}.config", inputs.NamePrefix) : "\\web.config" );
            string outputFileName = "web.config";

            //doc.Load( inputs.WebSiteDirectory + inputFileName );

            string inputFilePath = Environment.CurrentDirectory + @"\";

            Console.WriteLine("Opening and updating " + inputFilePath + inputFileName);

            doc.Load(inputFilePath + inputFileName);

            doc.SelectSingleNode(
                "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value").Value
                = inputs.EventHubNameDevices;
            doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value")
            .Value
                = inputs.EventHubNameAlerts;
            doc.SelectSingleNode(
                "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value").Value
                = nsConnectionString;
            doc.SelectSingleNode(
                "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value").Value
                = ehDevicesWebSiteConnectionString;
            doc.SelectSingleNode(
                "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value").Value
                = ehAlertsWebSiteConnectionString;
            doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value")
            .Value =
                String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", inputs.StorageAccountName,
                              storageKey);

            //var outputFile = System.IO.Path.GetFullPath( inputs.WebSiteDirectory + outputFileName );
            string outputFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //Console.WriteLine(outputFilePath);

            var outputFile = outputFilePath + @"\" + outputFileName;

            Console.WriteLine("Writing updates to " + outputFile);

            doc.Save(outputFile);
            Console.WriteLine(" ");
            Console.WriteLine("Web config saved to {0}", outputFile);
            Console.WriteLine(" ");
            return(true);
        }
        public void LoadEventHub(Property[] inputs, RequiredProperties required, Property[] returns, MethodType methodType, ServiceObject serviceObject)
        {
            Utilities.ServiceUtilities serviceUtilities = new Utilities.ServiceUtilities(serviceBroker);
            serviceObject.Properties.InitResultTable();
            NamespaceManager    namespaceManager = null;
            EventHubDescription EventHub         = null;

            try
            {
                string eventhubpath = string.Empty;
                if (inputs.Length == 0)
                {
                    eventhubpath = serviceObject.MetaData.DisplayName;
                }
                else
                {
                    eventhubpath = inputs.Where(p => p.Name.Equals("eventhub")).First().Value.ToString();
                }

                namespaceManager = serviceUtilities.GetNamespaceManager(inputs.Where(p => p.Name.Equals("requesttimeout")).FirstOrDefault());
                //namespaceManager = NamespaceManager.CreateFromConnectionString(serviceBroker.Service.ServiceConfiguration[ServiceConfigurationSettings.ConnectionString].ToString());

                EventHub = namespaceManager.GetEventHub(eventhubpath);

                foreach (Property prop in returns)
                {
                    switch (prop.Name)
                    {
                    case "createdat":
                        prop.Value = EventHub.CreatedAt;
                        break;

                    case "isreadonly":
                        prop.Value = EventHub.IsReadOnly;
                        break;

                    case "messageretentionindays":
                        prop.Value = EventHub.MessageRetentionInDays;
                        break;

                    case "partitioncount":
                        prop.Value = EventHub.PartitionCount;
                        break;

                    case "partitionids":
                        prop.Value = string.Join(",", EventHub.PartitionIds);
                        break;

                    case "path":
                        prop.Value = EventHub.Path;
                        break;

                    case "status":
                        prop.Value = EventHub.Status.ToString();
                        break;

                    case "updatedat":
                        prop.Value = EventHub.UpdatedAt;
                        break;

                    case "usermetadata":
                        prop.Value = EventHub.UserMetadata;
                        break;
                    }
                }
                returns.Where(p => p.Name.Equals("responsestatus")).First().Value            = ResponseStatus.Success;
                returns.Where(p => p.Name.Equals("responsestatusdescription")).First().Value = "Event Hub loaded";
            }
            catch (Exception ex)
            {
                returns.Where(p => p.Name.Equals("responsestatus")).First().Value            = ResponseStatus.Error;
                returns.Where(p => p.Name.Equals("responsestatusdescription")).First().Value = ex.Message;
            }
            finally
            {
                namespaceManager = null;
                EventHub         = null;
            }
            serviceObject.Properties.BindPropertiesToResultTable();
        }
コード例 #6
0
        bool Run( )
        {
            var partitionCount  = 8;
            var receiverKeyName = "WebSite";

            CloudWebDeployInputs inputs = null;

            if (!GetInputs(out inputs))
            {
                return(false);
            }

            Console.WriteLine("Retrieving namespace metadata...");
            // Create Namespace
            ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient(inputs.Credentials);

            var    nsDescription      = sbMgmt.Namespaces.GetNamespaceDescription(inputs.SBNamespace);
            string nsConnectionString = nsDescription.NamespaceDescriptions.First(
                (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization")
                ).ConnectionString;

            NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString);

            EventHubDescription ehDevices = nsManager.GetEventHub(inputs.EventHubNameDevices);

            StorageManagementClient stgMgmt = new StorageManagementClient(inputs.Credentials);
            var keyResponse = stgMgmt.StorageAccounts.GetKeys(inputs.StorageAccountName.ToLowerInvariant( ));

            if (keyResponse.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Error retrieving access keys for storage account {0} in Location {1}: {2}",
                                  inputs.StorageAccountName, inputs.Location, keyResponse.StatusCode);
                return(false);
            }

            var serviceNamespace = inputs.SBNamespace;
            var hubName          = inputs.EventHubNameDevices;

            var sharedAccessAuthorizationRule = ehDevices.Authorization.First((d)
                                                                              => String.Equals(d.KeyName, receiverKeyName, StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule;

            if (sharedAccessAuthorizationRule == null)
            {
                Console.WriteLine("Cannot locate Authorization rule for WebSite key.");
                return(false);
            }

            var receiverKey = sharedAccessAuthorizationRule.PrimaryKey;
            //Console.WriteLine("Starting temperature processor with {0} partitions.", partitionCount);

            CancellationTokenSource cts = new CancellationTokenSource( );

            int            closedReceivers  = 0;
            AutoResetEvent receiversStopped = new AutoResetEvent(false);

            for (int i = 0; i < partitionCount; i++)
            {
                Task.Factory.StartNew((state) =>
                {
                    try
                    {
                        _ConsoleBuffer.Add(string.Format("Starting worker to process partition: {0}", state));

                        var factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, ""), new MessagingFactorySettings( )
                        {
                            TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(receiverKeyName, receiverKey),
                            TransportType = TransportType.Amqp
                        });

                        var receiver = factory.CreateEventHubClient(hubName)
                                       .GetDefaultConsumerGroup( )
                                       .CreateReceiver(state.ToString( ), DateTime.UtcNow);

                        _ConsoleBuffer.Add(string.Format("Waiting for start receiving messages: {0} ...", state));

                        while (true)
                        {
                            // Receive could fail, I would need a retry policy etc...
                            var messages = receiver.Receive(10);
                            foreach (var message in messages)
                            {
                                //var eventBody = Newtonsoft.Json.JsonConvert.DeserializeObject<TemperatureEvent>(Encoding.Default.GetString(message.GetBytes()));
                                //Console.WriteLine("{0} [{1}] Temperature: {2}", DateTime.Now, message.PartitionKey, eventBody.Temperature);
                                _ConsoleBuffer.Add(message.PartitionKey + " sent message:" + Encoding.Default.GetString(message.GetBytes( )));
                            }

                            if (cts.IsCancellationRequested)
                            {
                                Console.WriteLine("Stopping: {0}", state);
                                receiver.Close( );
                                if (Interlocked.Increment(ref closedReceivers) >= partitionCount)
                                {
                                    receiversStopped.Set();
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _ConsoleBuffer.Add(ex.Message);
                    }
                }, i);
            }

            Console.ReadLine( );
            cts.Cancel( );

            //waiting for all receivers to stop
            receiversStopped.WaitOne( );

            bool saveToFile;

            for ( ;;)
            {
                Console.WriteLine("Do you want to save received data to file? (y/n)");

                string answer  = Console.ReadLine( );
                string request = "do not";

                saveToFile = false;
                if (!string.IsNullOrEmpty(answer) && answer.ToLower( ).StartsWith("y"))
                {
                    saveToFile = true;
                    request    = "";
                }
                if (ConsoleHelper.Confirm("Are you sure you " + request + " want to save received data?"))
                {
                    break;
                }
            }
            if (saveToFile)
            {
                string fileName     = inputs.SBNamespace + DateTime.UtcNow.ToString("_d_MMM_h_mm") + ".log";
                string filePath     = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string fileFullName = filePath + @"\" + fileName;
                if (_ConsoleBuffer.FlushToFile(fileFullName))
                {
                    Console.WriteLine("Output was saved to your desktop, at " + fileFullName + " file.");
                }
            }


            Console.WriteLine("Wait for all receivers to close and then press ENTER.");
            Console.ReadLine( );

            return(true);
        }