public static void DeclareExchangeForConnection(this IModel channel, RmqMessagingGatewayConnection connection) { if (connection.Exchange.SupportDelay) channel.ExchangeDeclare(connection.Exchange.Name, "x-delayed-message", connection.Exchange.Durable, autoDelete: false, arguments: new Dictionary<string, object> { { "x-delayed-type", connection.Exchange.Type } }); else channel.ExchangeDeclare(connection.Exchange.Name, connection.Exchange.Type, connection.Exchange.Durable); }
public TestRMQListener(RmqMessagingGatewayConnection connection, string channelName) { _channelName = channelName; _connectionFactory = new ConnectionFactory {Uri = connection.AmpqUri.Uri.ToString()}; _connection = _connectionFactory.CreateConnection(); _channel = _connection.CreateModel(); _channel.DeclareExchangeForConnection(connection); _channel.QueueDeclare(_channelName, false, false, false, null); _channel.QueueBind(_channelName, connection.Exchange.Name, _channelName); }
/// <summary> /// Initializes a new instance of the <see cref="MessageGateway"/> class. /// Use if you need to inject a test logger /// <param name="connection">The amqp uri and exchange to connect to</param> /// </summary> protected MessageGateway(RmqMessagingGatewayConnection connection) { Connection = connection; var connectionPolicyFactory = new ConnectionPolicyFactory(Connection); _retryPolicy = connectionPolicyFactory.RetryPolicy; _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy; _connectionFactory = new ConnectionFactory { Uri = Connection.AmpqUri.Uri.ToString(), RequestedHeartbeat = 30 }; DelaySupported = this is IAmAMessageGatewaySupportingDelay && Connection.Exchange.SupportDelay; }
/// <summary> /// Initializes a new instance of the <see cref="MessageGateway" /> class. /// </summary> /// <param name="connection"></param> /// <param name="queueName">The queue name.</param> /// <param name="routingKey">The routing key.</param> /// <param name="isDurable">Is the queue persisted to disk</param> /// <param name="preFetchSize">0="Don't send me a new message until I?ve finished", 1= "Send me one message at a time", n = number to grab (take care with competing consumers)</param> /// <param name="highAvailability"></param> public RmqMessageConsumer( RmqMessagingGatewayConnection connection, string queueName, string routingKey, bool isDurable, ushort preFetchSize = 1, bool highAvailability = false) : base(connection) { _queueName = queueName; _routingKey = routingKey; _isDurable = isDurable; _preFetchSize = preFetchSize; IsQueueMirroredAcrossAllNodesInTheCluster = highAvailability; _messageCreator = new RmqMessageCreator(); }
/// <summary> /// Initializes a new instance of the <see cref="ConnectionPolicyFactory"/> class. /// Use if you need to inject a test logger /// </summary> /// <param name="connection"></param> public ConnectionPolicyFactory(RmqMessagingGatewayConnection connection) { int retries = connection.AmpqUri.ConnectionRetryCount; int retryWaitInMilliseconds = connection.AmpqUri.RetryWaitInMilliseconds; int circuitBreakerTimeout = connection.AmpqUri.CircuitBreakTimeInMilliseconds; RetryPolicy = Policy.Handle<BrokerUnreachableException>() .Or<Exception>() .WaitAndRetry( retries, retryAttempt => TimeSpan.FromMilliseconds(retryWaitInMilliseconds * Math.Pow(2, retryAttempt)), (exception, timeSpan, context) => { if (exception is BrokerUnreachableException) { _logger.Value.WarnException( "RMQMessagingGateway: BrokerUnreachableException error on connecting to queue {0} exchange {1} on connection {2}. Will retry {3} times", exception, context["queueName"], connection.Exchange.Name, connection.AmpqUri.GetSanitizedUri(), retries); } else { _logger.Value.WarnException( "RMQMessagingGateway: Exception on connection to queue {0} via exchange {1} on connection {2}", exception, context["queueName"], connection.Exchange.Name, connection.AmpqUri.GetSanitizedUri()); throw exception; } }); CircuitBreakerPolicy = Policy.Handle<BrokerUnreachableException>().CircuitBreaker(1, TimeSpan.FromMilliseconds(circuitBreakerTimeout)); }
public ManagementAndMonitoringService() { log4net.Config.XmlConfigurator.Configure(); var container = new TinyIoCContainer(); var handlerFactory = new TinyIocHandlerFactory(container); var messageMapperFactory = new TinyIoCMessageMapperFactory(container); container.Register<IHandleRequests<GreetingCommand>, GreetingCommandHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register<GreetingCommand, GreetingCommandHandler>(); //create policies var retryPolicy = Policy .Handle<Exception>() .WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) }); var circuitBreakerPolicy = Policy .Handle<Exception>() .CircuitBreaker(1, TimeSpan.FromMilliseconds(500)); var policyRegistry = new PolicyRegistry() { {CommandProcessor.RETRYPOLICY, retryPolicy}, {CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy} }; //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { {typeof(GreetingCommand), typeof(GreetingCommandMessageMapper)} }; var rmqGatewayMessages = new RmqMessagingGatewayConnection { AmpqUri = new AmqpUriSpecification(new Uri("amqp://*****:*****@localhost:5672/%2f")), Exchange = new Exchange("paramore.brighter.exchange"), }; var rmqGatewayMonitoring = new RmqMessagingGatewayConnection { AmpqUri = new AmqpUriSpecification(new Uri("amqp://*****:*****@localhost:5672/%2f")), Exchange = new Exchange("paramore.brighter.exchange"), }; //create the gateway var rmqMessageConsumerFactory = new RmqMessageConsumerFactory(rmqGatewayMessages); var rmqMessageProducerFactory = new RmqMessageProducerFactory(rmqGatewayMessages); var connections = new List<Connection> { }; var builder = DispatchBuilder .With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build() ) .MessageMappers(messageMapperRegistry) .ChannelFactory(new InputChannelFactory(rmqMessageConsumerFactory, rmqMessageProducerFactory)) .Connections(connections); _dispatcher = builder.Build(); var controlBusBuilder = ControlBusReceiverBuilder .With() .Dispatcher(_dispatcher) .ProducerFactory(rmqMessageProducerFactory) .ChannelFactory(new InputChannelFactory(rmqMessageConsumerFactory, rmqMessageProducerFactory)) as ControlBusReceiverBuilder; _controlDispatcher = controlBusBuilder.Build(Environment.MachineName + "." + "ManagementAndMonitoring"); container.Register<IAmAControlBusSender>(new ControlBusSenderFactory().Create( new SqliteMessageStore( new SqliteMessageStoreConfiguration( "DataSource=\"" + Path.Combine(Path.GetDirectoryName(typeof(GreetingCommand).GetTypeInfo().Assembly.CodeBase.Substring(8)), "App_Data\\MessageStore.sdf") + "\"", "Messages") ), new RmqMessageProducer(rmqGatewayMonitoring))); }
public NotSupportedRmqMessageConsumer(RmqMessagingGatewayConnection connection, string queueName, string routingKey, bool isDurable, ushort preFetchSize, bool isHighAvailability) : base(connection, queueName, routingKey, isDurable, preFetchSize, isHighAvailability) { }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageConsumerFactory"/> class. /// </summary> public RmqMessageConsumerFactory(RmqMessagingGatewayConnection connection) { _connection = connection; }
public GreetingService() { log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config")); var container = new TinyIoCContainer(); var handlerFactory = new TinyIocHandlerFactory(container); var messageMapperFactory = new TinyIoCMessageMapperFactory(container); container.Register<IHandleRequests<GreetingEvent>, GreetingEventHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register<GreetingEvent, GreetingEventHandler>(); //create policies var retryPolicy = Policy .Handle<Exception>() .WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) }); var circuitBreakerPolicy = Policy .Handle<Exception>() .CircuitBreaker(1, TimeSpan.FromMilliseconds(500)); var policyRegistry = new PolicyRegistry() { {CommandProcessor.RETRYPOLICY, retryPolicy}, {CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy} }; //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { {typeof(GreetingEvent), typeof(GreetingEventMessageMapper)} }; //create the gateway var rmqConnnection = new RmqMessagingGatewayConnection { AmpqUri = new AmqpUriSpecification(new Uri("amqp://*****:*****@localhost:5672/%2f")), Exchange = new Exchange("paramore.brighter.exchange"), }; var rmqMessageConsumerFactory = new RmqMessageConsumerFactory(rmqConnnection); var rmqMessageProducerFactory = new RmqMessageProducerFactory(rmqConnnection); // < add connectionName = "paramore.example.greeting" channelName = "greeting." routingKey = "greeting.command" dataType = "Greetings.Ports.Commands.GreetingEvent" timeOutInMilliseconds = "200" /> // Service Activator connections var connections = new List<paramore.brighter.serviceactivator.Connection> { new paramore.brighter.serviceactivator.Connection( new ConnectionName("paramore.example.greeting"), new InputChannelFactory(rmqMessageConsumerFactory, rmqMessageProducerFactory), typeof(GreetingEvent), new ChannelName("greeting.event"), "greeting.event", timeoutInMilliseconds: 200) }; var builder = DispatchBuilder .With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build() ) .MessageMappers(messageMapperRegistry) .ChannelFactory(new InputChannelFactory(rmqMessageConsumerFactory, rmqMessageProducerFactory)) .Connections(connections); _dispatcher = builder.Build(); }
/// <summary> /// Initializes a new instance of the <see cref="MessageGateway" /> class. /// </summary> /// <param name="connection">The connection information needed to talk to RMQ</param> public RmqMessageProducer(RmqMessagingGatewayConnection connection) : base(connection) { }
public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); var container = new TinyIoCContainer(); var handlerFactory = new TinyIocHandlerFactory(container); var messageMapperFactory = new TinyIoCMessageMapperFactory(container); container.Register<IHandleRequests<GreetingEvent>, GreetingEventHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register<GreetingEvent, GreetingEventHandler>(); //create policies var retryPolicy = Policy .Handle<Exception>() .WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) }); var circuitBreakerPolicy = Policy .Handle<Exception>() .CircuitBreaker(1, TimeSpan.FromMilliseconds(500)); var policyRegistry = new PolicyRegistry() { {CommandProcessor.RETRYPOLICY, retryPolicy}, {CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy} }; //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { {typeof(GreetingEvent), typeof(GreetingEventMessageMapper)} }; //create the gateway var rmqConnnection = new RmqMessagingGatewayConnection { AmpqUri = new AmqpUriSpecification(new Uri("amqp://*****:*****@localhost:5672/%2f")), Exchange = new Exchange("paramore.brighter.exchange"), }; var rmqMessageConsumerFactory = new RmqMessageConsumerFactory(rmqConnnection ); var rmqMessageProducerFactory = new RmqMessageProducerFactory(rmqConnnection ); // Service Activator connections var connections = new List<paramore.brighter.serviceactivator.Connection> { new paramore.brighter.serviceactivator.Connection( new ConnectionName("paramore.example.greeting"), new InputChannelFactory(rmqMessageConsumerFactory, rmqMessageProducerFactory), typeof(GreetingEvent), new ChannelName("greeting.event"), "greeting.event", timeoutInMilliseconds: 200) }; var builder = DispatchBuilder .With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build() ) .MessageMappers(messageMapperRegistry) .ChannelFactory(new InputChannelFactory(rmqMessageConsumerFactory, rmqMessageProducerFactory)) .Connections(connections); var dispatcher = builder.Build(); dispatcher.Receive(); Console.WriteLine("Press Enter to stop ..."); Console.ReadLine(); dispatcher.End().Wait(); }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageProducerFactory"/> class. /// </summary> /// <param name="connection"></param> public RmqMessageProducerFactory(RmqMessagingGatewayConnection connection) { _connection = connection; }