Reject() public méthode

Rejects a message. It sends a rejected outcome to the peer.
public Reject ( Amqp.Message message, Amqp.Framing.Error error = null ) : void
message Amqp.Message The message to reject.
error Amqp.Framing.Error The error, if any, for the rejection.
Résultat void
Exemple #1
0
        public static void Main(string[] args)
        {
            Trace.TraceLevel = TraceLevel.Frame;
            Trace.TraceListener = (f, a) =>
            {
                var t = DateTime.Now.ToString("[hh:ss.fff]") + " " + string.Format(f, a);
                Console.WriteLine(t);
            };

            connection = new Connection(amqpAddress, null, new Open()
            {
                ContainerId = Guid.NewGuid().ToString(),
                ChannelMax = 64,
                MaxFrameSize = 200,
            }, null);
            connection.Closed = OnClosed;

            session = new Session(connection);
            session.Closed = OnClosed;

            var linkName = Guid.NewGuid().ToString();
            senderLink = new SenderLink(session, linkName, new Attach()
            {
                Target = new Target()
                {
                    Address = "TestQueue1",
                },
                //RcvSettleMode = ReceiverSettleMode.Second,
                //SndSettleMode = SenderSettleMode.Settled,
            }, null);
            senderLink.Closed = OnClosed;

            for (int i = 0; i < 10; i++)
            {
                senderLink.Send(CreateMessage(), 5000);
            }

            senderLink.Close();

            linkName = Guid.NewGuid().ToString();
            receiverLink = new ReceiverLink(session, linkName, "TestQueue1");
            receiverLink.Closed = OnClosed;
            receiverLink.SetCredit(1);
            var message = receiverLink.Receive(20000);
            int receiveCount = 0;
            while(message != null)
            {
                receiveCount++;
                //Console.WriteLine(message.Body.GetType());
                Console.WriteLine(message.BodySection.GetType());
                Console.WriteLine("Receive #{0}. Message = \"{1}\"", receiveCount.ToString(), Encoding.UTF8.GetString(message.GetBody<byte[]>()));
                if (receiveCount % 7 == 0)
                    receiverLink.Release(message);
                else if (receiveCount % 4 == 0)
                    receiverLink.Reject(message);
                else
                    receiverLink.Accept(message);
                Thread.Sleep(10000);
                message = receiverLink.Receive(20000);
            }
            receiverLink.Close();

            session.Close();
            connection.Close();
        }
        public void ContainerHostSourceLinkEndpointTest()
        {
            string name = MethodInfo.GetCurrentMethod().Name;
            int count = 100;
            Queue<Message> messages = new Queue<Message>();
            for (int i = 0; i < count; i++)
            {
                messages.Enqueue(new Message("test") { Properties = new Properties() { MessageId = name + i } });
            }

            var source = new TestMessageSource(messages);
            this.host.RegisterLinkProcessor(new TestLinkProcessor(link => new SourceLinkEndpoint(source, link)));

            var connection = new Connection(Address);
            var session = new Session(connection);
            var receiver = new ReceiverLink(session, "receiver0", name);
            int released = 0;
            int rejected = 0;
            for (int i = 1; i <= count; i++)
            {
                Message message = receiver.Receive();
                if (i % 5 == 0)
                {
                    receiver.Reject(message);
                    rejected++;
                }
                else if (i % 17 == 0)
                {
                    receiver.Release(message);
                    released++;
                }
                else
                {
                    receiver.Accept(message);
                }
            }

            receiver.Close();
            session.Close();
            connection.Close();

            Assert.AreEqual(released, messages.Count);
            Assert.AreEqual(rejected, source.DeadletterMessage.Count);
        }
        private void TryHandleMessage(ReceiverLink receiverLink, Message amqpMessage)
        {
            // NOTE this method _should not_ throw an exception!
            var threadName = receiverLink.Name;
            var messageID = amqpMessage.Properties?.MessageId ?? "?";
            try
            {
                logger.Debug("{0}: Received Message with MessageId={1}", threadName, messageID);

                var headers = new Dictionary<string, string>();
                if (amqpMessage.ApplicationProperties != null)
                {
                    foreach (var key in amqpMessage.ApplicationProperties.Map.Keys)
                    {
                        headers[key.ToString()] = amqpMessage.ApplicationProperties.Map[key].ToString();
                    }
                }

                object message = null;
                try
                {
                    message = DecodeMessage(amqpMessage);
                }
                catch (Exception e)
                {
                    logger.Error(e, "{0}: Cannot Decode Message. Will not re-enqueue.", threadName);
                    OnPoisonMessageDetected(new PoisonMessageDetectedEventArgs()
                    {
                        QueueName = ReceiverLinkAddress,
                        Retries = MaxRetries,
                        Exception = e,
                        MessageId = messageID,
                        ErrorCode = "DecodeException",
                    });
                }

                if (message != null)
                {
                    var transportMessage = new IncomingTransportMessage(messageID, headers, message);

                    Exception lastException = null;
                    if (faultManager.HasMaxRetriesExceeded(transportMessage, out lastException))
                    {
                        logger.Error(lastException, "{0}: MaxRetriesExceeded for MessageId={1}. Will not re-enqueue.", threadName, messageID.ToString());
                        OnPoisonMessageDetected(new PoisonMessageDetectedEventArgs()
                        {
                            QueueName = ReceiverLinkAddress,
                            Retries = MaxRetries,
                            Exception = lastException,
                            MessageId = messageID,
                            ErrorCode = "MaxRetriesExceeded",
                        });
                    }
                    else
                    {
                        logger.Debug("{0}: Notifying observers of new TransportMessage for MessageId={1}.", threadName, messageID.ToString());
                        OnMessageAvailable(transportMessage);
                        faultManager.ClearFailuresForMessage(messageID);
                    }
                }

                receiverLink.Accept(amqpMessage);
                logger.Debug("{0}: Accepting MessageId={1}", threadName, messageID.ToString());
            }
            catch (Exception e)
            {
                faultManager.IncrementFailuresForMessage(messageID.ToString(), e);
                receiverLink.Reject(amqpMessage);
                logger.Error(e, "{0}: Exception caught handling MessageId={1}. Rejecting.", threadName, messageID.ToString());
                Thread.Sleep(1000); // TODO possibly implement a backoff with the fault manager based on number of retries?
            }
        }