How can I get the latest message in a queue?

17 views
Skip to first unread message

ql...@mtu.edu

unread,
Aug 1, 2017, 2:13:35 PM8/1/17
to simpleamqpclient-users
Hello,

I want to get the latest message in a queue and discard all other messages.
I try to specify a maximum length using policy, but it does not work.

The code is as follows:

Channel::ptr_t pub = Channel::Create(ip, port, username, password); 
pub->DeclareExchange(exchange_name, "topic");

Channel::ptr_t subscriber = Channel::Create(ip, port, username, password);
Table properties;
properties.insert(TableEntry(TableKey("x-max-length"), TableValue(1)));
subscriber->DeclareQueue("car_test1", false, false, false, true, properties);
subscriber->BindQueue("car_test1", exchange_name, "car.#");
string consumer1 = subscriber->BasicConsume("car_test1");

Then I use pub send two messages to consumer1:

BasicMessage::ptr_t outgoing_message = BasicMessage::Create();
outgoing_message->Body("test");
pub->BasicPublish(exchange_name, "car.test1", outgoing_message);
outgoing_message->Body("test2");
pub->BasicPublish(exchange_name, "car.test1", outgoing_message);

I still get two messages. I don't know why.
So what can I do if I just want to get the last message in a queue?

Alan Antonuk

unread,
Aug 2, 2017, 5:55:27 PM8/2/17
to ql...@mtu.edu, simpleamqpclient-users
Your code calls BasicConsume() on car_test1 before any messages are published.  As soon as the first message is published to the queue, the broker is able to deliver the message, dropping the count of messages in the queue to 0, the same thing happens when you publish the 2nd message.

If you were to declare and bind the queue, then only start the consumer after publishing both messages, you would likely only get the 2nd message.

-Alan



--
You received this message because you are subscribed to the Google Groups "simpleamqpclient-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simpleamqpclient-...@googlegroups.com.
To post to this group, send email to simpleamqpc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/simpleamqpclient-users/a25b3e54-8a0e-459f-aec6-ce44ac841a24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

ql...@mtu.edu

unread,
Aug 3, 2017, 12:26:07 AM8/3/17
to simpleamqpclient-users
Thank you very much!
To unsubscribe from this group and stop receiving emails from it, send an email to simpleamqpclient-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages