private static BusClient Init() { var userName = Environment.GetEnvironmentVariable("BUS_USERNAME"); var password = Environment.GetEnvironmentVariable("BUS_PASSWORD"); var port = int.Parse(Environment.GetEnvironmentVariable("BUS_PORT")); var virtualHost = Environment.GetEnvironmentVariable("BUS_VIRTUAL_HOST"); var hostNames = Environment.GetEnvironmentVariable("BUS_HOSTNAME")?.Split(',').ToList(); var busConfig = new RawRabbitConfiguration { Username = userName, Password = password, Port = port, VirtualHost = virtualHost, Hostnames = hostNames }; return(RawRabbitFactory.CreateSingleton(new RawRabbitOptions { ClientConfiguration = busConfig, Plugins = p => p .UseStateMachine() .UseGlobalExecutionId() .UseHttpContext() .UseCustomQueueSuffix() .UseMessageContext(ctx => new MessageContext { GlobalRequestId = Guid.Parse(ctx.GetGlobalExecutionId()) }) })); }
public ThreadBasedChannelFactory(RawRabbitConfiguration config, IConnectionFactory connectionFactory) { _connectionFactory = connectionFactory; _accessDictionary = new ConcurrentDictionary <IModel, DateTime>(); _config = config; _threadChannels = new ThreadLocal <IModel>(true); try { _logger.LogDebug("Connecting to primary host."); _connection = _connectionFactory.CreateConnection(_config.Hostnames); _logger.LogInformation("Successfully established connection."); } catch (BrokerUnreachableException e) { _logger.LogError("Unable to connect to broker", e); throw e.InnerException; } _closeTimer = new System.Threading.Timer(state => { var enumerator = _accessDictionary.GetEnumerator(); while (enumerator.MoveNext()) { if (DateTime.Now - enumerator.Current.Value > _config.RequestTimeout) { DateTime lastUsed; if (_accessDictionary.TryRemove(enumerator.Current.Key, out lastUsed)) { _logger.LogInformation($"Channel {enumerator.Current.Key.ChannelNumber} was last used {lastUsed}. Closing..."); enumerator.Current.Key.Close(); } } } }, null, _config.RequestTimeout, _config.RequestTimeout); }
public static void InitializeContainer(this IApplicationBuilder app, Container container, IHostingEnvironment env) { container.RegisterMvcControllers(app); container.RegisterSingleton(() => { var sched = new StdSchedulerFactory().GetScheduler().Result; sched.JobFactory = new SimpleInjectiorJobFactory(container); return(sched); }); container.RegisterSingleton <IBusClient>(() => { var busConfig = new RawRabbitConfiguration { Username = "******", Password = "******", Port = 5672, VirtualHost = "/", Hostnames = { "localhost" } }; var client = BusClientFactory.CreateDefault(busConfig); return(client); }); container.RegisterSingleton <CoreRabbitService>(); container.Verify(); }
public static void AddRabbitMq(this ContainerBuilder builder) { builder.Register(context => { var configuration = context.Resolve <IConfiguration>(); var section = configuration.GetSection("rabbitmq"); var rawRabbitConfiguration = new RawRabbitConfiguration(); section.Bind(rawRabbitConfiguration); return(rawRabbitConfiguration); }).SingleInstance(); var assembly = Assembly.GetCallingAssembly(); builder.RegisterAssemblyTypes(assembly) .AsClosedTypesOf(typeof(IEventHandler <>)) .InstancePerDependency(); builder.RegisterAssemblyTypes(assembly) .AsClosedTypesOf(typeof(ICommandHandler <>)) .InstancePerDependency(); builder.RegisterType <BusPublisher>().As <IBusPublisher>() .InstancePerDependency(); ConfigureRabbitMq(builder); }
private static async Task RemoveEntities <TEntity>(string entityName, RawRabbitConfiguration config) where TEntity : IRabbtMqEntity { var tasks = new List <Task>(); using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(config.Username, config.Password) }) using (var httpClient = new HttpClient(handler)) { foreach (var hostname in config.Hostnames) { var response = await httpClient.GetAsync($"http://{hostname}:15672/api/{entityName}"); var entityStr = await response.Content.ReadAsStringAsync(); var entites = JsonConvert.DeserializeObject <List <TEntity> >(entityStr); foreach (var entity in entites) { if (string.IsNullOrEmpty(entity.Name) || entity.Name.StartsWith("amq.")) { continue; } var removeEntityTask = httpClient .DeleteAsync(new Uri($"http://{hostname}:15672/api/{entityName}/{Uri.EscapeDataString(entity.Vhost)}/{Uri.EscapeUriString(entity.Name)}")); tasks.Add(removeEntityTask); } } } await Task.WhenAll(tasks); }
public RabbitQueue(Settings settings) { try { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var config = new RawRabbitConfiguration() { Port = settings.QueuePortNumber, Username = settings.QueueUsername, Password = settings.QueuePassword, VirtualHost = "/", Hostnames = new List <string> { settings.QueueHostName } }; _busClient = BusClientFactory.CreateDefault(config); } catch (Exception ex) { Log.Error(ex, "Error establishing connection to Queue"); throw; } }
public static IServiceCollection AddRabbitMq( this IServiceCollection services, IConfiguration configuration) { var section = configuration.GetSection("RabbitMq"); if (!section.Exists()) { throw new InvalidOperationException("Missing 'RabbitMq' section in config!"); } var options = new RawRabbitConfiguration(); section.Bind(options); IBusClient client = null; Policy .Handle <BrokerUnreachableException>().Or <SocketException>() .WaitAndRetry(3, _ => TimeSpan.FromSeconds(10)) .Execute(() => { client = RawRabbitFactory.CreateSingleton( new RawRabbitOptions { ClientConfiguration = options }); }); services.AddSingleton <IBusClient>(_ => client); services.AddSingleton <IServiceBus, RabbitMqBus>(); services.AddSingleton <IBusBuilder, RabbitMqBusBuilder>(); return(services); }
public Startup(IHostingEnvironment env, ILogger <Startup> logger) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); env.ConfigureNLog("NLog.config"); logger.LogInformation("Service started"); var busConfig = new RawRabbitConfiguration { Username = "******", Password = "******", Port = 5672, VirtualHost = "/", Hostnames = { "localhost" } }; var client = BusClientFactory.CreateDefault(busConfig); client.SubscribeAsync <string>(async(json, context) => { var msg = JsonConvert.DeserializeObject <EmailMessage>(json); logger.LogInformation($"Sending email - {msg.EmailAddress} with message {msg.Message}"); }, (cfg) => cfg.WithExchange( ex => ex.WithName("email_exchange")).WithQueue( q => q.WithName("email_queue")).WithRoutingKey("email_command")); }
public static IBusClient CreateDefault(RawRabbitConfiguration config) { var addCfg = new Action <IServiceCollection>(s => s.AddSingleton(p => config)); var services = new ServiceCollection().AddRawRabbit(null, addCfg); return(CreateDefault(services)); }
private void AddRabbitMqService(IServiceCollection services, IConfigurationSection configuration) { var options = new RawRabbitConfiguration(); configuration.Bind(options); services.AddSingleton <IBusClient>(_ => BusClientFactory.CreateDefault(options)); }
public static void Register(ContainerBuilder builder, RawRabbitConfiguration configuration, int retryAttempts = 5) { var policy = Policy .Handle <ConnectFailureException>() .Or <BrokerUnreachableException>() .Or <IOException>() .WaitAndRetry(retryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, retryCount, context) => { Logger.Error(exception, "Can not connect to RabbitMQ. " + $"Retries: {retryCount}, duration: {timeSpan}"); } ); builder.RegisterInstance(configuration).SingleInstance(); policy.Execute(() => { builder.Register(context => RawRabbitFactory.CreateInstanceFactory(new RawRabbitOptions { DependencyInjection = ioc => ioc.AddSingleton(configuration) })) .As <IInstanceFactory>() .SingleInstance(); builder.Register(context => context.Resolve <IInstanceFactory>().Create()); }); }
private static Task RemoveEntities <TEntity>(string entityName, RawRabbitConfiguration config) where TEntity : IRabbtMqEntity { var tasks = new List <Task>(); foreach (var hostname in config.Hostnames) { var credentials = new NetworkCredential(config.Username, config.Password); var queuesTask = new WebRequester() .WithUrl($"http://{hostname}:15672/api/{entityName}") .WithMethod(HttpMethod.Get) .WithCredentials(credentials) .PerformAsync <List <TEntity> >() .ContinueWith(entitiesTask => { var removeTask = new List <Task>(); foreach (var entity in entitiesTask.Result) { if (string.IsNullOrEmpty(entity.Name) || entity.Name.StartsWith("amq.")) { continue; } var removeEntityTask = new WebRequester() .WithUrl($"http://{hostname}:15672/api/{entityName}/{Uri.EscapeDataString(entity.Vhost)}/{Uri.EscapeUriString(entity.Name)}") .WithMethod(HttpMethod.Delete) .WithCredentials(credentials) .GetResponseAsync(); removeTask.Add(removeEntityTask); } return(Task.WhenAll(removeTask)); }); tasks.Add(queuesTask); } return(Task.WhenAll(tasks)); }
public ChannelFactory(IConnectionFactory connectionFactory, RawRabbitConfiguration config, ConnectionPolicies policies = null) : base(connectionFactory, config) { CreateChannelPolicy = policies?.CreateChannel ?? Policy.NoOpAsync(); ConnectPolicy = policies?.Connect ?? Policy.NoOpAsync(); GetConnectionPolicy = policies?.GetConnection ?? Policy.NoOpAsync(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connection = @"Server=db;Database=TestWebApi;User=sa;Password=QAZwsx123_longone;"; services.AddDbContext <TestWebApiDbContext>( options => options.UseSqlServer(connection)); services.AddMvc(); var policy = Policy.Handle <Exception>() .WaitAndRetry(10, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { Debug.WriteLine("RABBIT MQ FAILED"); }); policy.Execute(() => { var busConfig = new RawRabbitConfiguration { Username = "******", Password = "******", Port = 5672, VirtualHost = "/", Hostnames = { "rabbitmq" } }; var busClient = BusClientFactory.CreateDefault(busConfig); services.AddSingleton <IBusClient>(busClient); }); /* services.AddSingleton<EventBus.RabbitMQ.Interfaces.IRabbitMQPersistentConnection>(sp => * { * var logger = sp.GetRequiredService<ILogger<RabbitMQPersistentConnection>>(); * * var factory = new ConnectionFactory() * { * HostName = "testwebapi-rabbitmq" * }; * * var retryCount = 5; * * return new RabbitMQPersistentConnection(factory, logger, retryCount); * }); * * services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => * { * var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); * var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); * var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); * var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); * var retryCount = 5; * * return new EventBusRabbitMQ(rabbitMQPersistentConnection, iLifetimeScope, eventBusSubcriptionsManager, logger, "TestWebApi", retryCount); * }); * * services.AddSingleton<IEventBusSubscriptionsManager, EventBusSubscriptionsManager>();*/ //services.AddTransient<ProductPriceChangedIntegrationEventHandler>(); }
public MessageSequenceBuilder(IBusClient <TMessageContext> busClient, IMessageChainTopologyUtil chainTopology, IMessageChainDispatcher dispatcher, IMessageSequenceRepository repository, RawRabbitConfiguration mainCfg) { _busClient = busClient; _chainTopology = chainTopology; _dispatcher = dispatcher; _repository = repository; _mainCfg = mainCfg; }
private void AddRabbitMqService(IServiceCollection services, IConfigurationSection configuration) { var options = new RawRabbitConfiguration(); configuration.Bind(options); services.AddSingleton <IBusClient>(_ => BusClientFactory.CreateDefault(options)); //services.AddTransient<ICommandHandler<CreateEvent>, CreateEventHandler>(); }
public MessageSequence(IBusClient client, INamingConventions naming, RawRabbitConfiguration clientCfg, SequenceModel model = null) : base(model) { _client = client; _naming = naming; _clientCfg = clientCfg; _triggerConfigurer = new TriggerConfigurer(); _stepDefinitions = new Queue <StepDefinition>(); _subscriptions = new List <Subscription.ISubscription>(); }
public static void AddRabbitMqConnection(this IServiceCollection services, IConfigurationSection section) { var options = new RawRabbitConfiguration(); section.Bind(options); var client = BusClientFactory.CreateDefault(options); services.AddSingleton <IBusClient>(_ => client); }
public BindingProvider(RawRabbitConfiguration config) { _config = config; var vHost = string.Equals(config.VirtualHost, DefaultVhost) ? UriEncodedDefaultVhost : config.VirtualHost; _baseUrl = $"http://{config.Hostnames.FirstOrDefault()}:15672/api/exchanges/{vHost}"; }
public static void ConfigureRawRabbit(IServiceCollection services, RawRabbitConfiguration configuration) { var options = new RawRabbitOptions { ClientConfiguration = configuration, Plugins = p => p.UseAttributeRouting() }; services.AddRawRabbit(options); }
public Publisher(IChannelFactory channelFactory, ITopologyProvider topologyProvider, IMessageSerializer serializer, IMessageContextProvider <TMessageContext> contextProvider, IPublishAcknowledger acknowledger, IBasicPropertiesProvider propertiesProvider, RawRabbitConfiguration config) { _channelFactory = channelFactory; _topologyProvider = topologyProvider; _serializer = serializer; _contextProvider = contextProvider; _acknowledger = acknowledger; _propertiesProvider = propertiesProvider; _config = config; }
private void AddRabbitMqService(IServiceCollection services, IConfigurationSection configuration) { var options = new RawRabbitConfiguration(); configuration.Bind(options); services.AddSingleton <IBusClient>(_ => BusClientFactory.CreateDefault(options)); services.AddTransient <IEventHandler <NotificationSent>, NotificationSentHandler>(); services.AddTransient <IQueryHandler <GetNotifications, Notifications>, GetNotificationsHandler>(); services.AddTransient <ICommandHandler <SendNotification>, SendNotificationHandler>(); }
public static void AddRabbitMq(this ContainerBuilder builder) { //Register the options from appconfig into the DI container builder.Register(context => { var config = context.Resolve <IConfiguration>(); var rabbitMqOptions = new RabbitMqOptions(); config.GetSection("rabbitMq").Bind(rabbitMqOptions); return(rabbitMqOptions); }).SingleInstance(); builder.Register(context => { var config = context.Resolve <IConfiguration>(); var rawRabbitOptions = new RawRabbitConfiguration(); config.GetSection("rabbitMq").Bind(rawRabbitOptions); return(rawRabbitOptions); }).SingleInstance(); //Note:Obtain the service assembly var assembly = Assembly.GetCallingAssembly(); builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(ICommandHandler <>)).InstancePerDependency(); //builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(IEventHandler<>)).InstancePerDependency(); // builder.RegisterType<MessageHandler>().As<IMessageHandler>().InstancePerDependency(); builder.RegisterType <BusPublisher>().As <IBusPublisher>().InstancePerDependency();; // builder.RegisterInstance(ParkAndRideTracer).As<ITracer>().SingleInstance().PreserveExistingDefaults(); builder.Register <IIntanceFactory>(context => { var options = context.Resolve <RabbitMqOptions>(); var configuration = context.Resolve <RawRabbitConfiguration>(); // var tracer = context.Resolve<ITracer>(); return(RawRabbitFactory <InstanceFactory> .CreateInstanceFactory(new RawRabbitOptions { DependencyInjection = ioc => { ioc.AddSingleton(options); ioc.AddSingleton(configuration); // ioc.AddSingleton(tracer); }, Plugins = p => p .UseAttributeRouting() .UseRetryLater() .UpdateRetryInfo() .UseMessageContext <CorrelationContext>() .UseContextForwarding() //.UseJaeger(tracer) })); }).SingleInstance(); //Create the instance factory and instantiate it right away??? builder.Register(context => context.Resolve <IInstanceFactory>().Create()); }
public ChannelFactory(IConnectionFactory connectionFactory, RawRabbitConfiguration config, ChannelFactoryConfiguration channelConfig) { _connectionFactory = connectionFactory; _config = config; _channelConfig = channelConfig; _requestQueue = new ConcurrentQueue <TaskCompletionSource <IModel> >(); _channels = new LinkedList <IModel>(); ConnectToBroker(); Initialize(); }
public static RawRabbitConfiguration Parse(string connectionString) { var mainMatch = MainRegex.Match(connectionString); var port = Defaults.Port; if (RegexMatchGroupIsNonEmpty(mainMatch, "port")) { var suppliedPort = mainMatch.Groups["port"].Value; if (!int.TryParse(suppliedPort, out port)) { throw new FormatException($"The supplied port '{suppliedPort}' in the connection string is not a number"); } } var cfg = new RawRabbitConfiguration { Username = RegexMatchGroupIsNonEmpty(mainMatch, "username") ? mainMatch.Groups["username"].Value : Defaults.Username, Password = RegexMatchGroupIsNonEmpty(mainMatch, "password") ? mainMatch.Groups["password"].Value : Defaults.Password, Hostnames = mainMatch.Groups["hosts"].Value.Split(',').ToList(), Port = port, VirtualHost = ExctractVirutalHost(mainMatch) }; var parametersMatches = ParametersRegex.Matches(mainMatch.Groups["parameters"].Value); foreach (Match match in parametersMatches) { var name = match.Groups["name"].Value.ToLower(); var val = match.Groups["value"].Value.ToLower(); var propertyInfo = cfg .GetType() .GetTypeInfo() .GetProperty(name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (propertyInfo == null) { throw new ArgumentException($"No configuration property named '{name}'"); } if (propertyInfo.PropertyType == typeof(TimeSpan)) { var convertedValue = TimeSpan.FromSeconds(int.Parse(val)); propertyInfo.SetValue(cfg, convertedValue, null); } else { propertyInfo.SetValue(cfg, Convert.ChangeType(val, propertyInfo.PropertyType), null); } } return(cfg); }
public static void AddRabbitMq(this IServiceCollection services, IConfiguration config) { var options = new RawRabbitConfiguration(); var section = config.GetSection("rabbitmq"); section.Bind(options); var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions { ClientConfiguration = options }); services.AddSingleton <IBusClient>(_ => client); }
/// <summary> /// Method to add a custom raw rabbit implementation with options from my appsettings. /// </summary> /// <param name="services"> Add as a singleton service.</param> /// <param name="configuration"> Get the RabbitMq section.</param> /// <returns> IServiceCollection to follow with the pipeline.</returns> public static IServiceCollection AddRabbitMq(this IServiceCollection services, IConfiguration configuration) { var rabbitSection = configuration.GetSection("RabbitMq"); var rabbitOptions = new RawRabbitConfiguration(); rabbitSection.Bind(rabbitOptions); var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions { ClientConfiguration = rabbitOptions }); return(services.AddSingleton <IBusClient>(client)); }
public ResourceDisposer( IChannelFactory channelFactory, IConnectionFactory connectionFactory, ISubscriptionRepository subscriptionRepo, IChannelPoolFactory channelPoolFactory, RawRabbitConfiguration config) { _channelFactory = channelFactory; _connectionFactory = connectionFactory; _subscriptionRepo = subscriptionRepo; _channelPoolFactory = channelPoolFactory; _config = config; }
public override void InitialiseBus() { var busConfig = new RawRabbitConfiguration { Username = "******", Password = "******", Port = 5672, VirtualHost = "/", PersistentDeliveryMode = true, Hostnames = { ConnectionString }, }; _busClient = BusClientFactory.CreateDefault(busConfig); }
public SpelRepository(DatabaseContext context, RawRabbitConfiguration rabbitconfig = null) : base(context) { _RabbitConfig = rabbitconfig ?? new RawRabbitConfiguration() { Username = "******", Password = "******", Port = 15672, Hostnames = { "localhost" }, Exchange = new GeneralExchangeConfiguration() { Type = RawRabbit.Configuration.Exchange.ExchangeType.Topic } }; }