コード例 #1
0
        public async Task PublishAsync_Should_Sent_To_Service_Exchange()
        {
            try
            {
                var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom);
                networkInfos.ServiceExchangeDescriptions.Add(new RabbitExchangeDescription("pub1_exchange"));
                var config = new RabbitPublisherConfiguration
                {
                    ConnectionInfos = GetConnectionInfos(),
                    NetworkInfos    = networkInfos
                };
                RabbitCommonTools.DeclareExchangesAndQueueForPublisher(channel, config);
                channel.QueueDeclare("CQELight");
                channel.QueueBind("CQELight", "pub1_exchange", "");

                var publisher = new RabbitPublisher(
                    loggerFactory,
                    config);

                await publisher.PublishEventAsync(new TestEvent());

                var result = channel.BasicGet("CQELight", true);
                result.Should().NotBeNull();
                Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull();
            }
            finally
            {
                DeleteData();
            }
        }
コード例 #2
0
ファイル: Global.asax.cs プロジェクト: bmegias/RabbitMQSample
 static RabbitUtils()
 {
     var serviceName = typeof(RabbitUtils).FullName;
     var cfg = new[] { new HandlerConfig<HelloResponse>(null) };
     _subscriber = new RabbitSubscriber(RabbitCfg.XCHG, serviceName, RabbitCfg.HOST, cfg);
     _publisher = new RabbitPublisher(RabbitCfg.XCHG, RabbitCfg.HOST, _subscriber);
 }
コード例 #3
0
        //[TestMethod]
        public void IntTest()
        {
            Environment.SetEnvironmentVariable("RABBIT_PORT", "5671");

            Rabbit subject = Rabbit.Connect();

            var consumer = subject.ConsumerBuilder()
                           .SetExchange(exchange)
                           .AddQueue(queue, expectedRoutingKey)
                           .Build();

            consumer.RegisterReceiver(receiver);

            var             msg       = Encoding.UTF8.GetBytes(expectedMsg);
            RabbitPublisher publisher = subject.PublishBuilder().
                                        SetExchange(exchange)
                                        .AddQueue(queue, expectedRoutingKey)
                                        .Build();



            string routingKey = "";

            publisher.Publish(msg, routingKey);

            Thread.Sleep(sleepTimeMs);

            Assert.AreEqual(expectedMsg, actual);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: bmegias/RabbitMQSample
        static void Main(string[] args)
        {
            var names = new[]
            {
                "Brau", "Raul", "Oriol", "Xavi", "Rocio", "Juanjo"
            };

            Console.WriteLine("***** RPC Client *****");
            var tot = 0;
            var serviceName = typeof(Program).Namespace.ToLowerInvariant();

            var cfg = new[] { new HandlerConfig<HelloResponse>(null) };
            var sub = new RabbitSubscriber(RabbitCfg.XCHG, serviceName, RabbitCfg.HOST, cfg);
            var pub = new RabbitPublisher(RabbitCfg.XCHG, RabbitCfg.HOST, sub);

            sub.StartAsyncMessageLoop();

            names
                .AsParallel()
                .ForAll(name =>
            {
                var corId = Guid.NewGuid().ToString();

                pub.PublishRequest<HelloRequest, HelloResponse>(
                    corId
                    , new HelloRequest() { Name = name }
                    , response =>
                    {
                        Console.WriteLine("Received CorrelationId: {0} Message: {1}"
                                               , corId
                                               , MessageBase.ToJson(response));
                    });
            });
        }
コード例 #5
0
        private void SendAnswer(string message)
        {
            var connection = RabbitConnector.GetConnection();
            var settings   = new QueryExecutorRabbitSettings();

            RabbitPublisher.Publish(connection, settings.ExchangeName, settings.AnswerQueueName, settings.AnswerRoutingKey, message);
        }
コード例 #6
0
        public async Task DispatchAsync_Should_Sent_To_FirstNamespacePart_By_Convention()
        {
            try
            {
                var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom);
                networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("CQELight"));
                var config = new RabbitPublisherConfiguration
                {
                    ConnectionInfos = GetConnectionInfos(),
                    NetworkInfos    = networkInfos
                };
                var publisher = new RabbitPublisher(
                    loggerFactory,
                    config);

                await publisher.DispatchAsync(new TestCommand());

                var result = channel.BasicGet("CQELight", true);
                result.Should().NotBeNull();
                Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull();
            }
            finally
            {
                DeleteData();
            }
        }
