コード例 #1
0
        public void AmqpWebSocketTransportTest()
        {
            string address = "ws://localhost:28088";
            var    broker  = new TestAmqpBroker(new string[] { address }, null, null, null);

            try
            {
                broker.Start();

                string queue = "AmqpWebSocketTransportTest";
                broker.AddQueue(queue);

                AmqpConnection connection = AmqpConnection.Factory.OpenConnectionAsync(address).GetAwaiter().GetResult();

                AmqpSession session = connection.CreateSession(new AmqpSessionSettings());
                session.Open();

                SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend));
                sLink.Open();

                int messageCount = 100;
                for (int i = 0; i < messageCount; i++)
                {
                    AmqpMessage message = AmqpMessage.Create(new AmqpValue()
                    {
                        Value = "message" + i
                    });
                    sLink.SendMessageAsync(message, AmqpConstants.EmptyBinary, AmqpConstants.NullBinary, TimeSpan.FromSeconds(10)).Wait();
                }

                sLink.Close();

                ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnReceive, 100));
                rLink.Open();

                for (int i = 0; i < messageCount; i++)
                {
                    AmqpMessage message2 = rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(60)).GetAwaiter().GetResult();
                    Assert.NotNull(message2);

                    rLink.AcceptMessage(message2, false);
                    message2.Dispose();
                }

                rLink.Close();

                connection.Close();
            }
            finally
            {
                broker.Stop();
            }
        }
コード例 #2
0
        public void AmqpWebSocketTransportTest()
        {
            string queue = "AmqpWebSocketTransportTest";

            broker.AddQueue(queue);

            AmqpConnection connection = AmqpConnection.Factory.OpenConnectionAsync(
                TestAmqpBrokerFixture.WsAddress.OriginalString).GetAwaiter().GetResult();

            AmqpSession session = connection.CreateSession(new AmqpSessionSettings());

            session.Open();

            SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend));

            sLink.Open();

            int messageCount = 1800;

            for (int i = 0; i < messageCount; i++)
            {
                AmqpMessage message = AmqpMessage.Create(new AmqpValue()
                {
                    Value = "message" + i
                });
                sLink.SendMessageAsync(message, EmptyBinary, NullBinary, TimeSpan.FromSeconds(10)).Wait();
            }

            sLink.Close();

            ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnReceive, 100));

            rLink.Open();

            for (int i = 0; i < messageCount; i++)
            {
                AmqpMessage message2 = rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(60)).GetAwaiter().GetResult();
                Assert.NotNull(message2);

                rLink.AcceptMessage(message2);
                message2.Dispose();
            }

            rLink.Close();

            connection.Close();
        }
コード例 #3
0
ファイル: Sender.cs プロジェクト: Bigsby/PoC
        static void Main(string[] args)
        {
            try
            {
                var connection = AmqpConnection.Factory.OpenConnectionAsync("amqp://localhost:5672").Result;

                var sessionSettings = new AmqpSessionSettings
                {
                };
                //AmqpSessionSettings.Create(new Begin())
                var session = connection.CreateSession(sessionSettings);

                var linkSettings = new AmqpLinkSettings
                {
                    LinkName = "theExchange",
                    Handle   = 1234,

                    Role   = false,
                    Target = new Target
                    {
                        Address = "theQueue",
                        Durable = 1
                    }
                };

                var sender = new SendingAmqpLink(session, linkSettings);
                session.Open();
                sender.Open();
                var key = ConsoleKey.A;
                var i   = 0;
                do
                {
                    Console.WriteLine("Press any key to send message");

                    var body    = "Hello, AMQP1.0!";
                    var message = AmqpMessage.Create(new AmqpValue()
                    {
                        Value = body
                    });
                    var tag     = new ArraySegment <byte>(BitConverter.GetBytes(i++));
                    var outcome = sender.SendMessageAsync(message, tag, AmqpConstants.NullBinary, TimeSpan.FromSeconds(3)).Result;

                    if (outcome.DescriptorCode == Accepted.Code)
                    {
                        Console.WriteLine(" [x] Sent {0}", body);
                    }
                    else
                    {
                        Console.WriteLine("Error sending message!");
                    }
                } while (key != ConsoleKey.Q);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                throw;
            }

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }