static void Main(string[] args) { var connectionFactory = new ConnectionFactory { HostName = "localhost" }; var eventBus = new RabbitMQEventBus(connectionFactory, "EventBus.RabbitMQ.Events.Exchange", queueName: "EventBus.RabbitMQ.Events.Queue"); var customerASendedEventHandler = new CustomerASendedEventHandler(); eventBus.Subscribe <SendedEvent>(customerASendedEventHandler); var customerBSendedEventHandler = new CustomerBSendedEventHandler(); eventBus.Subscribe <SendedEvent>(customerBSendedEventHandler); Console.ReadKey(); }
public static IFuxionBuilder RabbitMQ(this IFuxionBuilder me, out Func <IServiceProvider, RabbitMQEventBus> builder, string exchangeName, string queueName, string connectionHost = "localhost", int connectionRetryCount = 5, int queueRetryCount = 5) { me.Services.AddSingleton <IRabbitMQPersistentConnection>(new DefaultRabbitMQPersistentConnection(new ConnectionFactory() { HostName = connectionHost }, connectionRetryCount)); builder = new Func <IServiceProvider, RabbitMQEventBus>(sp => { var bus = new RabbitMQEventBus( sp, sp.GetRequiredService <IRabbitMQPersistentConnection>(), sp.GetRequiredService <TypeKeyDirectory>(), exchangeName, queueName, queueRetryCount); // Esto se ejecutará al activar la instancia de RabbitMQEventBus // Suscribo los eventos externos. Se manejarán mediante EventHandlers igual que los eventos del propio bounded context foreach (var sub in sp.GetServices <EventSubscription>()) { bus.Subscribe(sub.EventType); } return(bus); }); me.Services.AddSingleton(builder); me.AddToAutoActivateList <RabbitMQEventBus>(); return(me); }
public void HandlerSubscribeTest() { IMetricsClient client = NSubstitute.Substitute.For <IMetricsClient>(); IEventBus bus = new RabbitMQEventBus(new DATConfiguration(), client); MessageTest test = new MessageTest { Name = "Maikel" }; bus.Publish("exchange.subscribe-test-handler", test); bus.Publish("exchange.subscribe-test-handler", test); bus.Publish("exchange.subscribe-test-handler", test); int index = 0; bus.Subscribe <MessageTest>("exchange.subscribe-test-handler", messageTest => { Assert.Equal("Maikel", messageTest.Name); index++; return(true); }); Thread.Sleep(500); Assert.Equal(3, index); }
static void Main(string[] args) { var connectionFactory = new ConnectionFactory { HostName = "localhost" }; var eventBus = new RabbitMQEventBus(connectionFactory, "EventBus.RabbitMQ.ConsoleApp01.Exchange", queueName: "EventBus.RabbitMQ.ConsoleApp01.Queue"); var sendedEvent = new SendedEvent("优惠"); var customerASendedEventHandler = new CustomerASendedEventHandler(); eventBus.Subscribe <SendedEvent>(customerASendedEventHandler); var customerBSendedEventHandler = new CustomerBSendedEventHandler(); eventBus.Subscribe <SendedEvent>(customerBSendedEventHandler); Console.WriteLine($"商店发了{sendedEvent.Name}通知!"); eventBus.Publish <SendedEvent>(sendedEvent); Console.ReadKey(); }
public void ObservableSubscribeTest() { IMetricsClient client = NSubstitute.Substitute.For <IMetricsClient>(); IEventBus bus = new RabbitMQEventBus(new DATConfiguration(), client); MessageTest test = new MessageTest { Name = "Maikel" }; bus.Publish("exchange.subscribe-test", test); bus.Publish("exchange.subscribe-test", test); bus.Publish("exchange.subscribe-test", test); IObservable <MessageTest> subscription = bus.Subscribe <MessageTest>("exchange.subscribe-test"); subscription.Do(messageTest => Assert.Equal("Maikel", messageTest.Name)).Subscribe(); }
public EventHandler(RabbitMQEventBus eventBus, DBProxy dbProxy) { this.eventBus = eventBus; eventBus.Subscribe(this); this.dbContext = dbProxy?.DbContext; }
/// <summary> /// 测试ActvieMQ 消息发布与订阅 /// </summary> /// <param name="isServer"></param> private static void TestEventBus(MQTypeEnum mqType, Boolean isServer = true) { Console.WriteLine("消息测试:1,消息发布;2,消息订阅"); isServer = Console.ReadKey().KeyChar == '1'; var topic1 = "Topic.Test"; var topic2 = "Topic.Admin.User"; IEventBus eventbus; switch (mqType) { case MQTypeEnum.ActiveMQ: eventbus = new ActiveMQEventBus(); break; case MQTypeEnum.RabbitMQ: eventbus = new RabbitMQEventBus(); break; default: eventbus = new RabbitMQEventBus(); break; } Int64 userid = UserX.FindCount() + 1; TimerX timer; if (isServer) { timer = new TimerX((Object state) => { try { var time = DateTime.Now; eventbus.Publish(topic1, new EventMessage() { Action = "Test", Body = "学无先后达者为师" }); Console.WriteLine($"{time} 发送了一条消息,主题为:{topic1}"); // 实体消息测试 long tick = DateTime.Now.Ticks; var rd = new Random((Int32)(tick & 0xFFFFFFFFL) | (Int32)(tick >> 32)); var user = new UserX() { Name = "User" + rd.Next(), DisplayName = $"{Enum.GetName(typeof(MQTypeEnum), eventbus.MQType)}_{userid++}", Code = userid.ToString().PadLeft(7, '0') }; //user.SaveAsync(); // 向数据库中插入数据 eventbus.PublishAsync(topic2, new EventMessage() { Action = "Insert", Body = user.ToJson() }); } catch (Exception ex) { XTrace.WriteException(ex); throw ex; } }, null, 10, 10); } else { var handler = new EventBusHandler(); eventbus.Subscribe(topic1, handler); eventbus.SubscribeAsync(topic2, new MyHandler()); } }