1

I have a PHP program where I connect to a Rabbit MQ server and retrieve messages. I have put this functionality inside a function:

function get_messages()
{
    $connection = new AMQPConnection();
    $connection->setLogin($rabbit_username);
    $connection->setPassword($rabbit_passwd);
    $connection->setHost($rabbit_host);

    while (!$connection->connect())
    {
            echo "## Trying to connect to Rabbit MQ...\n";
            sleep(1);
    }

    $amqpchn = new AMQPChannel($connection);
    $mq = new AMQPQueue($amqpchn);
    $mq->setName("myqueue");
    $mq->setFlags(AMQP_DURABLE|AMQP_PASSIVE);
    $mq->declare();   // must declare then bind
    $mq->bind("my.exchange","my.routing");

    // do stuff
}

This works fine. However when I try to run the function get_messages() from inside a thread (just one thread), the code gets stuck at $connection->connect(). It cannot connect to the Rabbit server.

Any ideas why this happens?

Thanks in advance

4

0 回答 0