Skip to content

Instantly share code, notes, and snippets.

@acid24
Last active November 7, 2022 16:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acid24/fc7202449e9a226f31dc to your computer and use it in GitHub Desktop.
Save acid24/fc7202449e9a226f31dc to your computer and use it in GitHub Desktop.
RabbitMQ publish confirms mandatory
<?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();
@LimonCoder
Copy link

Brother, How to test this process?

@acid24
Copy link
Author

acid24 commented Nov 7, 2022

  1. Have rabbitmq installed and running
  2. Have php installed and running (no need to have php-fpm installed, cli only it's fine)
  3. Have composer installed and globally available
  4. Create a folder and enter it
  5. Have phpamqplib installed (composer require php-amqplib/php-amqplib)
  6. Copy the code in the gist in a publisher.php file
  7. 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