How to Test Publisher Confirms Code in RabbitMq.NET

1,154 views
Skip to first unread message

Ping

unread,
Mar 15, 2018, 9:28:52 AM3/15/18
to rabbitmq-users
I write the dummy code below trying to learn publisher confirms based on the links below.

My questions

1 Is the code correct in terms of implementing publisher confirms?

2 How can I test if it works, e.g. when server crashes. 

 
   class PublisherConfirms
   
{
       
public void Test(string[] args)
       
{
           
var factory = new ConnectionFactory() { HostName = "localhost" };
           
using (var connection = factory.CreateConnection())
           
using (var channel = connection.CreateModel())
           
{
                channel
.QueueDeclare(
                    queue
: "task_queue",              
                    durable
: true,
                    exclusive
: false,
                    autoDelete
: false,
                    arguments
: null);


               
var message = GetMessage(args);
               
var body = Encoding.UTF8.GetBytes(message);


                channel
.BasicAcks += Channel_BasicAcks;


               
//1
                channel
.ConfirmSelect();  //You’ll get an IllegalStateException if you try to use either variation of WaitForConfirms without first setting the Confirms property with ConfirmSelect.


               
var properties = channel.CreateBasicProperties();
                properties
.SetPersistent(true);


               
//messages are routed to the queue with the name specified by routingKey, if it exists.
                channel
.BasicPublish(
                    exchange
: "",
                    routingKey
: "task_queue",
                    basicProperties
: properties,
                    body
: body);


               
//2
                channel
.WaitForConfirmsOrDie();
               
Console.WriteLine(" [x] Sent {0}", message);
           
}


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


       
private void Channel_BasicAcks(object sender, RabbitMQ.Client.Events.BasicAckEventArgs e)
       
{
           
throw new NotImplementedException();
       
}


       
private static string GetMessage(string[] args)
       
{
           
return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!");
       
}
   
}


Arnaud Cogoluègnes

unread,
Mar 15, 2018, 9:42:57 AM3/15/18
to rabbitm...@googlegroups.com
Your code looks correct. Note you're using the synchronous flavor, meaning your application blocks channel.WaitForConfirmsOrDie() until the message is confirmed. You can also use a callback to be notified when the message is confirmed.

Publishing failure at the broker level can be hard to test, you'd better use some mocking library to mock the client and test your application logic.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ping

unread,
Mar 15, 2018, 9:54:59 AM3/15/18
to rabbitmq-users
Hi Arnaud,

"your application blocks channel.WaitForConfirmsOrDie() until the message is confirmed. You can also use a callback to be notified when the message is confirmed"

Can you please show me the code example? I am new to RabbitMQ.NET? Is it using this below which is subscribed in my code? What is the better method with async than using WaitForConfirmsOrDie()?

channel.BasicAcks += Channel_BasicAcks;




"Publishing failure at the broker level can be hard to test, you'd better use some mocking library to mock the client and test your application logic.
I know the mocking, but I want to verify Publisher Confirm by server if possible. It is tricky I think.


Michael Klishin

unread,
Mar 15, 2018, 9:56:17 AM3/15/18
to rabbitm...@googlegroups.com
I don't know what the hell is going on with Google Groups but there absolutely were threads about simulation of nacks.
Yet I cannot find anything.

At the very least you can restart the node but that's trivial and won't send nacks. To force a node to send nacks
you need to use rabbitmqctl eval to kill queue master process in a loop, so that at some point a message comes in right before it is terminated.

To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Michael Klishin

unread,
Mar 15, 2018, 9:58:01 AM3/15/18
to rabbitm...@googlegroups.com
There are event handlers on IModel.
Search for "IModel.BasicAcks event handler" in http://www.rabbitmq.com/dotnet-api-guide.html.


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ping

unread,
Mar 15, 2018, 10:08:19 AM3/15/18
to rabbitmq-users
Hi Michael,

Thanks. I am new to RabbitMQ. 

When exactly should RabbitMQ server be killed? Do you mean after channel.BasicPublish() is called? In that case, how is it possible for me to issue rabbitmqctl command to kill it? Because it is like ms time.

What exact argument should be used with rabbitmqctl ?

rabbitmqctl  shutdown

or

rabbitmqctl stop pid


Michael Klishin

unread,
Mar 15, 2018, 10:30:20 AM3/15/18
to rabbitm...@googlegroups.com
`rabbitmqctl stop` but that's not the scenario you want because it will close all client connections cleanly.

You probably want to just kill the running OS process or block traffic using iptables or similar.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ping

unread,
Mar 15, 2018, 10:35:24 AM3/15/18
to rabbitmq-users
What about the timing that I can kill rabbitMQ server? is it possible to do it?


On Thursday, March 15, 2018 at 2:30:20 PM UTC, Michael Klishin wrote:
`rabbitmqctl stop` but that's not the scenario you want because it will close all client connections cleanly.

You probably want to just kill the running OS process or block traffic using iptables or similar.
On Thu, Mar 15, 2018 at 5:08 PM, Ping <pingpongo...@gmail.com> wrote:
Hi Michael,

Thanks. I am new to RabbitMQ. 

When exactly should RabbitMQ server be killed? Do you mean after channel.BasicPublish() is called? In that case, how is it possible for me to issue rabbitmqctl command to kill it? Because it is like ms time.

What exact argument should be used with rabbitmqctl ?

rabbitmqctl  shutdown

or

rabbitmqctl stop pid


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Chandu Prasad K

unread,
Mar 22, 2018, 8:33:48 AM3/22/18
to rabbitmq-users
hi

 am prasad

I am new in software technologie, my friend is a developer in it tech services company. I think he is helping, and he is free cognos training  material for it students. I shared this link to my friend.

Reply all
Reply to author
Forward
0 new messages