|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NeedleProject\LaravelRabbitMq; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
|
6
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
|
7
|
|
|
use PhpAmqpLib\Connection\AMQPConnectionConfig; |
|
8
|
|
|
use PhpAmqpLib\Connection\AMQPConnectionFactory; |
|
9
|
|
|
use PhpAmqpLib\Connection\AMQPSocketConnection; |
|
10
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AMQPConnection |
|
14
|
|
|
* |
|
15
|
|
|
* @package NeedleProject\LaravelRabbitMq |
|
16
|
|
|
* @author Adrian Tilita <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class AMQPConnection |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @const array Default connections parameters |
|
22
|
|
|
*/ |
|
23
|
|
|
const DEFAULTS = [ |
|
24
|
|
|
'hostname' => '127.0.0.1', |
|
25
|
|
|
'port' => 5672, |
|
26
|
|
|
'username' => 'guest', |
|
27
|
|
|
'password' => 'guest', |
|
28
|
|
|
'vhost' => '/', |
|
29
|
|
|
|
|
30
|
|
|
# whether the connection should be lazy |
|
31
|
|
|
'lazy' => true, |
|
32
|
|
|
|
|
33
|
|
|
'insist' => false, |
|
34
|
|
|
|
|
35
|
|
|
# More info about timeouts can be found on https://www.rabbitmq.com/networking.html |
|
36
|
|
|
'read_write_timeout' => 3, // default timeout for writing/reading (in seconds) |
|
37
|
|
|
'connect_timeout' => 3, |
|
38
|
|
|
'channel_rpc_timeout' => 0, |
|
39
|
|
|
'heartbeat' => 0, |
|
40
|
|
|
'keep_alive' => false, |
|
41
|
|
|
|
|
42
|
|
|
'connection_name' => '', |
|
43
|
|
|
|
|
44
|
|
|
'io_type' => AMQPConnectionConfig::IO_TYPE_STREAM, |
|
45
|
|
|
|
|
46
|
|
|
'secure' => false, |
|
47
|
|
|
'ssl_crypto_method' => STREAM_CRYPTO_METHOD_ANY_CLIENT, |
|
48
|
|
|
'ssl_verify' => false, |
|
49
|
|
|
'ssl_verify_name' => false, |
|
50
|
|
|
|
|
51
|
|
|
'locale' => 'en_US', |
|
52
|
|
|
|
|
53
|
|
|
'login_method' => AMQPConnectionConfig::AUTH_AMQPPLAIN, |
|
54
|
|
|
'login_response' => null, |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected array $connectionDetails = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected string $aliasName = ''; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var null|AbstractConnection |
|
69
|
|
|
*/ |
|
70
|
|
|
private ?AbstractConnection $connection = null; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var null|AMQPChannel |
|
74
|
|
|
*/ |
|
75
|
|
|
private ?AMQPChannel $channel = null; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $aliasName |
|
79
|
|
|
* @param array $connectionDetails |
|
80
|
|
|
* @return AMQPConnection |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function createConnection(string $aliasName, array $connectionDetails) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($diff = array_diff(array_keys($connectionDetails), array_keys(self::DEFAULTS))) { |
|
85
|
|
|
throw new \InvalidArgumentException( |
|
86
|
|
|
sprintf( |
|
87
|
|
|
"Cannot create connection %s, received unknown arguments: %s!", |
|
88
|
|
|
(string) $aliasName, |
|
89
|
|
|
implode(', ', $diff) |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return new static( |
|
95
|
|
|
$aliasName, |
|
96
|
|
|
array_merge(self::DEFAULTS, $connectionDetails) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* AMQPConnection constructor. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $aliasName |
|
104
|
|
|
* @param array $connectionDetails |
|
105
|
|
|
*/ |
|
106
|
|
|
public function __construct(string $aliasName, array $connectionDetails = []) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->aliasName = $aliasName; |
|
109
|
|
|
$this->connectionDetails = $connectionDetails; |
|
110
|
|
|
|
|
111
|
|
|
if (isset($connectionDetails['lazy']) && $connectionDetails['lazy'] === false) { |
|
112
|
|
|
// dummy call |
|
113
|
|
|
$this->getConnection(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return AbstractConnection |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getConnection(): AbstractConnection |
|
121
|
|
|
{ |
|
122
|
|
|
if (is_null($this->connection)) { |
|
123
|
|
|
$this->connection = $this->createConnectionByType(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->connection; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private function createConnectionByType(): AbstractConnection |
|
130
|
|
|
{ |
|
131
|
|
|
$config = new AMQPConnectionConfig(); |
|
132
|
|
|
|
|
133
|
|
|
$config->setHost($this->connectionDetails['hostname']); |
|
134
|
|
|
$config->setPort($this->connectionDetails['port']); |
|
135
|
|
|
$config->setUser($this->connectionDetails['username']); |
|
136
|
|
|
$config->setPassword($this->connectionDetails['password']); |
|
137
|
|
|
$config->setVhost($this->connectionDetails['vhost']); |
|
138
|
|
|
|
|
139
|
|
|
if (!empty($this->connectionDetails['io_type'])) { |
|
140
|
|
|
$config->setIoType($this->connectionDetails['io_type']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (!empty($this->connectionDetails['connection_name'])) { |
|
144
|
|
|
$config->setConnectionName($this->connectionDetails['connection_name']); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (isset($this->connectionDetails['connect_timeout'])) { |
|
148
|
|
|
$config->setConnectionTimeout((float)$this->connectionDetails['connect_timeout']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (isset($this->connectionDetails['read_write_timeout'])) { |
|
152
|
|
|
$config->setReadTimeout((float)$this->connectionDetails['read_write_timeout']); |
|
153
|
|
|
$config->setWriteTimeout((float)$this->connectionDetails['read_write_timeout']); |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (isset($this->connectionDetails['channel_rpc_timeout'])) { |
|
158
|
|
|
$config->setChannelRPCTimeout((float)$this->connectionDetails['channel_rpc_timeout']); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (isset($this->connectionDetails['keep_alive'])) { |
|
162
|
|
|
$config->setKeepalive((bool)$this->connectionDetails['keep_alive']); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (isset($this->connectionDetails['heartbeat'])) { |
|
166
|
|
|
$config->setHeartbeat((int)$this->connectionDetails['heartbeat']); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if (isset($this->connectionDetails['secure'])) { |
|
170
|
|
|
$config->setIsSecure((bool)$this->connectionDetails['secure']); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (isset($this->connectionDetails['ssl_crypto_method'])) { |
|
174
|
|
|
$config->setSslCryptoMethod($this->connectionDetails['ssl_crypto_method']); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (isset($this->connectionDetails['ssl_verify_name'])) { |
|
178
|
|
|
$config->setSslVerifyName((bool)$this->connectionDetails['ssl_verify_name']); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (isset($this->connectionDetails['ssl_verify'])) { |
|
182
|
|
|
$config->setSslVerify((bool)$this->connectionDetails['ssl_verify']); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (isset($this->connectionDetails['lazy'])) { |
|
186
|
|
|
$config->setIsLazy((bool)$this->connectionDetails['lazy']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (isset($this->connectionDetails['insist'])) { |
|
190
|
|
|
$config->setInsist((bool)$this->connectionDetails['insist']); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if (!empty($this->connectionDetails['locale'])) { |
|
194
|
|
|
$config->setLocale($this->connectionDetails['locale']); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (!empty($this->connectionDetails['login_response'])) { |
|
198
|
|
|
$config->setLoginResponse($this->connectionDetails['login_response']); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return AMQPConnectionFactory::create($config); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Reconnect |
|
206
|
|
|
*/ |
|
207
|
|
|
public function reconnect(): void |
|
208
|
|
|
{ |
|
209
|
|
|
$this->getConnection()->channel()->close(); |
|
210
|
|
|
$this->channel = null; |
|
211
|
|
|
$this->getConnection()->reconnect(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
public function getChannel(): ?AMQPChannel |
|
216
|
|
|
{ |
|
217
|
|
|
if (is_null($this->channel)) { |
|
218
|
|
|
$this->channel = $this->getConnection()->channel(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $this->channel; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Retrieve the connection alias name |
|
226
|
|
|
* |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
|
|
public function getAliasName(): string |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->aliasName; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|