|
| 1 | +<?php |
| 2 | +namespace PHPQueue\Backend; |
| 3 | + |
| 4 | +use FuseSource\Stomp\Stomp as FuseStomp; |
| 5 | + |
| 6 | +use PHPQueue\Exception\BackendException; |
| 7 | +use PHPQueue\Exception\JobNotFoundException; |
| 8 | +use PHPQueue\Interfaces\FifoQueueStore; |
| 9 | +use PHPQueue\Interfaces\KeyValueStore; |
| 10 | + |
| 11 | +/** |
| 12 | + * Wrap a STOMP queue |
| 13 | + * |
| 14 | + * This has been tested against the ActiveMQ and ApolloMQ implementations. |
| 15 | + * |
| 16 | + * @author awight@wikimedia.org |
| 17 | + */ |
| 18 | +class Stomp |
| 19 | + extends Base |
| 20 | + implements FifoQueueStore, KeyValueStore |
| 21 | +{ |
| 22 | + public $queue_name; |
| 23 | + public $uri; |
| 24 | + public $connection; |
| 25 | + public $merge_headers; |
| 26 | + public $read_timeout; |
| 27 | + public $ack = true; |
| 28 | + |
| 29 | + public function __construct($options=array()) |
| 30 | + { |
| 31 | + parent::__construct(); |
| 32 | + if (!empty($options['queue'])) { |
| 33 | + $this->queue_name = $options['queue']; |
| 34 | + } |
| 35 | + if (!empty($options['uri'])) { |
| 36 | + $this->uri = $options['uri']; |
| 37 | + } |
| 38 | + if (isset($options['merge_headers'])) { |
| 39 | + $this->merge_headers = (bool)$options['merge_headers']; |
| 40 | + } |
| 41 | + if (isset($options['read_timeout'])) { |
| 42 | + $this->read_timeout = (int)$options['read_timeout']; |
| 43 | + } |
| 44 | + if (isset($options['ack'])) { |
| 45 | + $this->ack = (bool)$options['ack']; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public function __destruct() |
| 50 | + { |
| 51 | + if ( $this->connection ) { |
| 52 | + $this->connection->disconnect(); |
| 53 | + $this->connection = null; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function connect() |
| 58 | + { |
| 59 | + $this->connection = new FuseStomp($this->uri); |
| 60 | + $this->connection->connect(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param array $data |
| 65 | + * @param array $properties Optional headers. |
| 66 | + * |
| 67 | + * @throws \PHPQueue\Exception\BackendException |
| 68 | + */ |
| 69 | + public function push($data=array(), $properties=array()) |
| 70 | + { |
| 71 | + $this->beforeAdd(); |
| 72 | + |
| 73 | + $body = json_encode($data); |
| 74 | + $result = $this->getConnection()->send($this->queue_name, $body, $properties); |
| 75 | + if (!$result) { |
| 76 | + throw new BackendException("Couldn't send a message!"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array|null |
| 82 | + * @throws \PHPQueue\Exception\BackendException |
| 83 | + */ |
| 84 | + public function pop() |
| 85 | + { |
| 86 | + return $this->readFrame(); |
| 87 | + } |
| 88 | + |
| 89 | + public function set($key, $data=array(), $properties=array()) |
| 90 | + { |
| 91 | + $properties['correlation-id'] = $key; |
| 92 | + $this->push($data, $properties); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @return array|null |
| 97 | + */ |
| 98 | + public function get($key) |
| 99 | + { |
| 100 | + $properties = array( |
| 101 | + 'ack' => 'client', |
| 102 | + 'selector' => "JMSCorrelationID='{$key}' OR JMSMessageID='{$key}'", |
| 103 | + ); |
| 104 | + return $this->readFrame($properties); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param array|null $properties Optional selectors. |
| 109 | + */ |
| 110 | + protected function readFrame($properties = null) |
| 111 | + { |
| 112 | + $this->beforeGet(); |
| 113 | + if ($properties === null) { |
| 114 | + $properties = array('ack' => 'client'); |
| 115 | + } |
| 116 | + $this->getConnection()->subscribe($this->queue_name, $properties); |
| 117 | + if ($this->read_timeout) { |
| 118 | + $this->getConnection()->setReadTimeout($this->read_timeout); |
| 119 | + } |
| 120 | + $response = $this->getConnection()->readFrame(); |
| 121 | + $this->afterGet(); |
| 122 | + |
| 123 | + if ($response && $this->ack) { |
| 124 | + $this->getConnection()->ack($response); |
| 125 | + } |
| 126 | + |
| 127 | + $this->getConnection()->unsubscribe($this->queue_name); |
| 128 | + |
| 129 | + if (!$response) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + $message = json_decode($response->body, true); |
| 134 | + if ($this->merge_headers) { |
| 135 | + $message = array_merge($response->headers, $message); |
| 136 | + } |
| 137 | + return $message; |
| 138 | + } |
| 139 | + |
| 140 | + public function clear($key=null) |
| 141 | + { |
| 142 | + throw new Exception('Not implemented.'); |
| 143 | + } |
| 144 | +} |
0 commit comments