public virtual void ClearFailures(EnvelopeMessage message) { if (message != null) { this.messageFailures.Remove(message.MessageId); } }
public ProtocolBufferEnvelopeMessage(EnvelopeMessage message) : this() { this.MessageId = message.MessageId; this.ReturnAddress = message.ReturnAddress.ToString(); this.Headers = message.Headers; }
/// <summary> /// /// </summary> /// <param name="requestConfirm">Only valid for remote clients, since all local calls are confirmed or denied by default.</param> protected override SendToClientResultEnum DoSendToClient(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan?requestConfirmTimeout) { if (receiverId.IsMessageBusIndexValid && receiverId.MessageBus == this) {// Seems to be a local client Id. SendToClientResultEnum result = base.DoSendToClient(senderId, receiverId, envelope, requestConfirmTimeout); if (result != SendToClientResultEnum.ClientNotFound) { return(result); } } // Receiver was not local in parrent, try remote. if (IsConnected == false) { return(SendToClientResultEnum.Failure); } EnvelopeMessage message = new EnvelopeMessage() { Envelope = envelope, Receivers = new ClientId[] { receiverId }, Sender = senderId }; return(ToServer(message, requestConfirmTimeout) ? SendToClientResultEnum.Success : SendToClientResultEnum.Failure); }
public virtual void HandleFailure(EnvelopeMessage message, Exception exception) { if (this.CanRetry(message)) return; AppendExceptionHeaders(message, exception, 0); this.ForwardToPoisonMessageQueue(message); }
private static TimeSpan GetTimeToBeReceived(EnvelopeMessage message) { if (message.TimeToLive == TimeSpan.MaxValue || message.TimeToLive == TimeSpan.Zero) { return(MessageQueue.InfiniteTimeout); } return(message.TimeToLive); }
private void ForwardToPoisonMessageQueue(EnvelopeMessage message) { Log.Info(Diagnostics.ForwardingMessageToPoisonMessageQueue, this.maxRetries, message.MessageId); this.poisonQueue.Send(message, PoisonMessageQueue); if (postHandlingAction != null) { postHandlingAction(message); } }
public virtual bool IsPoison(EnvelopeMessage message) { if (message == null) return false; int retries; this.messageFailures.TryGetValue(message.MessageId, out retries); return retries >= this.maxRetries; }
private bool CanRetry(EnvelopeMessage message) { lock (this.messageFailures) { int retries; this.messageFailures.TryGetValue(message.MessageId, out retries); this.messageFailures[message.MessageId] = ++retries; return retries < this.maxRetries; } }
public static Message BuildMsmqMessage(EnvelopeMessage message, Stream serialized) { return new Message { Label = GetLabel(message), BodyStream = serialized, Recoverable = message.Persistent, TimeToBeReceived = GetTimeToBeReceived(message), }; }
public virtual void HandleFailure(EnvelopeMessage message, Exception exception) { if (this.CanRetry(message)) { return; } AppendExceptionHeaders(message, exception, 0); this.ForwardToPoisonMessageQueue(message); }
private bool CanRetry(EnvelopeMessage message) { lock (this.messageFailures) { int retries; this.messageFailures.TryGetValue(message.MessageId, out retries); this.messageFailures[message.MessageId] = ++retries; return(retries < this.maxRetries); } }
public virtual void Send(EnvelopeMessage message, params Uri[] recipients) { if (!message.IsPopulated()) { return; } Log.Debug(Diagnostics.SendingMessage); this.sender.Send(message, recipients); }
public static Message BuildMsmqMessage(EnvelopeMessage message, Stream serialized) { return(new Message { Label = GetLabel(message), BodyStream = serialized, Recoverable = message.Persistent, TimeToBeReceived = GetTimeToBeReceived(message), }); }
private static void AppendExceptionHeaders(EnvelopeMessage message, Exception exception, int depth) { if (null == exception) return; message.Headers["ExceptionMessage" + depth] = exception.Message; message.Headers["ExceptionStackTrace" + depth] = exception.StackTrace; message.Headers["ExceptionSource" + depth] = exception.Source; AppendExceptionHeaders(message, exception.InnerException, ++depth); }
public virtual void Route(EnvelopeMessage message) { this.CurrentMessage = message; if (!this.poisonMessageHandler.IsPoison(message)) this.TryRoute(message); Log.Debug(Diagnostics.CommittingUnitOfWork); this.unitOfWork.Complete(); this.poisonMessageHandler.ClearFailures(message); }
public virtual void Send(EnvelopeMessage message, params Uri[] recipients) { Log.Debug(Diagnostics.PreparingMessageToSend, message.MessageId, message.LogicalMessages.Count); foreach (var msg in message.LogicalMessages) Log.Verbose(Diagnostics.EnvelopeMessageContains, message.MessageId, msg.GetType().FullName); using (var serializedStream = new MemoryStream()) { this.serializer.Serialize(serializedStream, message); this.Send(BuildMsmqMessage(message, serializedStream), recipients); } }
public virtual bool IsPoison(EnvelopeMessage message) { if (message == null) { return(false); } int retries; this.messageFailures.TryGetValue(message.MessageId, out retries); return(retries >= this.maxRetries); }
private void RouteToHandlers(IRouteMessagesToHandlers router, EnvelopeMessage message) { try { Log.Info(Diagnostics.DispatchingToRouter, this.thread.Name, router.GetType()); router.Route(message); Log.Info(Diagnostics.MessageProcessed, this.thread.Name); } catch (Exception e) { Log.Info(Diagnostics.MessageProcessingFailed, this.thread.Name, e.Message); } }
private static void AppendExceptionHeaders(EnvelopeMessage message, Exception exception, int depth) { if (null == exception) { return; } message.Headers["ExceptionMessage" + depth] = exception.Message; message.Headers["ExceptionStackTrace" + depth] = exception.StackTrace; message.Headers["ExceptionSource" + depth] = exception.Source; AppendExceptionHeaders(message, exception.InnerException, ++depth); }
public virtual void Send(EnvelopeMessage message, params Uri[] recipients) { Log.Debug(Diagnostics.PreparingMessageToSend, message.MessageId, message.LogicalMessages.Count); foreach (var msg in message.LogicalMessages) { Log.Verbose(Diagnostics.EnvelopeMessageContains, message.MessageId, msg.GetType().FullName); } using (var serializedStream = new MemoryStream()) { this.serializer.Serialize(serializedStream, message); this.Send(BuildMsmqMessage(message, serializedStream), recipients); } }
private void SerializeMessage(Stream output, EnvelopeMessage message) { var protoMessage = new ProtocolBufferEnvelopeMessage(message); foreach (var logicalMessage in message.LogicalMessages) { using (var stream = new MemoryStream()) { this.SerializeMessage(stream, logicalMessage); protoMessage.LogicalMessages.Add(stream.ToArray()); } } Serializer.Serialize(output, protoMessage); }
private void TryRoute(EnvelopeMessage message) { Log.Verbose(Diagnostics.RoutingMessagesToHandlers); try { var routes = this.handlerTable.GetHandlers(this.CurrentMessage); foreach (var route in routes.TakeWhile(route => this.ContinueProcessing)) { route.Handle(this.CurrentMessage); } } catch (Exception e) { this.poisonMessageHandler.HandleFailure(message, e); throw; } }
/// <summary> /// /// </summary> /// <param name="senderId"></param> /// <param name="receiverId"></param> /// <param name="envelope"></param> /// <param name="requestConfirm">Only valid for remote clients, since all local calls are confirmed or denied by default.</param> /// <returns></returns> protected override SendToClientResultEnum DoSendToClient(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan?requestConfirmTimeout) { if (receiverId.IsMessageBusIndexValid && (receiverId.MessageBus == this)) //|| receiverId.MessageBus == null)) // This allows for "lost" ids. {// Receiver seems to be a local item. SendToClientResultEnum result = base.DoSendToClient(senderId, receiverId, envelope, requestConfirmTimeout); if (result != SendToClientResultEnum.ClientNotFound) { return(result); } } int clientSocketId = 0; lock (_syncRoot) { if (_remoteClientNetId.TryGetValue(receiverId, out clientSocketId) == false) { return(SendToClientResultEnum.ClientNotFound); } } // Send message. EnvelopeMessage message = new EnvelopeMessage() { Envelope = envelope, Receivers = new ClientId[] { receiverId }, Sender = senderId, RequestResponse = false }; if (ToClient(clientSocketId, message, requestConfirmTimeout) == false) { return(SendToClientResultEnum.Failure); } else { return(SendToClientResultEnum.Success); } }
public virtual void Route(EnvelopeMessage message) { if (message == null || message.LogicalMessages == null || message.LogicalMessages.Count == 0) { // we wore unable to get a valid message from the queue. // probably deserialization issue, and the message has been forwarder to the poison queue. // this will complete the transaction removing the message from the current queue. Log.Debug(Diagnostics.CommittingUnitOfWork); this.unitOfWork.Complete(); return; } this.CurrentMessage = message; if (!this.poisonMessageHandler.IsPoison(message)) { this.TryRoute(message); } Log.Debug(Diagnostics.CommittingUnitOfWork); this.unitOfWork.Complete(); this.poisonMessageHandler.ClearFailures(message); }
public virtual bool IsPoison(EnvelopeMessage message) { return(false); }
private static string GetLabel(EnvelopeMessage message) { var messages = message.LogicalMessages; return LabelFormat.FormatWith(messages.Count, messages.First().GetType().FullName); }
private static TimeSpan GetTimeToBeReceived(EnvelopeMessage message) { if (message.TimeToLive == TimeSpan.MaxValue || message.TimeToLive == TimeSpan.Zero) return MessageQueue.InfiniteTimeout; return message.TimeToLive; }
private static string GetLabel(EnvelopeMessage message) { var messages = message.LogicalMessages; return(LabelFormat.FormatWith(messages.Count, messages.First().GetType().FullName)); }
/// <summary> /// Creates the schema for applications in the migration target. /// </summary> /// <param name="token">The cancellation token.</param> /// <returns>Task used to await the operation.</returns> protected override async Task AnalyzeInternalAsync(CancellationToken token) { // Get parsed BizTalk model from the application model var parsedApplicationGroup = Model.GetSourceModel <ParsedBizTalkApplicationGroup>(); if (parsedApplicationGroup?.Applications == null) { _logger.LogDebug(TraceMessages.SkippingRuleAsSourceModelMissing, RuleName, nameof(SC001SchemaAnalyzer)); } else { _logger.LogDebug(TraceMessages.RunningRule, RuleName, nameof(SC001SchemaAnalyzer)); foreach (var targetApplication in Model.MigrationTarget.MessageBus.Applications) { var sourceApplication = parsedApplicationGroup.Applications.SingleOrDefault(a => a.Application.Name == targetApplication.Name); if (sourceApplication?.Application?.Schemas != null) { var schemaMessages = from schema in sourceApplication.Application.Schemas from messageDefinition in schema.MessageDefinitions where schema.SchemaType == Types.Enumerations.BizTalkSchemaType.Document select new { Schema = schema, MessageDefinition = messageDefinition }; foreach (var schemaMessage in schemaMessages) { var appName = $"{sourceApplication.Application.Name.Replace(".", "-").Replace(" ", string.Empty).Replace(":", "-")}"; var messageName = $"{schemaMessage.MessageDefinition.LocalName.Replace(".", "-").Replace(" ", string.Empty).Replace("/", "-").Replace(":", "-")}"; var resourceMapKey = $"applicationMessage{appName}{messageName}"; // Check if the schema is an envelope or document schema? if (schemaMessage.Schema.IsEnvelope) { // Create an envelope message var envelopeMessage = new EnvelopeMessage { Name = schemaMessage.MessageDefinition.LocalName, Key = $"{targetApplication.Key}:{schemaMessage.MessageDefinition.LocalName}", Description = MigrationTargetResources.SchemaDescription, ResourceMapKey = resourceMapKey, ContentType = MessageContentType.Xml, MessageSchema = new MessageSchema { ResourceKeyRef = schemaMessage.MessageDefinition.ResourceKey, Name = schemaMessage.Schema.Name } }; envelopeMessage.Properties[ModelConstants.MessageType] = schemaMessage.MessageDefinition.MessageType; envelopeMessage.Properties[ModelConstants.TypeName] = schemaMessage.Schema.FullName; targetApplication.Messages.Add(envelopeMessage); _logger.LogDebug(TraceMessages.CreatedEnvelopeMessage, RuleName, schemaMessage.Schema.Name); } else { // Create a document message var documentMessage = new DocumentMessage { Name = schemaMessage.MessageDefinition.LocalName, Key = $"{targetApplication.Key}:{schemaMessage.MessageDefinition.LocalName}", Description = MigrationTargetResources.SchemaDescription, ResourceMapKey = resourceMapKey, ContentType = MessageContentType.Xml, MessageSchema = new MessageSchema { ResourceKeyRef = schemaMessage.MessageDefinition.ResourceKey, Name = schemaMessage.Schema.Name } }; documentMessage.Properties[ModelConstants.MessageType] = schemaMessage.MessageDefinition.MessageType; documentMessage.Properties[ModelConstants.TypeName] = schemaMessage.Schema.FullName; targetApplication.Messages.Add(documentMessage); _logger.LogDebug(TraceMessages.CreatedDocumentMessage, RuleName, schemaMessage.Schema.Name); } } } } _logger.LogDebug(TraceMessages.RuleCompleted, RuleName, nameof(SC001SchemaAnalyzer)); } await Task.CompletedTask.ConfigureAwait(false); }
public virtual void HandleFailure(EnvelopeMessage message, Exception exception) { // no-op }
public virtual void ClearFailures(EnvelopeMessage message) { // no-op }
private void TryRoute(EnvelopeMessage message) { Log.Verbose(Diagnostics.RoutingMessagesToHandlers); try { var routes = this.handlerTable.GetHandlers(this.CurrentMessage); foreach (var route in routes.TakeWhile(route => this.ContinueProcessing)) route.Handle(this.CurrentMessage); } catch (Exception e) { this.poisonMessageHandler.HandleFailure(message, e); throw; } }
public virtual void ClearFailures(EnvelopeMessage message) { if (message != null) this.messageFailures.Remove(message.MessageId); }
public virtual void Route(EnvelopeMessage message) { if (message == null || message.LogicalMessages == null || message.LogicalMessages.Count == 0) { // we wore unable to get a valid message from the queue. // probably deserialization issue, and the message has been forwarder to the poison queue. // this will complete the transaction removing the message from the current queue. Log.Debug(Diagnostics.CommittingUnitOfWork); this.unitOfWork.Complete(); return; } this.CurrentMessage = message; if (!this.poisonMessageHandler.IsPoison(message)) this.TryRoute(message); Log.Debug(Diagnostics.CommittingUnitOfWork); this.unitOfWork.Complete(); this.poisonMessageHandler.ClearFailures(message); }
public static bool IsPopulated(this EnvelopeMessage message) { return(message != null && message.LogicalMessages != null && message.LogicalMessages.Count > 0); }
void _messageClient_MessageReceivedEvent(SocketCommunicator helper, object message) { if (message is EnvelopeMessage) { EnvelopeMessage envelopeMessage = (EnvelopeMessage)message; // Remove the remote message bus index association. envelopeMessage.Sender.LocalMessageBusIndex = ClientId.InvalidMessageBusClientIndex; foreach (ClientId id in envelopeMessage.Receivers) { // Decode the id. id.LocalMessageBusIndex = base.GetClientIndexByGuid(id.Guid); if (id.IsMessageBusIndexValid) { // Assign as a part of the local bus. id.MessageBus = this; if (DoSendToClient(envelopeMessage.Sender, id, envelopeMessage.Envelope, null) != SendToClientResultEnum.Success) { #if Matrix_Diagnostics InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}].", envelopeMessage.ToString())); #endif } } else { #if Matrix_Diagnostics InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}] due to unrecognized receiver id.", envelopeMessage.ToString())); #endif } } } else if (message is ClientsListMessage) {// Received client update from server. ClientsListMessage listMessage = (ClientsListMessage)message; int jef = 0; foreach (var client in listMessage.Ids) { Console.WriteLine("Incoming client id: " + client + " Source type: " + listMessage.SourcesTypes[jef]); jef++; } List <ClientId> existingIds = new List <ClientId>(); lock (_syncRoot) { existingIds.AddRange(_originalServerClientsHotSwap.Values); _originalServerClientsHotSwap.Clear(); _originalServerClientsTypesHotSwap.Clear(); _originalServerClientsSourcesTypesHotNamesSwap.Clear(); // Preprocess Ids, by assigning them new indeces and adding to the local message bus register. for (int i = 0; i < listMessage.Ids.Count; i++) { // Add an original copy to the list. _originalServerClientsHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Ids[i]); _originalServerClientsTypesHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Types[i]); _originalServerClientsSourcesTypesHotNamesSwap.Add(listMessage.Ids[i].Guid, listMessage.SourcesTypes[i]); // Add the client to a new spot. //_clientsHotSwap.Add(null); //int messageBusIndex = _clientsHotSwap.Count - 1; // This type of assignment will also work with multiple entries. // This performs an internal hotswap. //_guidToIndexHotSwap[id.Guid] = messageBusIndex; // Also add to this classes collection. //_localToRemoteId[messageBusIndex] = id; } } foreach (ClientId id in listMessage.Ids) { existingIds.Remove(id); RaiseClientAddedEvent(id); } // Raise for any that were removed. foreach (ClientId id in existingIds) { RaiseClientRemovedEvent(id, true); } } else if (message is RequestClientListUpdateMessage) { SendClientsUpdate(); } else if (message is ClientUpdateMessage) { ClientUpdateMessage updateMessage = (ClientUpdateMessage)message; if (_originalServerClientsHotSwap.ContainsKey(updateMessage.ClientId.Guid)) { RaiseClientUpdateEvent(updateMessage.ClientId); } else { #if Matrix_Diagnostics InstanceMonitor.OperationError(string.Format("Failed to raise update event for client [{0}], since client not found.", updateMessage.ClientId.ToString())); #endif } } else if (message is StateUpdateMessage) { RaiseCounterPartyUpdateEvent("Server", ((StateUpdateMessage)message).State.ToString()); } else { #if Matrix_Diagnostics InstanceMonitor.Warning(string.Format("Message [{0}] not recognized.", message.GetType().Name)); #endif } }
public virtual bool IsPoison(EnvelopeMessage message) { return false; }
void _server_ClientMessageReceivedEvent(SocketMessageServer server, SocketCommunicatorEx client, object message) { ServerAccessControl accessControl = AccessControl; // Check security first. if (accessControl != null && message is AccessMessage == false) { if (accessControl.IsAllowed(ObtainClientAccessControl(client.Id)) == false) { #if Matrix_Diagnostics InstanceMonitor.Info("Message [" + message.ToString() + "] from client [" + client.ToString() + "] not allowed due to access control.", TracerItem.PriorityEnum.Medium); #endif return; } } if (message is EnvelopeMessage) {// Envelope user message. EnvelopeMessage envelopeMessage = (EnvelopeMessage)message; // Remove the remote message bus index association. envelopeMessage.Sender.LocalMessageBusIndex = ClientId.InvalidMessageBusClientIndex; foreach (ClientId id in envelopeMessage.Receivers) { // Assign the id as local id, if it is, otherwise skip it. id.LocalMessageBusIndex = base.GetClientIndexByGuid(id.Guid); if (id.IsMessageBusIndexValid) { id.MessageBus = this; if (DoSendToClient(envelopeMessage.Sender, id, envelopeMessage.Envelope, null) != SendToClientResultEnum.Success) { #if Matrix_Diagnostics InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}].", envelopeMessage.ToString())); #endif } } else { #if Matrix_Diagnostics InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}] due unrecognized receiver id.", envelopeMessage.ToString())); #endif } } } else if (message is ClientsListMessage) {// Message bus system message. ClientsListMessage updateMessage = (ClientsListMessage)message; for (int i = 0; i < updateMessage.Ids.Count; i++) { RegisterClientId(client.Id, updateMessage.Ids[i], updateMessage.Types[i], updateMessage.SourcesTypes[i]); } } else if (message is RequestClientListUpdateMessage) { SendClientsUpdate(client.Id); } else if (message is ClientUpdateMessage) { ClientUpdateMessage updateMessage = (ClientUpdateMessage)message; bool validClient; lock (_syncRoot) { validClient = _remoteClientNetId.ContainsKey(updateMessage.ClientId); } if (validClient) { RaiseClientAddedEvent(updateMessage.ClientId); } else { #if Matrix_Diagnostics InstanceMonitor.OperationError(string.Format("Failed to raise update event for client [{0}], since client not found.", updateMessage.ClientId.ToString())); #endif } } else if (message is AccessMessage) { ClientAccessControl control = ObtainClientAccessControl(client.Id); if (control != null) { control.Update(message as AccessMessage); } } else if (message is StateUpdateMessage) { RaiseCounterPartyUpdateEvent("Client:" + client.Id.ToString(), ((StateUpdateMessage)message).State.ToString()); } else { #if Matrix_Diagnostics InstanceMonitor.Warning(string.Format("Message [{0}] not recognized.", message.GetType().Name)); #endif } }