Last active
January 12, 2021 12:47
-
-
Save IgorDePaula/699b92ee5431ea5789b8f12b32ae914c to your computer and use it in GitHub Desktop.
Rabbitmq service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use PhpAmqpLib\Connection\AMQPStreamConnection; | |
use PhpAmqpLib\Message\AMQPMessage; | |
class Rabbitmq | |
{ | |
private $channel, $connection, $queueName = null; | |
public function __construct($queueName) | |
{ | |
$this->queueName = $queueName; | |
$this->connection = new AMQPStreamConnection(env('QUEUE_HOST'), env('QUEUE_PORT'), env('QUEUE_USER'), env('QUEUE_PASS')); | |
$this->channel = $this->connection->channel(); | |
$this->channel->queue_declare($queueName, false, true, false, false); | |
} | |
public function sendMessage($message = null) | |
{ | |
$msg = new AMQPMessage($message); | |
$this->channel->basic_publish($msg, '', $this->queueName); | |
} | |
public function consume(callable $callback) | |
{ | |
$this->channel->basic_consume($this->queueName, '', false, true, false, false, $callback); | |
while ($this->channel->is_consuming()) { | |
$this->channel->wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment