Keeping connection alive always

722 views
Skip to first unread message

tester...@gmail.com

unread,
Jul 3, 2015, 9:31:24 AM7/3/15
to rabbitm...@googlegroups.com
Hi all,

I am new to RabbitMQ. I am using rabbitMQ for my application queuing with HA and clustering. As always my connection timed out. I lost my connections as well as the queue. I am frustrated with the issue for quite some time . Would this be useful if I try to send an impulse at a regular interval with this simple code snippet. The idea behind this is much simpler as I always want the connection to be busy at a regular interval, thus never dies.

please keep this in mind as I am undergoing with a new Rabbit server setup and It will take some time. My old one does not have any heartbeat facility. So for this time being I need to keep the connection alive.

require_once('application/libraries/rabbitmq/amqp.inc');
        $host =  $this->config->item('HOST');
        $port = $this->config->item('PORT');
        $user = $this->config->item('USER');
        $password = $this->config->item('PASSWORD');
        $vhost = $this->config->item('VHOST');
        $connection = new AMQPConnection($host, $port, $user, $password, $vhost);
        $channel = $connection->channel();
        $exchange = $queue = 'ha-pacemaker';
            $channel->queue_declare($queue, false, true, false, true);
            $channel->exchange_declare($exchange, 'direct', false, true, false);
            $channel->queue_bind($queue, $exchange);
            $rabbitmq_data['send_email'] = du...@email.addr';
            $msg_body = json_encode($rabbitmq_data);
            $msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery-mode' => 2));
            $channel->basic_publish($msg, $exchange);
        }
        $channel->close();
        $connection->close();

Does anyone have any better idea? Thanx in advance.

Alvaro Videla

unread,
Jul 3, 2015, 9:32:51 AM7/3/15
to tester...@gmail.com, rabbitm...@googlegroups.com
You seem to be using an old version of php-amqplib, any reason for that?

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

tester...@gmail.com

unread,
Jul 3, 2015, 9:44:35 AM7/3/15
to rabbitm...@googlegroups.com, tester...@gmail.com
Hi Alvaro,

Yes, this is my old server setup, I am switching this to a new one. Thats why for this time being I am using this library.

Alvaro Videla

unread,
Jul 3, 2015, 9:52:45 AM7/3/15
to tester...@gmail.com, rabbitm...@googlegroups.com
In that code you are publishing a message and the closing the channel and connection right? What's the point of that if you are trying to keep the connection alive?

BTW, if your goal is to keep the queue alive, then don't declare the queue as exclusive/auto_delete. If the queue is durable and not exclusive, then it should survive consumer restarts

tester...@gmail.com

unread,
Jul 3, 2015, 10:21:55 AM7/3/15
to rabbitm...@googlegroups.com, tester...@gmail.com
Hi Alvaro,
Thanx for quick reply.  actually I need to keep my consumers conection alive all the time not the producer. The simple logic behind this is, I am running my producer through cron. As after the queue is getting generated there is no need to keep producer connection alive. But the worker or consumer will be initiated once. And this connection should never die. If this is also done by cron I will get a long list of consumer connection at the end of the day, which is annoying.

So the punchline here is:

Produce the queue and close the connection by cron every time.
Consume started once and goes on forever. Anything queued will be consumed instantly.

Can this architecture be built in my old system. The new code I am using for my new Rabbit instance is written by you. That is working fine in beta.

Thanx in advance.

Alvaro Videla

unread,
Jul 3, 2015, 10:35:31 AM7/3/15
to tester...@gmail.com, rabbitm...@googlegroups.com
You don't need to publish a message every time, maybe you can re-send a basic_qos from your consumer, or an access_request call from time to time, since those methods will be faster.

tester...@gmail.com

unread,
Jul 6, 2015, 1:15:48 AM7/6/15
to rabbitm...@googlegroups.com, tester...@gmail.com
Hi Alvaro,

Thank you for your suggestion. Now I have a situation here. I have crossed a way to do my connections everlasting. in the last Friday I created a connection of worker and made it as TCP keep alive on and with heartbeat of 30s and let my system running for the weekend. After two days I found this alive, everything is working fine.  I got the connection, the channel, the blank queue all alive.

But as soon as I queued two messages, everything except the queue were gone all of a sudden. I get the hike in the graph for two messages. and this stands still. Now, this is the problem I am facing for last few days. Was this connection actually alive? If yes, what happened to them while queuing? How to stop this behavior. I need to get the messages to be consumed always after any which idle time. No matter what.

Please show me the way. Thanx in advance.

Alvaro Videla

unread,
Jul 13, 2015, 9:24:22 AM7/13/15
to tester...@gmail.com, rabbitm...@googlegroups.com
>But as soon as I queued two messages, everything except the queue were gone all of a sudden.

What does this means? What's on the server logs? Which code are you using to publish said messages?
Reply all
Reply to author
Forward
0 new messages