public void Listen(CancellationToken cancellationToken, int listenerId) { cancellationToken.ThrowIfCancellationRequested(); Message newMessage = MSMessageQueue.Receive(); var body = newMessage.Body as byte[]; var message = Encoding.UTF8.GetString(body); Log.Debug(x => x("Message queue listener received {0}", message)); if (IntegrationJobTypes != null && !IntegrationJobTypes.Any()) { return; } var type = IntegrationJobTypes.FirstOrDefault(t => t.FullName.Equals(message)); var integrationJob = Activator.CreateInstance(type) as IIntegrationJob; try { if (integrationJob != null) { integrationJob.Run(); } } catch (Exception exception) { Log.Error(x => x("Integration job did not run successfully ({0})}", message), exception); } }
/// <summary> /// Registers the integration jobs. /// Resolve the integration point type (specified in a job's parameters). /// Configure the integration point type with a configuration, based on a parameter's name. /// </summary> public void RegisterIntegrationJobs() { IntegrationJobTypes.ForEach(jobType => { Func <ParameterInfo[], object[]> resolveParameters = infos => { var resolvedParameters = new List <object>(); foreach (var parameterInfo in infos) { var parameterType = parameterInfo.ParameterType; // The type of integration point (e.g. IElasticClient) var parameterName = parameterInfo.ParameterType.Name; // The name of the configuration endpoint (e.g. "MyElasticClient") // If the parameter implements IIntegrationPoint, resolve it's configuration type from the container. if (typeof(IIntegrationPoint).IsAssignableFrom(parameterType)) { var configType = parameterType.GetInterface(typeof(IIntegrationPoint <IIntegrationPointConfiguration>).Name).GetGenericArguments().Single();; resolvedParameters.Add(Activator.CreateInstance(parameterType, Container.Resolve(configType, parameterName))); } } return(resolvedParameters.Cast <object>().ToArray()); }; var constructors = jobType.GetConstructors(); if (constructors.Count() == 1 && !constructors.Single().GetParameters().Any()) // Handle Default Constructor case. { Container.RegisterType(jobType, new InjectionFactory((c, t, s) => Activator.CreateInstance(jobType))); } else { // Use the first constructor with parameters. var constructor = constructors.First(x => x.GetParameters().Any()); Container.RegisterType(jobType, new InjectionFactory((c, t, s) => Activator.CreateInstance(jobType, resolveParameters(constructor.GetParameters())))); } }); }
public Type GetRegisteredJobTypeByName(string jobTypeName) { var jobTypes = IntegrationJobTypes.Where(x => x.FullName == jobTypeName); if (jobTypes.Any()) { return(jobTypes.Single()); } Log.Warn(x => x("JobType is not registered: {0}", jobTypeName)); return(null); }
public void Listen(CancellationToken cancellationToken, int listenerId) { _listenerId = listenerId; Connection = MessageQueueConnection.GetConnectionFactory().CreateConnection(); using (var channel = Connection.CreateModel()) { Consumer = new QueueingBasicConsumer(channel); channel.BasicConsume(RabbitMQConfiguration.QueueName, true, Consumer); Log.Info(x => x("(Id={0}) Waiting for messages...", _listenerId)); while (true) { var message = new DispatchTrigger(); try { cancellationToken.ThrowIfCancellationRequested(); var eventArgs = (BasicDeliverEventArgs)Consumer.Queue.Dequeue(); var body = eventArgs.Body; message = JsonConvert.DeserializeObject <DispatchTrigger>(Encoding.UTF8.GetString(body)); Log.Debug(x => x("Message queue listener received {0}", message)); if (IntegrationJobTypes != null && !IntegrationJobTypes.Any()) { continue; } var type = IntegrationJobTypes.FirstOrDefault(t => t.FullName.Equals(message.JobType)); var integrationJob = UnityContainer.Resolve(type) as IIntegrationJob; if (integrationJob != null) { if (integrationJob is IParameterizedJob) { var parameterizedJob = integrationJob as IParameterizedJob; parameterizedJob.Parameters = message.Parameters; Log.Info(x => x("Running job: {0} with parameters {1}", integrationJob, message.Parameters)); parameterizedJob.Run(); } else { Log.Info(x => x("Running job: {0}", integrationJob)); integrationJob.Run(); } Log.Info(x => x("Integration job ran successfully: {0}", integrationJob)); } } catch (OperationCanceledException exception) { Log.Info(x => x("Message queue listener (id: {0}), listening on queue \"{1}\", has gracefully shutdown.", _listenerId, RabbitMQConfiguration.QueueName), exception); return; } catch (IntegrationJobRunFailureException exception) { Log.Error(x => x("Integration job did not run successfully ({0}).", message.JobType), exception); } catch (EndOfStreamException exception) { Log.Debug(x => x("The message queue ({0}) has closed.", RabbitMQConfiguration.QueueName), exception); } catch (Exception exception) { Log.Error("Issue receiving/decoding dispatch message or running job.", exception); } } } }