Access to new RabbitMQ messages from windows service

1,882 views
Skip to first unread message

Алексей Рюмин

unread,
May 10, 2017, 5:25:17 AM5/10/17
to rabbitmq-users

Hello

I am trying to get new messages from RabbitMQ using windows service but event of recieving new message doesn't accure. This service could be launched as Console Application in Debug Mode. In this case the event occures and I get new messages. The service is launched with the user I use to log into Windows. This is a part of code with event handling


var factory = new ConnectionFactory() { HostName = "localhost" };
               
using (var conn = factory.CreateConnection())
               
{
                   
using (var channel = conn.CreateModel())
                   
{
                        channel
.ExchangeDeclare("sm_posts", "fanout");
                       
var argu = new Dictionary<string, object>();
                        argu
.Add("x-max-length", 10000);

                       
var consumerQueue = channel.QueueDeclare().QueueName;
                        channel
.QueueBind(queue: consumerQueue, exchange: "sm_posts", routingKey: "");
                       
var consumer = new EventingBasicConsumer(channel);

                        log
.Info("Waiting for new messages...");

                        consumer
.Received += (model, ea) =>
                       
{
                            count
++;
                           
var body = ea.Body;
                           
var message = Encoding.UTF8.GetString(body);

                            log
.Info(message + "\n\n\n\n\n");

So, why windows service doesn't get messages and if it is launched as Console application in Debug Mode it gets messages? Thanks

Michael Klishin

unread,
May 10, 2017, 5:35:27 AM5/10/17
to rabbitm...@googlegroups.com
See RabbitMQ and Windows logs for clues. It's not possible suggest much with the amount of information provided.

For example, are you sure that the node doesn't terminate when executed as a service?
Can it be running with a different config file? and so on.

 

--
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

Алексей Рюмин

unread,
May 10, 2017, 5:41:17 AM5/10/17
to rabbitmq-users
I deleted all logs from logs folder C:\Users\Administrator\AppData\Roaming\RabbitMQ\log - after I restart my windows service there are no log files appear.
Also after that I can stop my windows service and start it as console application - it works fine. So I suppose node is not terminated.
Also I jave another windows service which posts new messages to RabbitMQ exchange and it works without problems

среда, 10 мая 2017 г., 12:35:27 UTC+3 пользователь Michael Klishin написал:
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.

Michael Klishin

unread,
May 10, 2017, 5:41:58 AM5/10/17
to rabbitm...@googlegroups.com
I'd recommend taking a traffic capture as well as it will contain a lot of information
that will help you pin point the problem.

We have a brief Wireshark guide at https://www.rabbitmq.com/amqp-wireshark.html
but there really isn't much RabbitMQ-specific when it comes to traffic captures (or Wireshark).

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

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

Staff Software Engineer, Pivotal/RabbitMQ

Michael Klishin

unread,
May 10, 2017, 5:43:57 AM5/10/17
to rabbitm...@googlegroups.com
That's why I mentioned Windows even logs (or whatever they are called).

If no logs files are created this strongly suggest that either

 * The node does not start
 * It starts with a pretty different configuration

If the node does not start then your program should fail to connect with a reasonably
clear exception quite quickly. You haven't mentioned any exceptions or steps you're taking,
so this is nothing more than a guess at this point.
 

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.

Niels Berglund

unread,
May 10, 2017, 6:29:01 AM5/10/17
to rabbitm...@googlegroups.com
Are you sure your service is still running when the message is posted to the exchange? Also, Can you see the message on the queue after it has been posted, and not picked up by the service?

In essence, if your console app is working and it is the same code as your service, then it is an issue with the service doing something weird, not the receiving code.

Niels

Алексей Рюмин

unread,
May 10, 2017, 7:05:02 AM5/10/17
to rabbitmq-users
Thank you for advice to install wireshark
I installed it, rebooted windows server. And don't see any amqp traffic in wireshark. But windows service (which doesn't work properly as windows service) pulishes new messages to exchange, and console application recieves it (it writes it to database so it surely works).



среда, 10 мая 2017 г., 12:41:58 UTC+3 пользователь Michael Klishin написал:
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.
--
MK

Staff Software Engineer, Pivotal/RabbitMQ
Auto Generated Inline Image 1

Алексей Рюмин

unread,
May 10, 2017, 7:14:18 AM5/10/17
to rabbitmq-users
Niels, yes, I am sure the service is still running after message is posted to exchange. But there is no queue in web interface of RabbitMQ. So the queue is created only if the program is launched as Console Application ( I see it in web interface) and not if it is launched as windows service

среда, 10 мая 2017 г., 13:29:01 UTC+3 пользователь Niels Berglund написал:

Niels Berglund

unread,
May 10, 2017, 8:27:01 AM5/10/17
to rabbitmq-users
OK, so when you say it runs in debug mode, are you saying it runs in your editor while debugging? What if you run it as console, not debugging - do you see the same behavior then? If that is the case you may have a garbage collection issue. Perhaps change the connection, channel and consumer to global variables - in order to ensure they don't go out of scope.

Niels

Michael Klishin

unread,
May 10, 2017, 9:14:14 AM5/10/17
to rabbitm...@googlegroups.com
You need to start a capture and select a network interface first, see traffic being captured, then apply the filter.

In case you want to get to the bottom of it, please stop guessing and collect more data. One critical piece of data
that I haven't seen in this thread is server logs.

Debuggers can pause execution of a lot of things, including the heartbeat sender frame, which
will lead RabbitMQ to closing the connection. That's just one possible hypothesis that's trivial to prove
right or wrong if you take a close look at the server logs.


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.

--
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.

Алексей Рюмин

unread,
May 10, 2017, 10:23:03 AM5/10/17
to rabbitmq-users
Niels, I meant that I run it as a console application - just ran my .exe file of c# project. And In this case everything work fine. And it is very strange because several weeks ago I ended with another c# project with the same NUGET package for RabbitMQ (RabbitMQ.Client) and the code is very similar. But at that application I was given a Queue name and conncted to it. In this case consumer declare a queue and bind it to exchange. And I don't see any other differences between using RabbitMQ.Client in these projects



среда, 10 мая 2017 г., 15:27:01 UTC+3 пользователь Niels Berglund написал:
Auto Generated Inline Image 1

Niels Berglund

unread,
May 10, 2017, 11:17:06 AM5/10/17
to rabbitmq-users
Any chance you can create a minimal repro of this, and share the code. I.e. one which works in console mode, and not in service mode.

Niels

Алексей Рюмин

unread,
May 11, 2017, 3:23:46 AM5/11/17
to rabbitmq-users
Thank you a lot for your help!
I declared exchange and queue using rabbitmq web manager (not in c# code). And also in c# I used disposable objects for channel and connection. I suppose the were disposed after 1st event of consuming and service didn't go on with listening to new messages. So I deleted "using" constractions and it works fine

среда, 10 мая 2017 г., 18:17:06 UTC+3 пользователь Niels Berglund написал:

Michael Klishin

unread,
May 11, 2017, 5:58:55 AM5/11/17
to rabbitm...@googlegroups.com
As a general recommendation, even though we use `using` in the tutorials, connections
(and largely channels, if applicable) are supposed to be long lived in every messaging
protocol RabbitMQ supports.

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.
Reply all
Reply to author
Forward
0 new messages