コード例 #7
0
 public ConversionService(FileServiceContext context, IStorage storage, RabbitPublisher rabbitPublisher, IOptions <FileServiceSettings> settings)
 {
     _context         = context;
     _storage         = storage;
     _rabbitPublisher = rabbitPublisher;
     _settings        = settings;
 }
コード例 #8
0
        public void Publish()
        {
            IPublisher <TestMessage> pubs = new RabbitPublisher <TestMessage>(Options);

            pubs.Init();

            var message = GetTestMessage();

            pubs.Publish(message);
        }
コード例 #9
0
        public void Publish_DoesWait_WhenNotConfirm()
        {
            this.subject = new RabbitPublisher(mockedCreator.Object, exchange, basicProperties.Object, false);

            byte[] body       = Encoding.UTF8.GetBytes("hello");
            string routingKey = "";

            subject.Publish(body, routingKey);

            mockedChannel.Verify(c => c.WaitForConfirms(It.IsAny <TimeSpan>()), Times.Never);
        }
コード例 #10
0
 public void InitializeRabbitPublisherTest()
 {
     mockedCreator    = new Mock <IRabbitConnectionCreator>();
     basicProperties  = new Mock <IBasicProperties>();
     mockedChannel    = new Mock <IModel>();
     mockedConnection = new Mock <IConnection>();
     mockedConnection.Setup(c => c.CreateModel()).Returns(mockedChannel.Object);
     mockedCreator.Setup(c => c.GetConnection()).Returns(mockedConnection.Object);
     mockedCreator.Setup(c => c.GetChannel()).Returns(mockedChannel.Object);
     subject = new RabbitPublisher(mockedCreator.Object, exchange, basicProperties.Object, confirmPublish);
 }
コード例 #11
0
        public async Task PublishAsync_RoutingKey_Topic_Should_BeConsidered()
        {
            try
            {
                var fakeRoutingKeyFactory = new Mock <IRoutingKeyFactory>();
                fakeRoutingKeyFactory
                .Setup(m => m.GetRoutingKeyForEvent(It.IsAny <object>()))
                .Returns("cqelight.events.testevent");

                var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom);
                networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("CQELight"));
                networkInfos.ServiceExchangeDescriptions.Add(new RabbitExchangeDescription("MyCustomExchange")
                {
                    ExchangeType = ExchangeType.Topic
                });
                var queue = new RabbitQueueDescription("MyCustomQueue");
                queue.Bindings.Add(new RabbitQueueBindingDescription("MyCustomExchange")
                {
                    RoutingKeys = new List <string> {
                        "*.events.*"
                    }
                });
                networkInfos.ServiceQueueDescriptions.Add(queue);
                var config = new RabbitPublisherConfiguration
                {
                    ConnectionInfos   = GetConnectionInfos(),
                    NetworkInfos      = networkInfos,
                    RoutingKeyFactory = fakeRoutingKeyFactory.Object
                };
                var publisher = new RabbitPublisher(
                    loggerFactory,
                    config);

                await publisher.PublishEventAsync(new TestEvent());

                channel.BasicGet("CQELight", true).Should().BeNull();

                var result = channel.BasicGet("MyCustomQueue", true);
                result.Should().NotBeNull();
                Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestEvent>().Should().NotBeNull();
            }
            finally
            {
                DeleteData();
                channel.QueueDelete("MyCustomQueue");
                channel.ExchangeDelete("MyCustomExchange");
            }
        }
コード例 #12
0
        public void Publish()
        {
            var consumer = subject.ConsumerBuilder()
                           .SetExchange(topic)
                           .AddQueue(queue, routingKey)
                           .Build();

            var             msg       = Encoding.UTF8.GetBytes(expected);
            RabbitPublisher publisher = subject.PublishBuilder().
                                        SetExchange(topic)
                                        .AddQueue(queue, routingKey)
                                        .Build();


            publisher.Publish(msg, routingKey);
        }
