-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.php
156 lines (130 loc) · 3.26 KB
/
Driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Bernard\Driver\Amqp;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Message\AMQPMessage;
final class Driver implements \Bernard\Driver
{
/**
* @var AbstractConnection
*/
private $connection;
/**
* @var AMQPChannel
*/
private $channel;
/**
* @var string
*/
private $exchange;
/**
* @var array
*/
private $defaultMessageProperties;
/**
* @param AbstractConnection $connection
* @param string $exchange
* @param array $defaultMessageProperties
*/
public function __construct(AbstractConnection $connection, $exchange, array $defaultMessageProperties = [])
{
$this->connection = $connection;
$this->exchange = $exchange;
$this->defaultMessageProperties = $defaultMessageProperties;
}
/**
* {@inheritdoc}
*/
public function listQueues()
{
return [];
}
/**
* {@inheritdoc}
*/
public function createQueue($queueName)
{
$channel = $this->getChannel();
$channel->exchange_declare($this->exchange, 'direct', false, true, false);
$channel->queue_declare($queueName, false, true, false, false);
$channel->queue_bind($queueName, $this->exchange, $queueName);
}
/**
* {@inheritdoc}
*/
public function countMessages($queueName)
{
list(, $messageCount) = $this->getChannel()->queue_declare($queueName, true);
return $messageCount;
}
/**
* {@inheritdoc}
*/
public function pushMessage($queueName, $message)
{
$amqpMessage = new AMQPMessage($message, $this->defaultMessageProperties);
$this->getChannel()->basic_publish($amqpMessage, $this->exchange, $queueName);
}
/**
* {@inheritdoc}
*/
public function popMessage($queueName, $duration = 5)
{
$runtime = microtime(true) + $duration;
while (microtime(true) < $runtime) {
$message = $this->getChannel()->basic_get($queueName);
if ($message) {
return [$message->body, $message->get('delivery_tag')];
}
// sleep for 10 ms to prevent hammering CPU
usleep(10000);
}
return [null, null];
}
/**
* {@inheritdoc}
*/
public function acknowledgeMessage($queueName, $receipt)
{
$this->getChannel()->basic_ack($receipt);
}
/**
* {@inheritdoc}
*/
public function peekQueue($queueName, $index = 0, $limit = 20)
{
return [];
}
/**
* {@inheritdoc}
*/
public function removeQueue($queueName)
{
$this->getChannel()->queue_delete($queueName);
}
/**
* {@inheritdoc}
*/
public function info()
{
return [];
}
public function __destruct()
{
if (null !== $this->channel) {
$this->channel->close();
}
}
/**
* Creates a channel or returns an already created one.
*
* @return AMQPChannel
*/
private function getChannel()
{
if (null === $this->channel) {
$this->channel = $this->connection->channel();
}
return $this->channel;
}
}