Last active
November 7, 2022 16:36
-
-
Save acid24/fc7202449e9a226f31dc to your computer and use it in GitHub Desktop.
RabbitMQ publish confirms mandatory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PhpAmqpLib\Message\AMQPMessage; | |
define('ROOT_DIR', realpath(dirname(dirname(__DIR__)))); | |
define('HOST', 'rabbitmq'); | |
define('PORT', 5672); | |
define('USER', 'guest'); | |
define('PASSWD', 'guest'); | |
define('VHOST', '/'); | |
define('AMQP_DEBUG', true); | |
require_once ROOT_DIR . '/library/vendor/autoload.php'; | |
$brokerConnection = new \PhpAmqpLib\Connection\AMQPStreamConnection(HOST, PORT, USER, PASSWD, VHOST); | |
$queue = 'test.queue'; | |
$exchange = 'test.exchange'; | |
$routingKey = 'my.routing.key'; | |
$channel = $brokerConnection->channel(); | |
$channel->confirm_select(); | |
$channel->set_ack_handler(function(AMQPMessage $message) { | |
echo "Message acked!", PHP_EOL; | |
var_dump($message); | |
}); | |
$channel->set_nack_handler(function(AMQPMessage $message) use ($channel, $exchange, $routingKey) { | |
echo "Message nacked!", PHP_EOL; | |
var_dump($message); | |
// resend | |
$channel->basic_publish($message, $exchange, $routingKey, $mandatory = true); | |
}); | |
$channel->set_return_listener(function($replyCode, $replyText, $exchange, $routingKey, AMQPMessage $message) use ($channel) { | |
echo "Message returned!", PHP_EOL; | |
var_dump($message); | |
// resend | |
$channel->basic_publish($message, $exchange, $routingKey, $mandatory = true); | |
}); | |
$channel->exchange_declare($exchange, $type = 'direct', $passive = false, $durable = true, $autoDelete = false); | |
$channel->queue_declare($queue, $passive = false, $durable = true, $exclusive = false, $autoDelete = false); | |
$channel->queue_bind($queue, $exchange, $routingKey); | |
$message = new AMQPMessage('hello!', array('content_type' => 'text/plain', 'delivery_mode' => 2)); | |
$channel->basic_publish($message, $exchange, $routingKey, $mandatory = true); | |
$channel->wait_for_pending_acks_returns(); |
- Have rabbitmq installed and running
- Have php installed and running (no need to have php-fpm installed, cli only it's fine)
- Have composer installed and globally available
- Create a folder and enter it
- Have phpamqplib installed (
composer require php-amqplib/php-amqplib
) - Copy the code in the gist in a publisher.php file
- Run
php publisher.php
Depending on your local setup you might need to alter a few things in the script.
For example, in the script the rabbitmq host is defined as rabbitmq, you might need to change that to localhost or whatever is relevant for your setup.
This line
define('ROOT_DIR', realpath(dirname(dirname(__DIR__))));
might need to be changed to probably just define('ROOT_DIR', realpath(__DIR__));
This line
require_once ROOT_DIR . '/library/vendor/autoload.php';
should then be require_once ROOT_DIR . '/vendor/autoload.php';
You can monitor if the script sent a message by using rabbitmq's management interface
Good luck!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brother, How to test this process?