Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. <?php
  2. abstract class AbstractCarrier {
  3.  
  4. protected $connection;
  5. protected $channel;
  6. protected $queueName;
  7.  
  8. public function __construct($queueName, $host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest') {
  9.  
  10. $this->queueName = $queueName;
  11.  
  12. $this->connection = new AMQPStreamConnection($host, $port, $username, $password);
  13. $this->channel = $this->connection->channel();
  14. $this->channel->queue_declare($queueName, false, false, false, false);
  15.  
  16. }
  17.  
  18. /**
  19. * This method is called by the script that implements the carrier. It is blocking.
  20. * The call to basic_consume calls the 'receivedParcel' method which is implmented by the subclasses
  21. */
  22. public function waitForMail() {
  23. $this->channel->basic_consume($this->queueName, $this->queueName, false,false,false,false, [$this, 'receivedParcel']);
  24.  
  25. while (count($this->channel->callbacks)) {
  26. $this->channel->wait();
  27. }
  28. $this->channel->close();
  29. $this->connection->close();
  30. }
  31.  
  32. /* Stub to be implemented by sub classes */
  33. protected function receivedParcel($msg){}
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement