Ejemplo n.º 1
0
 private static void ReceiveDirectFromPartition(
     EventHubClient eventHubClient,
     string partitionId,
     string consumerGroup)
 {
     try
     {
         var group = eventHubClient.GetConsumerGroup(consumerGroup);
         EventHubReceiver receiver = null;
         receiver = group.CreateReceiver(partitionId, DateTime.UtcNow);
         LogEngine.TraceInformation($"Direct Receiver created. Partition {partitionId}");
         while (true)
         {
             var message = receiver?.Receive();
             if (message != null)
             {
                 BubblingObject bubblingObject = BubblingObject.DeserializeMessage(message.GetBytes());
                 MessageIngestor.IngestMessagge(bubblingObject);
             }
         }
     }
     catch (Exception ex)
     {
         LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Error {ex.Message}");
     }
 }
Ejemplo n.º 2
0
        public bool CreateEventUpStream()
        {
            try
            {
                //EH Configuration
                connectionString = ConfigurationLibrary.AzureNameSpaceConnectionString();
                eventHubName     = ConfigurationLibrary.GroupEventHubsName();

                LogEngine.TraceInformation($"Start GrabCaster UpStream - Point Id {ConfigurationLibrary.PointId()} - Point name {ConfigurationLibrary.PointName()} - Channel Id {ConfigurationLibrary.ChannelId()} - Channel name {ConfigurationLibrary.ChannelName()} ");

                var builder = new ServiceBusConnectionStringBuilder(connectionString)
                {
                    TransportType =
                        TransportType.Amqp
                };

                eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        public void PersistEventToStorage(byte[] messageBody, string messageId, string groupEventHubsStorageAccountName,
                                          string groupEventHubsStorageAccountKey)
        {
            try
            {
                var storageAccountName = groupEventHubsStorageAccountName;
                var storageAccountKey  = groupEventHubsStorageAccountKey;
                var connectionString   =
                    $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey}";
                var storageAccount = CloudStorageAccount.Parse(connectionString);
                var blobClient     = storageAccount.CreateCloudBlobClient();

                // Retrieve a reference to a container.
                var container = blobClient.GetContainerReference(ConfigurationLibrary.GroupEventHubsName());

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                container.SetPermissions(
                    new BlobContainerPermissions {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

                // Create the messageid reference
                var blockBlob = container.GetBlockBlobReference(messageId);
                blockBlob.UploadFromByteArray(messageBody, 0, messageBody.Length);
                LogEngine.TraceInformation("Event persisted -  Consistency Transaction Point created.");
            }
            catch (Exception ex)
            {
                LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Error {ex.Message}");
            }
        }
Ejemplo n.º 4
0
        public void Run(MessageIngestor.SetEventActionEventEmbedded setEventActionEventEmbedded)
        {
            try
            {
                SetEventActionEventEmbedded = setEventActionEventEmbedded;

                //Load message ingestor
                MessageIngestor.Init(SetEventActionEventEmbedded);
                // Assign the delegate

                // Load vars
                var eventHubConnectionString = ConfigurationLibrary.AzureNameSpaceConnectionString();
                var eventHubName             = ConfigurationLibrary.GroupEventHubsName();

                LogEngine.TraceInformation(
                    $"Start GrabCaster DownStream - Point Id {ConfigurationLibrary.PointId()} - Point name {ConfigurationLibrary.PointName()} - Channel Id {ConfigurationLibrary.ChannelId()} - Channel name {ConfigurationLibrary.ChannelName()} ");

                var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString)
                {
                    TransportType =
                        TransportType.Amqp
                };

                //If not exit it create one, drop brachets because Azure rules
                var eventHubConsumerGroup = string.Concat(ConfigurationLibrary.EngineName(), "_",
                                                          ConfigurationLibrary.ChannelId().Replace("{", "").Replace("}", "").Replace("-", ""));

                var nsManager = NamespaceManager.CreateFromConnectionString(builder.ToString());

                LogEngine.TraceInformation(
                    $"Start DirectRegisterEventReceiving. - Initializing Group Name {eventHubConsumerGroup}");

                // Create Event Hubs
                var eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);
                // Create consumer
                nsManager.CreateConsumerGroupIfNotExists(eventHubName, eventHubConsumerGroup);

                var namespaceManager = NamespaceManager.CreateFromConnectionString(builder.ToString());
                var ehDescription    = namespaceManager.GetEventHub(eventHubName);
                // Use the default consumer group

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

                LogEngine.TraceInformation("After DirectRegisterEventReceiving Downstream running.");
            }
            catch (Exception ex)
            {
                LogEngine.TraceError(
                    $"Error in {MethodBase.GetCurrentMethod().Name} - Hint: Check if the firewall outbound port 5671 is opened. - Error {ex.Message}");
            }
        }
Ejemplo n.º 5
0
 public static void Init(SetEventActionEventEmbedded setEventOnRampMessageReceived)
 {
     try
     {
         setEventActionEventEmbedded = setEventOnRampMessageReceived;
     }
     catch (Exception ex)
     {
         LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Error {ex.Message}");
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 ///     Send a EventMessage message
 ///     invio importantissimo perche spedisce eventi e oggetti in array bytela dimensione e strategica
 /// </summary>
 /// <param name="message"></param>
 public void SendMessage(SkeletonMessage message)
 {
     try
     {
         byte[]    byteArrayBytes = SkeletonMessage.SerializeMessage(message);
         EventData evtData        = new EventData(byteArrayBytes);
         eventHubClient.Send(evtData);
     }
     catch (Exception ex)
     {
         LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Error {ex.Message}");
     }
 }
Ejemplo n.º 7
0
        public byte[] PersistEventFromStorage(string messageId, string groupEventHubsStorageAccountName,
                                              string groupEventHubsStorageAccountKey)
        {
            try
            {
                var storageAccountName = groupEventHubsStorageAccountName;
                var storageAccountKey  = groupEventHubsStorageAccountKey;
                var connectionString   =
                    $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey}";
                var storageAccount = CloudStorageAccount.Parse(connectionString);
                var blobClient     = storageAccount.CreateCloudBlobClient();

                // Retrieve a reference to a container.
                var container = blobClient.GetContainerReference(ConfigurationLibrary.GroupEventHubsName());

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                container.SetPermissions(
                    new BlobContainerPermissions {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

                // Create the messageid reference
                var blockBlob = container.GetBlockBlobReference(messageId);

                blockBlob.FetchAttributes();
                var msgByteLength = blockBlob.Properties.Length;
                var msgContent    = new byte[msgByteLength];
                for (var i = 0; i < msgByteLength; i++)
                {
                    msgContent[i] = 0x20;
                }

                blockBlob.DownloadToByteArray(msgContent, 0);

                LogEngine.TraceInformation("Event persisted recovered -  Consistency Transaction Point restored.");

                return(msgContent);
            }
            catch (Exception ex)
            {
                LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Error {ex.Message}");
                return(null);
            }
        }
Ejemplo n.º 8
0
        public static void IngestMessagge(object message)
        {
            byte[] eventDataByte  = null;
            var    bubblingObject = (IBubblingObject)message;

            // ****************************CHECK MESSAGE TYPE*************************
            try
            {
                // Who sent the message
                var senderId          = bubblingObject.SenderPointId;
                var senderDescription = bubblingObject.SenderDescriprion;

                // Who receive the message
                LogEngine.TraceInformation(
                    $"Event received step1 from Sender {senderId} Sender description {senderDescription}");

                var receiverChannelId =
                    bubblingObject.DestinationChannelId;
                var receiverPointId =
                    bubblingObject.DestinationPointId;

                ChannelId = "asd";
                PointId   = "asd";
                var requestAvailable = (receiverChannelId.Contains(ChannelId) &&
                                        receiverPointId.Contains(PointId)) ||
                                       (receiverChannelId.Contains("*") &&
                                        receiverPointId.Contains(PointId)) ||
                                       (receiverChannelId.Contains(ChannelId) &&
                                        receiverPointId.Contains("*")) ||
                                       (receiverChannelId.Contains("*") &&
                                        receiverPointId.Contains("*"));
                if (!requestAvailable)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                // If error then not message typeof (no property present.)
                LogEngine.TraceError(
                    $"Error in {MethodBase.GetCurrentMethod().Name} - Not GrabCaster message type received (Missing GrabCaster_MessageType_Name properties.) -DISCARED- Error {ex.Message}");
                return;
            }

            // ****************************CHECK MESSAGE TYPE*************************
            // Check if >256, the restore or not
            LogEngine.TraceInformation($"Event received step2 Check if > 256, the restore or not.");
            if (bubblingObject.Persisting)
            {
                LogEngine.TraceInformation($"Event received step3 it is > 256");
                BlobDevicePersistentProvider storagePersistent = new BlobDevicePersistentProvider();
            }
            else
            {
                LogEngine.TraceInformation($"Event received step4 it is < 256");
                eventDataByte = bubblingObject.Data;
            }

            LogEngine.TraceInformation($"Event received step5 before serialization.");
            setEventActionEventEmbedded(eventDataByte);
        }
Ejemplo n.º 9
0
        public static void IngestMessagge(object message)
        {
            string senderId;
            string senderDescription;

            byte[] eventDataByte   = null;
            var    skeletonMessage = (ISkeletonMessage)message;

            // ****************************CHECK MESSAGE TYPE*************************
            try
            {
                // Check message subscription, it must come from engine
                if (skeletonMessage.Properties[Configuration.GrabCasterMessageTypeName].ToString()
                    != Configuration.GrabCasterMessageTypeValue)
                {
                    LogEngine.TraceInformation("Not GrabCaster message type received -DISCARED-");
                    return;
                }
                else
                {
                    // Who sent the message
                    senderId          = skeletonMessage.Properties[Configuration.MessageDataProperty.SenderId.ToString()].ToString();
                    senderDescription =
                        skeletonMessage.Properties[Configuration.MessageDataProperty.SenderDescriprion.ToString()].ToString();

                    // Who receive the message
                    LogEngine.TraceInformation($"Event received step1 from Sender {senderId} Sender description {senderDescription}");

                    var receiverChannelId =
                        skeletonMessage.Properties[Configuration.MessageDataProperty.ReceiverChannelId.ToString()].ToString();
                    var receiverPointId =
                        skeletonMessage.Properties[Configuration.MessageDataProperty.ReceiverPointId.ToString()].ToString();

                    ChannelId = "asd";
                    PointId   = "asd";
                    var requestAvailable = (receiverChannelId.Contains(ChannelId) &&
                                            receiverPointId.Contains(PointId)) ||
                                           (receiverChannelId.Contains("*") &&
                                            receiverPointId.Contains(PointId)) ||
                                           (receiverChannelId.Contains(ChannelId) &&
                                            receiverPointId.Contains("*")) ||
                                           (receiverChannelId.Contains("*") &&
                                            receiverPointId.Contains("*"));
                    if (!requestAvailable)
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                // If error then not message typeof (no property present.)
                LogEngine.TraceError($"Error in {MethodBase.GetCurrentMethod().Name} - Not GrabCaster message type received (Missing GrabCaster_MessageType_Name properties.) -DISCARED- Error {ex.Message}");
                return;
            }

            // ****************************CHECK MESSAGE TYPE*************************
            // Check if >256, the restore or not
            LogEngine.TraceInformation($"Event received step2 Check if > 256, the restore or not.");
            if ((bool)skeletonMessage.Properties[Configuration.MessageDataProperty.Persisting.ToString()])
            {
                LogEngine.TraceInformation($"Event received step3 it is > 256");
                string messageId = skeletonMessage.Properties[Configuration.MessageDataProperty.MessageId.ToString()].ToString();
                BlobDevicePersistentProvider storagePersistent = new BlobDevicePersistentProvider();

                //var ret = storagePersistent.PersistEventFromStorage(
                //    messageId,
                //    GroupEventHubsStorageAccountName,
                //    GroupEventHubsStorageAccountKey,
                //    GroupEventHubsName);


                //eventDataByte = (byte[])ret;
            }
            else
            {
                LogEngine.TraceInformation($"Event received step4 it is < 256");
                eventDataByte = skeletonMessage.Body;
            }

            LogEngine.TraceInformation($"Event received step5 before serialization.");
            setEventActionEventEmbedded(eventDataByte);
        }