PHP code example of longhao / yii2-amqp
1. Go to this page and download the library: Download longhao/yii2-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/ */
longhao / yii2-amqp example snippets
return [
'amqp' => [
'use' => 'production',
'properties' => [
'production' => [
'host' => 'localhost',
'port' => 5672,
'username' => 'username',
'password' => 'password',
'vhost' => '/',
'exchange' => 'amq.topic',
'exchange_type' => 'topic',
'consumer_tag' => 'consumer',
'ssl_options' => [], // See https://secure.php.net/manual/en/context.ssl.php
'connect_options' => [], // See https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Connection/AMQPSSLConnection.php
'queue_properties' => ['x-ha-policy' => ['S', 'all']],
'exchange_properties' => [],
'timeout' => 0
],
],
]
];
/*
|--------------------------------------------------------------------------
| Register Component
|--------------------------------------------------------------------------
*/
//...
'components' => [
'Amqp' => [
'class' => 'Longhao\Amqp\Amqp'
]
]
//...
$amqp = new Amqp;
$amqp->publish('routing-key', 'message');
Yii::$app->Amqp->publish('routing-key', 'message');
$amqp = new Amqp;
$amqp->publish('routing-key', 'message' , ['queue' => 'queue-name']);
Yii::$app->Amqp->publish('routing-key', 'message' , ['queue' => 'queue-name']);
$amqp = new Amqp;
$amqp->publish('routing-key', 'message' , ['exchange' => 'amq.direct']);
Yii::$app->Amqp->publish('routing-key', 'message' , ['exchange' => 'amq.direct']);
$amqp = new Amqp;
$amqp->consume('queue-name', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
$resolver->stopWhenProcessed();
});
Yii::$ap->Amqp->consume('queue-name', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
$resolver->stopWhenProcessed();
});
$amqp = new Amqp;
$amqp->consume('queue-name', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
});
Yii::$app->Amqp->consume('queue-name', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
});
$amqp = new Amqp;
$amqp->consume('queue-name', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
}, [
'timeout' => 2,
'vhost' => 'vhost3'
]);
Yii::$app->Amqp->consume('queue-name', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
}, [
'timeout' => 2,
'vhost' => 'vhost3'
]);
$amqp = new Amqp;
$amqp->publish('', 'message' , [
'exchange_type' => 'fanout',
'exchange' => 'amq.fanout',
]);
Yii::$app->Amqp->publish('', 'message' , [
'exchange_type' => 'fanout',
'exchange' => 'amq.fanout',
]);
$amqp = new Amqp;
$amqp->consume('', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
}, [
'exchange' => 'amq.fanout',
'exchange_type' => 'fanout',
'queue_force_declare' => true,
'queue_exclusive' => true,
'persistent' => true//
Yii::$app->Amqp->consume('', function ($message, $resolver) {
var_dump($message->body);
$resolver->acknowledge($message);
}, [
'exchange' => 'amq.fanout',
'exchange_type' => 'fanout',
'queue_force_declare' => true,
'queue_exclusive' => true,
'persistent' => true//
batch
$ php composer update
$ php composer