1. Go to this page and download the library: Download webit/message-bus-amqp 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');
/* Start to develop here. Best regards https://php-download.com/ */
webit / message-bus-amqp example snippets
use Webit\MessageBus\Infrastructure\Amqp\Connection\Pool\ConnectionPoolBuilder;
use Webit\MessageBus\Infrastructure\Amqp\Connection\ConnectionParams;
$builder = ConnectionPoolBuilder::create();
// optionally set connection factory (LazyConnectionFactory used by default)
$builder->setConnectionFactory(
new \Webit\MessageBus\Infrastructure\Amqp\Connection\InstantConnectionFactory()
);
// optionally add logger (use a smarter one in real life)
$logger = new \Psr\Log\NullLogger();
$builder->setLogger($logger);
// register at least one connection
$builder->registerConnection(
new ConnectionParams(
'rabbitmq.host',
'5672', // port,
'my-username',
'my-password'
),
'connection-1'
);
$connectionPool = $builder->build();
use Webit\MessageBus\Infrastructure\Amqp\Connection\Channel\NewChannelConnectionAwareChannelFactory;
use Webit\MessageBus\Infrastructure\Amqp\Publisher\ExchangePublicationTarget;
use Webit\MessageBus\Infrastructure\Amqp\Publisher\QueuePublicationTarget;
use Webit\MessageBus\Infrastructure\Amqp\Publisher\AmqpPublisher;
use Webit\MessageBus\Infrastructure\Amqp\Publisher\Routing\FromMessageTypeRoutingKeyResolver;
$channelFactory = new NewChannelConnectionAwareChannelFactory($connectionPool);
$publicationTarget = new ExchangePublicationTarget(
$channelFactory,
new FromMessageTypeRoutingKeyResolver(), // you can provide your implementation
'exchange-name'
);
// or
$publicationTarget = new QueuePublicationTarget(
$channelFactory,
'queueName'
);
$publisher = new AmqpPublisher($publicationTarget);
$message = new Message('my-type', 'message_content');
$publisher->publish($message);
use Webit\MessageBus\Consumer;
use Webit\MessageBus\Message;
class \MyConsumer implements Consumer
{
public function consume(Message $message)
{
// do your stuff here
}
}
use Webit\MessageBus\Infrastructure\Amqp\Listener\Message\MessageFactory;
use Webit\MessageBus\Infrastructure\Amqp\Listener\AmqpConsumerBuilder;
$builder = AmqpConsumerBuilder::create();
$builder->setConsumer(new \MyConsumer());
$builder->setLogger(new NullLogger()); // optional
$builder->shouldSendFeedback(false); // if you don't want to acknowledge messages, set this to false (true by default)
$builder->setMessageFactory(new SimpleMessageFactory()); // optionally set your MessageFactory
$amqpConsumer = $builder->build();