コード例 #13
0
        static void Main(string[] args)
        {
            IRabbitPublisher _publisher = new RabbitPublisher();

            _publisher.PublishQueen(new SampleDataModel
            {
                Id        = 1,
                Age       = 30,
                Name      = "Hanifi",
                Surname   = "Tayfur",
                QueenGuid = Guid.NewGuid()
            });

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }
コード例 #14
0
        public static void PublishMessageTest()
        {
            IRabbitPublisherConfiguration publishConfig = new RabbitPublisherConfiguration();
            RabbitPublisher <CommandArgs> publisher     = new RabbitPublisher <CommandArgs>(publishConfig);

            IRabbitSubscriberConfiguration subscriberConfig = new RabbitSubscriberConfiguration();
            SandboxConsumer consumer = new SandboxConsumer();

            RabbitSubscriber <SandboxConsumer> subscriber = new RabbitSubscriber <SandboxConsumer>(consumer, subscriberConfig);

            subscriber.Start();

            publisher.Publish(new CommandArgs {
                Value = "some data"
            });



            Console.ReadKey();
        }
コード例 #15
0
ファイル: sp_PostRabbitMsg.cs プロジェクト: HP-dufeng/WIKI
    public static void sp_PostRabbitMsg(string endpointName, string msg)
    {
        //step 0: read rabbit config
        var endpoint = Database.GetEndpointConfig(endpointName);

        if (endpoint == null)
        {
            SqlContext.Pipe.Send(string.Format("Endpoint: {0}, Not Found", endpointName));
            return;
        }

        if (!endpoint.IsEnabled)
        {
            SqlContext.Pipe.Send(string.Format("Endpoint: {0}, Disabled", endpointName));
            return;
        }

        //step 1: send msg to rabbit queue
        int i = 3;

        do
        {
            try
            {
                RabbitPublisher.SendMsg(endpoint, msg);
                break;
            }
            catch (Exception ex)
            {
                i--;

                if (i <= 0)
                {
                    throw ex;
                }
            }
        } while (i > 0);
    }
コード例 #16
0
        private void Connect()
        {
            rabbit = Rabbit.Connect();

            var builder = rabbit.PublishBuilder().
                          SetExchange(exchange)
                          .SetConfirmPublish()
                          .SetExchangeType(type)
                          .SetContentType(contentType);

            var queue = config.GetProperty("QUEUE", "");

            if (queue.Length > 0)
            {
                builder.UseQueueType(Enum.Parse <RabbitQueueType>(config.GetProperty("QUEUE_TYPE")));
                builder.AddQueue(queue, config.GetProperty("ROUTING_KEY", ""));
            }

            List <Thread> list = new List <Thread>(this.producerCount);


            publisher = builder.Build();
        }
コード例 #17
0
        public async Task DispatchAsync_Default_Routing_Key_Factory_Should_Be_Considered()
        {
            try
            {
                var fakeRoutingKeyFactory = new Mock <IRoutingKeyFactory>();
                fakeRoutingKeyFactory
                .Setup(m => m.GetRoutingKeyForCommand(It.IsAny <object>()))
                .Returns("MyCustomQueue");

                var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom);
                networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("CQELight"));
                networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("MyCustomQueue"));
                var config = new RabbitPublisherConfiguration
                {
                    ConnectionInfos   = GetConnectionInfos(),
                    NetworkInfos      = networkInfos,
                    RoutingKeyFactory = fakeRoutingKeyFactory.Object
                };
                var publisher = new RabbitPublisher(
                    loggerFactory,
                    config);

                await publisher.DispatchAsync(new TestCommand());

                channel.BasicGet("CQELight", true).Should().BeNull();

                var result = channel.BasicGet("MyCustomQueue", true);
                result.Should().NotBeNull();
                Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull();
            }
            finally
            {
                DeleteData();
                channel.QueueDelete("MyCustomQueue");
            }
        }
コード例 #18
0
 public FileService(FileServiceContext context, IStorage storage, RabbitPublisher rabbitPublisher)
 {
     _context         = context;
     _storage         = storage;
     _rabbitPublisher = rabbitPublisher;
 }