static void Main(string[] args) { //configuration var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.dev.json").Build(); var services = new ServiceCollection(); services.AddSingleton(configuration); //logging services.AddLogging(cfg => cfg.AddConsole()); //rabbitmq services.AddRabbitMq(configuration.GetSection("rabbitmq")); var serviceProvider = services.BuildServiceProvider(); using (var scope = serviceProvider.CreateScope()) { var exchange = new ExchangeDefinition { ExchangeName = "logs", Type = ExchangeType.Topic }; var queue = new QueueDefinition { QueueName = "color.warning", AutoDelete = false, Exclusive = false, Durable = false }; var publisher = scope.ServiceProvider.GetRequiredService <IRabbitMqPublisher>(); //publisher publisher.Initialize(queue: queue, exchange: exchange, routingKey: "#.warning"); publisher.Publish(new UserRegisterEventData { UserName = "******", RegisterTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }); var consumer = scope.ServiceProvider.GetRequiredService <IRabbitMqConsumer>(); consumer.Initialize(queue); var disposableHandle = consumer.Subscribe <UserRegisterEventData>(usr => { Console.Write($"{usr.UserName}\t{DateTimeOffset.FromUnixTimeMilliseconds(usr.RegisterTime).ToString()}"); Console.WriteLine(); return(Task.CompletedTask); }); Console.WriteLine("Press any key to exit.."); Console.ReadLine(); //unsubscribe disposableHandle.Dispose(); Console.WriteLine("exit.."); } }
public InMemoryConnectorTests() { _reset = new AutoResetEvent(false); _connector = new InMemoryConnector(); _exchangeDefinition = new ExchangeDefinition("Exchange" + Guid.NewGuid().ToString(), ExchangeType.Direct) { AutoDelete = true }; _queueDefinition = new QueueDefinition { Name = "Queue" + Guid.NewGuid().ToString(), AutoDelete = true, RoutingKeys = new[] { "some.key" } }; }
public async Task <Result> Create(Action <ExchangeCreateAction> action, CancellationToken cancellationToken = new CancellationToken()) { cancellationToken.RequestCanceled(); var impl = new ExchangeCreateActionImpl(); action(impl); impl.Validate(); ExchangeDefinition definition = impl.Definition.Value; Debug.Assert(definition != null); string url = $"api/exchanges/{impl.VirtualHost.Value.ToSanitizedName()}/{impl.ExchangeName.Value}"; if (impl.Errors.Value.Any()) { return(new FaultedResult(impl.Errors.Value, new DebugInfoImpl(url, definition.ToJsonString(Deserializer.Options)))); } return(await Put(url, definition, cancellationToken).ConfigureAwait(false)); }
public void Verify_can_create_exchange() { var container = GetContainerBuilder().BuildServiceProvider(); var result = container.GetService <IBrokerObjectFactory>() .Object <Exchange>() .Create(x => { x.Exchange("fake_exchange"); x.Configure(c => { c.IsDurable(); c.IsForInternalUse(); c.HasRoutingType(ExchangeRoutingType.Fanout); c.HasArguments(arg => { arg.Set("fake_arg", "8238b"); }); }); x.Targeting(t => t.VirtualHost("HareDu")); }) .GetResult(); Console.WriteLine(result.ToJsonString(Deserializer.Options)); result.HasFaulted.ShouldBeFalse(); result.DebugInfo.ShouldNotBeNull(); ExchangeDefinition definition = result.DebugInfo.Request.ToObject <ExchangeDefinition>(Deserializer.Options); result.DebugInfo.URL.ShouldBe("api/exchanges/HareDu/fake_exchange"); definition.RoutingType.ShouldBe(ExchangeRoutingType.Fanout); definition.Durable.ShouldBeTrue(); definition.Internal.ShouldBeTrue(); definition.AutoDelete.ShouldBeFalse(); definition.Arguments.Count.ShouldBe(1); definition.Arguments["fake_arg"].ToString().ShouldBe("8238b"); }
protected Action ExchangeResponder(string exchangeType = ExchangeType.Direct, string routingKey = "") { var exchange = new ExchangeDefinition(ExchangeName, exchangeType) { AutoDelete = true }; return _connector.ListenTo<int>(exchange, routingKey, new LambdaMessageHandler<int>((props, message) => { var result = message.ToString().Length; _connector.SendTo( new QueueDefinition { Name = props.ReplyTo }, p => { p.CorrelationId = props.CorrelationId; }, result); return true; })); }
public void When_querying_an_exchange_with_a_routngkey() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Topic) { AutoDelete = true, }; var unsubscribe = ExchangeResponder(ExchangeType.Topic, "some.*"); int recieved = 0; var message = 1234; _connector .Query<int>(exchange, "some.key", props => { }, message) .ContinueWith(response => { recieved = response.Result.Message; _reset.Set(); }) .Wait(TimeSpan.FromSeconds(5)); unsubscribe(); recieved.ShouldBe(4); }
public void When_querying_an_exchange() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Direct) { AutoDelete = true }; var unsubscribe = ExchangeResponder(); int recieved = 0; var message = 1234; _connector .Query<int>(exchange, props => { }, message) .ContinueWith(response => { recieved = response.Result.Message; _reset.Set(); }) .Wait(); unsubscribe(); recieved.ShouldBe(4); }
public void When_sending_to_an_exchange_with_a_routing_key() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Direct) { AutoDelete = true }; var queue = new QueueDefinition { Name = QueueName, AutoDelete = true, RoutingKeys = new[] { "some.key" } }; int recieved = 0; var unsubscribe = _connector.ListenTo<int>( exchange, queue, new LambdaMessageHandler<int>((props, json) => { recieved = json; _reset.Set(); return true; })); _connector.SendTo(exchange, "some.key", props => { }, 123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }
public void When_sending_to_an_exchange() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Direct) { AutoDelete = true }; int recieved = 0; var unsubscribe = _connector.ListenTo<int>( exchange, new LambdaMessageHandler<int>((props, json) => { recieved = json; _reset.Set(); return true; })); _connector.SendTo(exchange, props => { }, 123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }
public void When_listening_to_an_exchange_with_a_custom_queue() { var exchange = new ExchangeDefinition(ExchangeName, ExchangeType.Direct) { AutoDelete = true }; var queue = new QueueDefinition { Name = QueueName, AutoDelete = true, }; int recieved = 0; var unsubscribe = _connector.ListenTo<int>( exchange, queue, new LambdaMessageHandler<int>((props, json) => { recieved = json; _reset.Set(); return true; })); SendToExchange(123); _reset.WaitOne(TimeSpan.FromSeconds(5)); recieved.ShouldBe(123); }