From e90d4fd3257defd48f7fbc4e37442af8f0b71798 Mon Sep 17 00:00:00 2001 From: Odd-Arne Johansen Date: Wed, 30 Jan 2019 13:20:29 +0100 Subject: [PATCH] Make vhost configurable #39 --- README.md | 5 +++-- src/BowlerServiceProvider.php | 3 ++- src/Connection.php | 14 ++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d20e80e..e1f2a3f 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Tools like the Rabbitmq *[Management](https://www.rabbitmq.com/management.html)* ### Configuration -In order to configure rabbitmq host, port, username, password and configure the timeout values too, add the following inside the connections array in config/queue.php file: +In order to configure rabbitmq host, port, username, password and configure the timeout and vhost values too, add the following inside the connections array in config/queue.php file: ```php 'rabbitmq' => [ @@ -45,10 +45,11 @@ In order to configure rabbitmq host, port, username, password and configure the 'connection_timeout' => 30, 'read_write_timeout' => 30, 'heartbeat' => 15, + 'vhost' => '/', ], ``` -The default value for `connection_timeout` and `read_write_timeout` is set to 30 (seconds) and `heartbeat` is set to 15 (seconds). +The default value for `connection_timeout` and `read_write_timeout` is set to 30 (seconds) and `heartbeat` is set to 15 (seconds). The `vhost` value defaults to '/'. And register the service provider by adding `Vinelab\Bowler\BowlerServiceProvider::class` to the providers array in `config/app`. diff --git a/src/BowlerServiceProvider.php b/src/BowlerServiceProvider.php index 4a18ba9..1d261e3 100644 --- a/src/BowlerServiceProvider.php +++ b/src/BowlerServiceProvider.php @@ -39,8 +39,9 @@ public function register() $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30; $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30; $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15; + $rbmqVhost = config('queue.connections.rabbitmq.vhost', '/'); - return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat); + return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat, $rbmqVhost); }); $this->app->bind( diff --git a/src/Connection.php b/src/Connection.php index b4caf4b..093ae37 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -79,6 +79,12 @@ class Connection */ private $heartbeat = 15; + /** + * RabbitMQ vhost. + * @var string + */ + private $vhost = '/'; + /** * @param string $host the ip of the rabbitmq server, default: localhost * @param int $port. default: 5672 @@ -88,7 +94,7 @@ class Connection * @param int $readWriteTimeout, default: 30 * @param int $heartbeat, default: 15 */ - public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest', $connectionTimeout = 30, $readWriteTimeout = 30, $heartbeat = 15) + public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest', $connectionTimeout = 30, $readWriteTimeout = 30, $heartbeat = 15, $vhost = '/') { $this->host = $host; $this->port = $port; @@ -97,13 +103,13 @@ public function __construct($host = 'localhost', $port = 5672, $username = 'gues $this->connectionTimeout = $connectionTimeout; $this->readWriteTimeout = $readWriteTimeout; $this->heartbeat = $heartbeat; + $this->vhost = $vhost; - $this->initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat); + $this->initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost); } - protected function initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost = '/',$insist = false, $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', $context = null, $keepalive = false) + protected function initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost = '/', $insist = false, $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', $context = null, $keepalive = false) { - $vhost = '/'; $insist = false; $login_method = 'AMQPLAIN'; $login_response = null;