/// <summary> /// Adds a handling rule based on message name pattern matching /// </summary> /// <param name="configuration">The configuration object to which the /// handling rule is being added</param> /// <param name="namePattern">A regular expression used to match /// message names</param> /// <param name="messageHandler">The message handler to which matching /// messages will be routed</param> /// <param name="queueName">(Optional) The name of the queue to which /// the handler will be attached</param> /// <param name="queueOptions">(Optional) Options for how queued messages for the handler /// should be processed</param> public static void AddHandlingRule(this PlatibusConfiguration configuration, string namePattern, IMessageHandler messageHandler, QueueName queueName = null, QueueOptions queueOptions = null) { var specification = new MessageNamePatternSpecification(namePattern); var handlingRule = new HandlingRule(specification, messageHandler, queueName, queueOptions); configuration.AddHandlingRule(handlingRule); }
/// <summary> /// Adds a handling rule based on the a name pattern and action delegate /// </summary> /// <typeparam name="TContent">The type of deserialized message /// content expected</typeparam> /// <param name="configuration">The configuration object to which the /// handling rule is being added</param> /// <param name="namePattern">A regular expression used to match message /// names for the handler</param> /// <param name="handleContent">An action delegate that will be used to /// handle messages</param> /// <param name="queueName">(Optional) The name of the queue to which /// the handler will be attached</param> /// <param name="queueOptions">(Optional) Options for how queued messages for the handler /// should be processed</param> public static void AddHandlingRule <TContent>(this PlatibusConfiguration configuration, string namePattern, Action <TContent, IMessageContext> handleContent, QueueName queueName = null, QueueOptions queueOptions = null) { var specification = new MessageNamePatternSpecification(namePattern); var messageHandler = DelegateMessageHandler.For(handleContent); var handlingRule = new HandlingRule(specification, messageHandler, queueName, queueOptions); configuration.AddHandlingRule(handlingRule); }
/// <summary> /// Adds a handling rule based on message name pattern matching /// </summary> /// <typeparam name="TContent">The type of deserialized message /// content expected</typeparam> /// <param name="configuration">The configuration object to which the /// handling rule is being added</param> /// <param name="namePattern">A regular expression used to match /// message names</param> /// <param name="messageHandler">The message handler to which matching /// messages will be routed</param> /// <param name="queueName">(Optional) The name of the queue to which /// the handler will be attached</param> /// <param name="queueOptions">(Optional) Options for how queued messages for the handler /// should be processed</param> public static void AddHandlingRule <TContent>(this PlatibusConfiguration configuration, string namePattern, IMessageHandler <TContent> messageHandler, QueueName queueName = null, QueueOptions queueOptions = null) { var specification = new MessageNamePatternSpecification(namePattern); var genericMessageHandler = GenericMessageHandlerAdapter.For(messageHandler); var handlingRule = new HandlingRule(specification, genericMessageHandler, queueName, queueOptions); configuration.AddHandlingRule(handlingRule); }
/// <summary> /// Initializes send rules in the supplied <paramref name="configuration"/> based on the /// properties of the specified <paramref name="configSection"/> /// </summary> /// <param name="configuration">The configuration to initialize</param> /// <param name="configSection">The configuration section containing the send rule /// properties</param> protected virtual void InitializeSendRules(TConfiguration configuration, PlatibusConfigurationSection configSection) { IEnumerable <SendRuleElement> sendRules = configSection.SendRules; foreach (var sendRule in sendRules) { var messageSpec = new MessageNamePatternSpecification(sendRule.NamePattern); var endpointName = (EndpointName)sendRule.Endpoint; configuration.AddSendRule(new SendRule(messageSpec, endpointName)); } }
/// <summary> /// Adds a handling rule based on message name pattern matching /// </summary> /// <typeparam name="TContent">The type of deserialized message /// content expected</typeparam> /// <param name="configuration">The configuration object to which the /// handling rule is being added</param> /// <param name="namePattern">A regular expression used to match /// message names</param> /// <param name="handlerFactory">A factory method for getting an instance of the handler /// (may return a singleton or scoped handler instance)</param> /// <param name="queueName">(Optional) The name of the queue to which /// the handler will be attached</param> /// <param name="queueOptions">(Optional) Options for how queued messages for the handler /// should be processed</param> public static void AddHandlingRule <TContent>(this PlatibusConfiguration configuration, string namePattern, Func <IMessageHandler <TContent> > handlerFactory, QueueName queueName = null, QueueOptions queueOptions = null) { var specification = new MessageNamePatternSpecification(namePattern); var messageHandler = new DelegateMessageHandler((content, context, ct) => { var handler = GenericMessageHandlerAdapter.For(handlerFactory()); return(handler.HandleMessage(content, context, ct)); }); var handlingRule = new HandlingRule(specification, messageHandler, queueName, queueOptions); configuration.AddHandlingRule(handlingRule); }
/// <summary> /// Initializes send rules in the supplied <paramref name="platibusConfiguration"/> based on the /// properties of the specified <paramref name="configuration"/> /// </summary> /// <param name="platibusConfiguration">The configuration to initialize</param> /// <param name="configuration">The configuration section containing the send rule /// properties</param> protected virtual void InitializeSendRules(TConfiguration platibusConfiguration, IConfiguration configuration) { var sendRulesSection = configuration?.GetSection("sendRules"); if (sendRulesSection == null) { return; } foreach (var sendRuleSection in sendRulesSection.GetChildren()) { var messageSpec = new MessageNamePatternSpecification(sendRuleSection["namePattern"]); var endpointName = (EndpointName)sendRuleSection["endpoint"]; platibusConfiguration.AddSendRule(new SendRule(messageSpec, endpointName)); } }
/// <summary> /// Uses reflection to add handling rules for every implemented <see cref="IMessageHandler{TContent}"/> /// interface. /// </summary> /// <remarks> /// <para>A handling rule will be added for implemented <see cref="IMessageHandler{TContent}"/> interface /// of <paramref name="handlerType"/> according to the following convention:</para> /// <list type="bullet"> /// <item>The name pattern will be an exact match of the <see cref="MessageName"/> returned by the /// <see cref="IMessageNamingService"/> in the specified <paramref name="configuration"/></item> /// <item>The handling function will retrieve an instance of the handler object using the supplied /// <paramref name="handlerFactory"/> and invoke the appropriate <see cref="IMessageHandler{TContent}.HandleMessage"/> /// method (via reflection)</item> /// <item>The designated queue name will be a hash derived from the full names of the <paramref name="handlerType"/> /// and the type of message that is handled</item> /// </list> /// </remarks> /// <param name="configuration">The Platibus configuration to which the handling rules will be added</param> /// <param name="handlerType">The type of handler returned by the factory method</param> /// <param name="handlerFactory">(Optional) A factory method for getting an instance of the handler (may /// return a singleton or scoped handler instance). If no factory method is specified, then the /// default constructor will be used.</param> /// <param name="queueNameFactory">(Optional) A factory method that will return an appropriate queue /// name for each combination of handler type and message type</param> /// <param name="queueOptions">(Optional) Options for how queued messages for the handler /// should be processed</param> public static void AddHandlingRulesForType(this PlatibusConfiguration configuration, Type handlerType, Func <object> handlerFactory = null, QueueNameFactory queueNameFactory = null, QueueOptions queueOptions = null) { var diagnosticService = configuration.DiagnosticService; var autoBindInterfaces = handlerType .GetInterfaces() .Where(i => i.IsGenericType && typeof(IMessageHandler <>).IsAssignableFrom(i.GetGenericTypeDefinition())); if (handlerFactory == null) { handlerFactory = () => Activator.CreateInstance(handlerType); } if (queueNameFactory == null) { queueNameFactory = DeriveQueueName; } foreach (var autoBindInterface in autoBindInterfaces) { var messageType = autoBindInterface.GetGenericArguments().First(); var messageName = configuration.MessageNamingService.GetNameForType(messageType); var specification = new MessageNamePatternSpecification("^" + Regex.Escape(messageName) + "$"); var queueName = queueNameFactory(handlerType, messageType); var args = new[] { messageType, typeof(IMessageContext), typeof(CancellationToken) }; var method = autoBindInterface.GetMethod("HandleMessage", args); if (method == null) { continue; } var handlerActivation = new HandlerActivation(diagnosticService, handlerType, handlerFactory, method); var rule = new HandlingRule(specification, handlerActivation, queueName, queueOptions); configuration.AddHandlingRule(rule); } }
/// <inheritdoc /> /// <summary> /// Initializes a new <see cref="T:Platibus.Config.LoopbackConfiguration" /> with a preconfigured /// <paramref name="diagnosticService" /> /// </summary> /// <param name="diagnosticService">(Optional) The service through which diagnostic events /// are reported and processed</param> public LoopbackConfiguration(IDiagnosticService diagnosticService) : base(diagnosticService, new LoopbackEndpoints(LoopbackEndpoint, LoopbackUri)) { var allMessages = new MessageNamePatternSpecification(".*"); base.AddSendRule(new SendRule(allMessages, LoopbackEndpoint)); }