RabbitMQ (PHP AMQP) - messages are not sent on every request

64 views
Skip to first unread message

pit3rek

unread,
Jun 23, 2023, 3:48:44 AM6/23/23
to rabbitmq-users

I have a problem with sending messages on every request. The current situation is that the message is sent after the 2nd or 3rd request. Below is the code of the function responsible for sending.

<?php
require_once __DIR__ . '../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Message\AMQPMessage;

function sendToRabbitMQ($content, $queue = 'queue-name') {
    $ssl_opts = ['cafile' => __DIR__ . '/ssl/ca_certificate.pem',
                 'local_cert' => __DIR__ . '/ssl/client_certificate.pem',
                 'local_pk' => __DIR__ . '/ssl/client_key.pem',
                 'ssl_version' => 'tlsv1.2',
                 'verify_peer' => true,
                 'allow_self_signed' => true,
                 'verify_peer_name' => true,
                ];
    $options = [ 'connection_timeout' => 10 ];
    if($content === false OR empty($content)) {
        return false;
    }
    try {
        $connection = new AMQPSSLConnection('server.host', 5671, 'user', 'password', 'vhost', $ssl_opts, $options);
        $msg = new AMQPMessage($content);
        $channel = $connection->channel();
        $channel->queue_declare($queue, false, true, false, false);
        $channel->basic_publish($msg, '', $queue);
        $channel->close();
        $connection->close();
    } catch(Exception $e) {
        return false;
    }
    return true;
}
?>

In RabbitMQ log when failed I get:

client unexpectedly closed TCP connection

Unfortunately, Exception returns nothing.

Restarting RabbitMQ server helps. But why?

Reply all
Reply to author
Forward
0 new messages