Skip to content

Commit b2076e2

Browse files
committed
Merge pull request vyuldashev#34 from hartmut-ltd/v5.1
make creation of exchange, queue & binding configurable
2 parents 891d398 + 1e5c91d commit b2076e2

File tree

4 files changed

+88
-37
lines changed

4 files changed

+88
-37
lines changed

README.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,32 @@ After composer update is finished you need to add ServiceProvider to your `provi
1515
Add these lines to your `app/config/queue.php` file to `connections` array:
1616

1717
'rabbitmq' => [
18-
'driver' => 'rabbitmq',
18+
'driver' => 'rabbitmq',
1919

20-
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
21-
'port' => env('RABBITMQ_PORT', 5672),
20+
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
21+
'port' => env('RABBITMQ_PORT', 5672),
2222

23-
'vhost' => env('RABBITMQ_VHOST', '/'),
24-
'login' => env('RABBITMQ_LOGIN', 'guest'),
25-
'password' => env('RABBITMQ_PASSWORD', 'guest'),
23+
'vhost' => env('RABBITMQ_VHOST', '/'),
24+
'login' => env('RABBITMQ_LOGIN', 'guest'),
25+
'password' => env('RABBITMQ_PASSWORD', 'guest'),
2626

27-
'queue' => env('RABBITMQ_QUEUE'), // name of the default queue,
28-
29-
'queue_params' => [
30-
'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),
31-
'durable' => env('RABBITMQ_QUEUE_DURABLE', true),
32-
'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
33-
'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
27+
'queue' => env('RABBITMQ_QUEUE'), // name of the default queue,
28+
29+
'exchange_declare' => env('RABBITMQ_EXCHANGE_DECLARE', true), // create the exchange if not exists
30+
'queue_declare_bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true), // create the queue if not exists and bind to the exchange
31+
32+
'queue_params' => [
33+
'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),
34+
'durable' => env('RABBITMQ_QUEUE_DURABLE', true),
35+
'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
36+
'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
3437
],
3538

36-
'exchange_params' => [
37-
'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'), // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
38-
'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
39-
'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), // the exchange will survive server restarts
40-
'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
39+
'exchange_params' => [
40+
'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'), // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
41+
'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
42+
'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), // the exchange will survive server restarts
43+
'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
4144
],
4245

4346
],

src/FintechFab/LaravelQueueRabbitMQ/LaravelQueueRabbitMQServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class LaravelQueueRabbitMQServiceProvider extends ServiceProvider
1313
*/
1414
public function register()
1515
{
16+
$this->mergeConfigFrom(
17+
__DIR__.'/../../config/rabbitmq.php', 'queue.connections.rabbitmq'
18+
);
1619
}
1720

1821
/**

src/FintechFab/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class RabbitMQQueue extends Queue implements QueueContract
1515
protected $connection;
1616
protected $channel;
1717

18+
protected $declareExchange;
19+
protected $declareBindQueue;
20+
1821
protected $defaultQueue;
1922
protected $configQueue;
2023
protected $configExchange;
@@ -29,6 +32,8 @@ public function __construct(AMQPConnection $amqpConnection, $config)
2932
$this->defaultQueue = $config['queue'];
3033
$this->configQueue = $config['queue_params'];
3134
$this->configExchange = $config['exchange_params'];
35+
$this->declareExchange = $config['exchange_declare'];
36+
$this->declareBindQueue = $config['queue_declare_bind'];
3237

3338
$this->channel = $this->getChannel();
3439
}
@@ -140,26 +145,30 @@ private function declareQueue($name)
140145
{
141146
$name = $this->getQueueName($name);
142147

143-
// declare queue
144-
$this->channel->queue_declare(
145-
$name,
146-
$this->configQueue['passive'],
147-
$this->configQueue['durable'],
148-
$this->configQueue['exclusive'],
149-
$this->configQueue['auto_delete']
150-
);
151-
152-
// declare exchange
153-
$this->channel->exchange_declare(
154-
$name,
155-
$this->configExchange['type'],
156-
$this->configExchange['passive'],
157-
$this->configExchange['durable'],
158-
$this->configExchange['auto_delete']
159-
);
148+
if ($this->declareExchange) {
149+
// declare exchange
150+
$this->channel->exchange_declare(
151+
$name,
152+
$this->configExchange['type'],
153+
$this->configExchange['passive'],
154+
$this->configExchange['durable'],
155+
$this->configExchange['auto_delete']
156+
);
157+
}
160158

161-
// bind queue to the exchange
162-
$this->channel->queue_bind($name, $name, $name);
159+
if ($this->declareBindQueue) {
160+
// declare queue
161+
$this->channel->queue_declare(
162+
$name,
163+
$this->configQueue['passive'],
164+
$this->configQueue['durable'],
165+
$this->configQueue['exclusive'],
166+
$this->configQueue['auto_delete']
167+
);
168+
169+
// bind queue to the exchange
170+
$this->channel->queue_bind($name, $name, $name);
171+
}
163172
}
164173

165174
/**

src/config/rabbitmq.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* default configuration for laravel-queue-rabbitmq merged with project config to base key 'queue'
5+
* @see \MapleSyrupGroup\AMQPEvents\Providers\AMQPEventServiceProvider
6+
*/
7+
return [
8+
9+
'driver' => 'rabbitmq',
10+
11+
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
12+
'port' => env('RABBITMQ_PORT', 5672),
13+
14+
'vhost' => env('RABBITMQ_VHOST', '/'),
15+
'login' => env('RABBITMQ_LOGIN', 'guest'),
16+
'password' => env('RABBITMQ_PASSWORD', 'guest'),
17+
18+
'queue' => env('RABBITMQ_QUEUE'), // name of the default queue,
19+
20+
'exchange_declare' => env('RABBITMQ_EXCHANGE_DECLARE', true), // create the exchange if not exists
21+
'queue_declare_bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true), // create the queue if not exists and bind to the exchange
22+
23+
'queue_params' => [
24+
'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),
25+
'durable' => env('RABBITMQ_QUEUE_DURABLE', true),
26+
'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
27+
'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
28+
],
29+
'exchange_params' => [
30+
'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'), // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
31+
'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
32+
'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), // the exchange will survive server restarts
33+
'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
34+
],
35+
36+
];

0 commit comments

Comments
 (0)