protected override async Task OnStart(IBusContext context) { var routingTable = settings.Get <UnicastRoutingTable>(); var endpointInstances = settings.Get <EndpointInstances>(); routingTable.AddDynamic((list, bag) => FindDestination(list)); endpointInstances.AddDynamic(FindInstances); subscription = await dataBackplane.GetAllAndSubscribeToChanges("NServiceBus.HandledMessages", async e => { var deserializedData = JsonConvert.DeserializeObject <HandledMessageDeclaration>(e.Data); var endpointName = new EndpointName(deserializedData.EndpointName); var instanceName = new EndpointInstanceName(endpointName, deserializedData.UserDiscriminator, deserializedData.TransportDiscriminator); var types = deserializedData.HandledMessageTypes.Select(x => Type.GetType(x, false)) .Where(x => x != null) .ToArray(); EndpointInstanceInfo instanceInfo; if (!instanceInformation.TryGetValue(instanceName, out instanceInfo)) { var newInstanceInformation = new Dictionary <EndpointInstanceName, EndpointInstanceInfo>(instanceInformation); instanceInfo = new EndpointInstanceInfo(); newInstanceInformation[instanceName] = instanceInfo; instanceInformation = newInstanceInformation; } if (deserializedData.Active) { instanceInfo.Activate(deserializedData.Timestamp); Logger.InfoFormat("Instance {0} active (heartbeat).", instanceName); } else { instanceInfo.Deactivate(); Logger.InfoFormat("Instance {0} deactivated.", instanceName); } await UpdateCaches(endpointName, instanceName, types); }, async e => { var deserializedData = JsonConvert.DeserializeObject <HandledMessageDeclaration>(e.Data); var endpointName = new EndpointName(deserializedData.EndpointName); var instanceName = new EndpointInstanceName(endpointName, deserializedData.UserDiscriminator, deserializedData.TransportDiscriminator); Logger.InfoFormat("Instance {0} removed from routing tables.", instanceName); await UpdateCaches(endpointName, instanceName, new Type[0]); instanceInformation.Remove(instanceName); }); sweepTimer = new Timer(state => { foreach (var info in instanceInformation) { if (!info.Value.Sweep(DateTime.UtcNow, heartbeatTimeout)) { Logger.InfoFormat("Instance {0} deactivated (heartbeat timeout).", info.Key); } } }, null, sweepPeriod, sweepPeriod); }
protected override async Task OnStart(IBusContext context) { var routingTable = settings.Get<UnicastRoutingTable>(); var endpointInstances = settings.Get<EndpointInstances>(); routingTable.AddDynamic((list, bag) => FindDestination(list)); endpointInstances.AddDynamic(FindInstances); subscription = await dataBackplane.GetAllAndSubscribeToChanges("NServiceBus.HandledMessages", async e => { var deserializedData = JsonConvert.DeserializeObject<HandledMessageDeclaration>(e.Data); var endpointName = new EndpointName(deserializedData.EndpointName); var instanceName = new EndpointInstanceName(endpointName, deserializedData.UserDiscriminator, deserializedData.TransportDiscriminator); var types = deserializedData.HandledMessageTypes.Select(x => Type.GetType(x, false)) .Where(x => x != null) .ToArray(); EndpointInstanceInfo instanceInfo; if (!instanceInformation.TryGetValue(instanceName, out instanceInfo)) { var newInstanceInformation = new Dictionary<EndpointInstanceName, EndpointInstanceInfo>(instanceInformation); instanceInfo = new EndpointInstanceInfo(); newInstanceInformation[instanceName] = instanceInfo; instanceInformation = newInstanceInformation; } if (deserializedData.Active) { instanceInfo.Activate(deserializedData.Timestamp); Logger.InfoFormat("Instance {0} active (heartbeat).", instanceName); } else { instanceInfo.Deactivate(); Logger.InfoFormat("Instance {0} deactivated.", instanceName); } await UpdateCaches(endpointName, instanceName, types); }, async e => { var deserializedData = JsonConvert.DeserializeObject<HandledMessageDeclaration>(e.Data); var endpointName = new EndpointName(deserializedData.EndpointName); var instanceName = new EndpointInstanceName(endpointName, deserializedData.UserDiscriminator, deserializedData.TransportDiscriminator); Logger.InfoFormat("Instance {0} removed from routing tables.", instanceName); await UpdateCaches(endpointName, instanceName, new Type[0]); instanceInformation.Remove(instanceName); }); sweepTimer = new Timer(state => { foreach (var info in instanceInformation) { if (!info.Value.Sweep(DateTime.UtcNow, heartbeatTimeout)) { Logger.InfoFormat("Instance {0} deactivated (heartbeat timeout).", info.Key); } } }, null, sweepPeriod, sweepPeriod); }