static void Test() { var topic = "testtopic"; var cnn = RabbitMQBuilder.Get(MQConfig.Default).GetConnection(); var operation = cnn.GetOperation(topic); Console.WriteLine("正连接到订阅【" + topic + "】"); operation.Subscribe(); Console.WriteLine("正在入队"); Task.Factory.StartNew(() => { while (true) { operation.Enqueue(Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " hello!")); Thread.Sleep(1); } }); Console.WriteLine("正在出队"); Task.Factory.StartNew(() => { while (true) { var result = operation.Dnqueue(); if (result == null) { Thread.Sleep(1); } else { Console.WriteLine(Encoding.UTF8.GetString(result)); } } }); Console.ReadLine(); Console.WriteLine("正在取消订阅"); operation.UnSubscribe(); Console.WriteLine("测试完成"); }
public static IServiceCollection AddEasyRabbit(this IServiceCollection services, Action <RabbitMQBuilder> builderAction) { var builder = new RabbitMQBuilder(); builderAction(builder); services.AddLogging(); services.AddSingleton <IMessagePublisher, MessagePublisher>(); services.AddSingleton <RabbitMQBuilder>(builder); services.AddSingleton <ILoggerFactory, MicrosoftLoggerFactory>(); services.AddSingleton <IObjectContainer, ServiceProviderObjectContainer>(); builder.UseNewtonSoftJson(); foreach (var consumeHandler in builder.ConsumeBuilders) { services.AddScoped(consumeHandler.HandlerType); } return(services); }