PHP code example of php-amqplib / rabbitmq-bundle
1. Go to this page and download the library: Download php-amqplib/rabbitmq-bundle library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
php-amqplib / rabbitmq-bundle example snippets
$msg = array ('user_id' => 1235 , 'image_path' => '/path/to/new/pic.png' );
$this ->get('old_sound_rabbit_mq.upload_picture_producer' )->publish(serialize($msg));
public function registerBundles ()
{
$bundles = array (
new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(),
);
}
use OldSound \RabbitMqBundle \DependencyInjection \OldSoundRabbitMqExtension ;
use OldSound \RabbitMqBundle \DependencyInjection \Compiler \RegisterPartsPass ;
$containerBuilder->registerExtension(new OldSoundRabbitMqExtension());
$containerBuilder->addCompilerPass(new RegisterPartsPass());
class ConnectionParametersProviderService implements ConnectionParametersProvider {
...
public function getConnectionParameters () {
return array ('vhost' => $this ->getVhost());
}
...
}
public function indexAction ($name)
{
$msg = array ('user_id' => 1235 , 'image_path' => '/path/to/new/pic.png' );
$this ->get('old_sound_rabbit_mq.upload_picture_producer' )->publish(serialize($msg));
}
$this ->get('old_sound_rabbit_mq.upload_picture_producer' )->setContentType('application/json' );
namespace App \EventListener ;
use OldSound \RabbitMqBundle \Event \BeforeProducerPublishMessageEvent ;
use Symfony \Component \EventDispatcher \Attribute \AsEventListener ;
final class AMQPBeforePublishEventListener
{
public function __invoke (BeforeProducerPublishMessageEvent $event) : void
{
}
}
namespace App \EventListener ;
use OldSound \RabbitMqBundle \Event \AfterProducerPublishMessageEvent ;
use Symfony \Component \EventDispatcher \Attribute \AsEventListener ;
final class AMQPBeforePublishEventListener
{
public function __invoke (AfterProducerPublishMessageEvent $event) : void
{
}
}
class OnConsumeEvent extends AMQPEvent
{
const NAME = AMQPEvent::ON_CONSUME;
public function __construct (Consumer $consumer)
{
$this ->setConsumer($consumer);
}
}
class BeforeProcessingMessageEvent extends AMQPEvent
{
const NAME = AMQPEvent::BEFORE_PROCESSING_MESSAGE;
public function __construct (Consumer $consumer, AMQPMessage $AMQPMessage)
{
$this ->setConsumer($consumer);
$this ->setAMQPMessage($AMQPMessage);
}
}
class AfterProcessingMessageEvent extends AMQPEvent
{
const NAME = AMQPEvent::AFTER_PROCESSING_MESSAGE;
public function __construct (Consumer $consumer, AMQPMessage $AMQPMessage)
{
$this ->setConsumer($consumer);
$this ->setAMQPMessage($AMQPMessage);
}
}
class OnIdleEvent extends AMQPEvent
{
const NAME = AMQPEvent::ON_IDLE;
public function __construct (Consumer $consumer)
{
$this ->setConsumer($consumer);
$this ->forceStop = true ;
}
}
public function indexAction ($name, ProducerInterface $uploadPictureProducer)
{
$msg = array ('user_id' => 1235 , 'image_path' => '/path/to/new/pic.png' );
$uploadPictureProducer->publish(serialize($msg));
}
namespace Acme \DemoBundle \Consumer ;
use OldSound \RabbitMqBundle \RabbitMq \ConsumerInterface ;
use PhpAmqpLib \Message \AMQPMessage ;
class UploadPictureConsumer implements ConsumerInterface
{
public function execute (AMQPMessage $msg)
{
$isUploadSuccess = someUploadPictureMethod();
if (!$isUploadSuccess) {
return false ;
}
}
}
public function indexAction ($name)
{
$client = $this ->get('old_sound_rabbit_mq.integer_store_rpc' );
$client->addRequest(serialize(array ('min' => 0 , 'max' => 10 )), 'random_int' , 'request_id' );
$replies = $client->getReplies();
}
public function indexAction ($name)
{
$expiration = 5000 ;
$client = $this ->get('old_sound_rabbit_mq.integer_store_rpc' );
$client->addRequest($body, $server, $requestId, $routingKey, $expiration);
try {
$replies = $client->getReplies();
} catch (\PhpAmqpLib\Exception \AMQPTimeoutException $e) {
}
}
public function indexAction ($name)
{
$client = $this ->get('old_sound_rabbit_mq.parallel_rpc' );
$client->addRequest($name, 'char_count' , 'char_count' );
$client->addRequest(serialize(array ('min' => 0 , 'max' => 10 )), 'random_int' , 'random_int' );
$replies = $client->getReplies();
}
public function indexAction ()
{
$msg = array ('user_id' => 1235 , 'image_path' => '/path/to/new/pic.png' );
$additionalProperties = ['priority' => 10 ] ;
$routing_key = '' ;
$this ->get('old_sound_rabbit_mq.upload_picture_producer' )->publish(serialize($msg), $routing_key , $additionalProperties );
}
namespace AppBundle \Service ;
use OldSound \RabbitMqBundle \RabbitMq \BatchConsumerInterface ;
use PhpAmqpLib \Message \AMQPMessage ;
class DevckBasicConsumer implements BatchConsumerInterface
{
public function batchExecute (array $messages)
{
echo sprintf('Doing batch execution%s' , PHP_EOL);
foreach ($messages as $message) {
$this ->executeSomeLogicPerMessage($message);
}
return true ;
}
}
namespace AppBundle \Service ;
use OldSound \RabbitMqBundle \RabbitMq \BatchConsumerInterface ;
use PhpAmqpLib \Message \AMQPMessage ;
class DevckBasicConsumer implements BatchConsumerInterface
{
public function batchExecute (array $messages)
{
echo sprintf('Doing batch execution%s' , PHP_EOL);
$result = [];
foreach ($messages as $message) {
$result[$message->getDeliveryTag()] = $this ->executeSomeLogicPerMessage($message);
}
return $result;
}
}
bash
$ composer
{
" " php-amqplib/rabbitmq-bundle": " ^2.0 ",
}
}