Last active
June 1, 2017 14:15
-
-
Save kevinmlong/439bb65f259c2d13ae2c55f03a416c1b to your computer and use it in GitHub Desktop.
AbstractCarrier Class for PHP Event Bus Using RabbitMQ
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 | |
abstract class AbstractCarrier { | |
protected $connection; | |
protected $channel; | |
protected $queueName; | |
public function __construct($queueName, $host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest') { | |
$this->queueName = $queueName; | |
$this->connection = new AMQPStreamConnection($host, $port, $username, $password); | |
$this->channel = $this->connection->channel(); | |
$this->channel->queue_declare($queueName, false, false, false, false); | |
} | |
/** | |
* This method is called by the script that implements the carrier. It is blocking. | |
* The call to basic_consume calls the 'receivedParcel' method which is implmented by the subclasses | |
*/ | |
public function waitForMail() { | |
$this->channel->basic_consume($this->queueName, $this->queueName, false,false,false,false, [$this, 'receivedParcel']); | |
while (count($this->channel->callbacks)) { | |
$this->channel->wait(); | |
} | |
$this->channel->close(); | |
$this->connection->close(); | |
} | |
/* Stub to be implemented by sub classes */ | |
protected function receivedParcel($msg){} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment