2

I have written a function which checks if rabbitmq is running.

function getBrokerStatus()
{
    log_message("info", "Checking if broker is running....");
    try {
        $amqpConnection = new AMQPConnection();
        $amqpConnection->setLogin("guest");
        $amqpConnection->setPassword("guest");
        $amqpConnection->setVhost("/");
        $amqpConnection->connect();
    } catch (Exception $e) {
        log_message("info", "Exception: " . $e->getMessage());
        return false;
    }

    if (!$amqpConnection->isConnected()) {
        log_message("info", "Cannot connect to the broker! It might not be running");
        return false;
    }
    $amqpConnection->disconnect();
    return true;
}

My code catches this exception. I see below in logs -

Exception: Socket error: could not connect to host.

But my rabbitmq server is running then why am I getting this exception? I am using v3.1.1 of rabbitmq-server.

Hussain
  • 5,057
  • 6
  • 45
  • 71
  • Since your code doesn't state either the broker's host or the host the status checker is trying to make a connection to, it's hard to judge. – marekful Mar 07 '14 at 10:17
  • If it helps, when I print `new AMQPConnection()` instance I get - `AMQPConnection Object ( [login] => guest [password] => guest [host] => localhost [vhost] => / [port] => 5672 [read_timeout] => 0 [write_timeout] => 0 ) ` – Hussain Mar 07 '14 at 10:23
  • It's strange. Obviously, the connection attempt to the same host:port should result the same. My best guess is that there must be some difference. That's not much to say, but that's how I'd go about it. To find it. – marekful Mar 07 '14 at 10:44
  • Please, specify platform versions. Do you have access to RabbitMQ management panel? – pinepain Mar 07 '14 at 12:01
  • I am using `v3.1.1` of rabbitmq-server. I think 'http://localhost:5672` will take me to RabbitMQ management panel. But I am not able to access it. I mean that url is not found – Hussain Mar 07 '14 at 12:36
  • You have to enable the management panel. Also make sure you've started RabbitMQ. – pinepain Mar 07 '14 at 12:50
  • Hi I have enabled it using `sudo rabbitmq-plugins enable rabbitmq_management` and I can see it at `http://localhost:15672`. But still I am getting the same error. – Hussain Mar 07 '14 at 13:29
  • I took advice from @asksolem and used `$amqpConnection->setHost("127.0.0.1");` It is working now. But I don't know what's the issue with `localhost` – Hussain Mar 10 '14 at 06:18

1 Answers1

0

if you cannot ping your localhost on your machine, it must be you didn't setup your hosts file. Edit it in /etc/hosts

meilei007
  • 1
  • 1