public void TestDirect()
        {
            using (IConnector conn = Factory.CreateConnector(_builder)) {
                //create two parties
                IMessaging foo = Factory.CreateMessaging();
                foo.Connector      = conn;
                foo.Identity       = "foo";
                foo.Sent          += TestHelper.Sent;
                foo.SetupReceiver += delegate(IModel channel) {
                    DeclareQueue(channel, foo.Identity);
                };
                foo.Init();
                IMessaging bar = Factory.CreateMessaging();
                bar.Connector      = conn;
                bar.Identity       = "bar";
                bar.Sent          += TestHelper.Sent;
                bar.SetupReceiver += delegate(IModel channel) {
                    DeclareQueue(channel, bar.Identity);
                };
                bar.Init();

                //send message from foo to bar
                IMessage mf = foo.CreateMessage();
                mf.Body = TestHelper.Encode("message1");
                mf.To   = "bar";
                foo.Send(mf);

                //receive message at bar and reply
                IReceivedMessage rb = bar.Receive();
                TestHelper.LogMessage("recv", bar, rb);
                IMessage mb = bar.CreateReply(rb);
                mb.Body = TestHelper.Encode("message2");
                bar.Send(mb);
                bar.Ack(rb);

                //receive reply at foo
                IReceivedMessage rf = foo.Receive();
                TestHelper.LogMessage("recv", foo, rf);
                foo.Ack(rf);
            }
        }
Example #2
0
        void Run(Address me, Address you, AmqpTcpEndpoint server, int sleep)
        {
            ConnectionFactory connectionFactory = new ConnectionFactory();

            connectionFactory.Endpoint = server;
            using (IConnector conn =
                       Factory.CreateConnector(new ConnectionBuilder(connectionFactory))) {
                IMessaging m = Factory.CreateMessaging();
                m.Connector    = conn;
                m.Identity     = me;
                m.Sent        += Sent;
                m.SetupSender += delegate(IModel channel) {
                    //We declare the recipient queue here to avoid
                    //sending messages into the ether. That's an ok
                    //thing to do for testing
                    channel.QueueDeclare(you, true, false, false, null);
                };
                m.SetupReceiver += delegate(IModel channel) {
                    channel.QueueDeclare(me, true, false, false, null);
                };
                m.Init();
                byte[] body = new byte[0];
                for (int i = 0;; i++)
                {
                    //send
                    IMessage msg = m.CreateMessage();
                    msg.Body = body;
                    msg.To   = you;
                    msg.Properties.SetPersistent(true);
                    m.Send(msg);
                    pending.Add(msg.MessageId, true);
                    //handle inbound
                    while (true)
                    {
                        IReceivedMessage r = m.ReceiveNoWait();
                        if (r == null)
                        {
                            break;
                        }
                        if (r.CorrelationId == null)
                        {
                            recv++;
                            DisplayStats();
                            m.Send(m.CreateReply(r));
                        }
                        else
                        {
                            if (pending.ContainsKey(r.CorrelationId))
                            {
                                pending.Remove(r.CorrelationId);
                                pend--;
                            }
                            else
                            {
                                disc++;
                            }
                            DisplayStats();
                        }
                        m.Ack(r);
                    }
                    //Slow down to prevent pending from growing w/o bounds
                    System.Threading.Thread.Sleep(sleep + pend);
                }
            }
        }
        protected void TestRelayedHelper(MessagingClosure senderSetup,
                                         MessagingClosure receiverSetup,
                                         ConnectionBuilder builder)
        {
            using (IConnector conn = Factory.CreateConnector(builder),
                   relayConn = Factory.CreateConnector(builder)) {
                //create relay
                IMessaging relay = Factory.CreateMessaging();
                relay.Connector    = relayConn;
                relay.Identity     = "relay";
                relay.ExchangeName = "out";
                relay.SetupSender += delegate(IModel channel) {
                    DeclareExchange(channel, relay.ExchangeName, "direct");
                };
                relay.SetupReceiver += delegate(IModel channel) {
                    DeclareExchange(channel, "in", "fanout");
                    DeclareQueue(channel, relay.QueueName);
                    BindQueue(channel, relay.QueueName, "in", "");
                };
                relay.Init();

                //run relay
                new System.Threading.Thread
                    (delegate() {
                    //receive messages and pass it on
                    IReceivedMessage r;
                    try {
                        while (true)
                        {
                            r = relay.Receive();
                            relay.Send(r);
                            relay.Ack(r);
                        }
                    }
                    catch (EndOfStreamException) {
                    }
                    catch (AlreadyClosedException) {
                    }
                    catch (OperationInterruptedException) {
                    }
                }).Start();

                //create two parties
                IMessaging foo = Factory.CreateMessaging();
                foo.Connector      = conn;
                foo.Identity       = "foo";
                foo.SetupSender   += senderSetup(foo);
                foo.SetupReceiver += receiverSetup(foo);
                foo.ExchangeName   = "in";
                foo.Sent          += TestHelper.Sent;
                foo.Init();
                IMessaging bar = Factory.CreateMessaging();
                bar.Connector      = conn;
                bar.Identity       = "bar";
                bar.SetupSender   += senderSetup(bar);
                bar.SetupReceiver += receiverSetup(bar);
                bar.ExchangeName   = "in";
                bar.Sent          += TestHelper.Sent;
                bar.Init();

                //send message from foo to bar
                IMessage mf = foo.CreateMessage();
                mf.Body = TestHelper.Encode("message1");
                mf.To   = "bar";
                foo.Send(mf);

                //receive message at bar and reply
                IReceivedMessage rb = bar.Receive();
                TestHelper.LogMessage("recv", bar, rb);
                IMessage mb = bar.CreateReply(rb);
                mb.Body = TestHelper.Encode("message2");
                bar.Send(mb);
                bar.Ack(rb);

                //receive reply at foo
                IReceivedMessage rf = foo.Receive();
                TestHelper.LogMessage("recv", foo, rf);
                foo.Ack(rf);
            }
        }
Example #4
0
 private void Ack(IReceivedMessage message)
 {
     _messaging.Ack(message);
 }