Queue.php
14.5 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
namespace AliyunMNS;
use AliyunMNS\Http\HttpClient;
use AliyunMNS\AsyncCallback;
use AliyunMNS\Model\QueueAttributes;
use AliyunMNS\Requests\SetQueueAttributeRequest;
use AliyunMNS\Responses\SetQueueAttributeResponse;
use AliyunMNS\Requests\GetQueueAttributeRequest;
use AliyunMNS\Responses\GetQueueAttributeResponse;
use AliyunMNS\Requests\SendMessageRequest;
use AliyunMNS\Responses\SendMessageResponse;
use AliyunMNS\Requests\PeekMessageRequest;
use AliyunMNS\Responses\PeekMessageResponse;
use AliyunMNS\Requests\ReceiveMessageRequest;
use AliyunMNS\Responses\ReceiveMessageResponse;
use AliyunMNS\Requests\DeleteMessageRequest;
use AliyunMNS\Responses\DeleteMessageResponse;
use AliyunMNS\Requests\ChangeMessageVisibilityRequest;
use AliyunMNS\Responses\ChangeMessageVisibilityResponse;
use AliyunMNS\Requests\BatchSendMessageRequest;
use AliyunMNS\Responses\BatchSendMessageResponse;
use AliyunMNS\Requests\BatchReceiveMessageRequest;
use AliyunMNS\Responses\BatchReceiveMessageResponse;
use AliyunMNS\Requests\BatchPeekMessageRequest;
use AliyunMNS\Responses\BatchPeekMessageResponse;
use AliyunMNS\Requests\BatchDeleteMessageRequest;
use AliyunMNS\Responses\BatchDeleteMessageResponse;
class Queue
{
private $queueName;
private $client;
// boolean, whether the message body will be encoded in base64
private $base64;
public function __construct(HttpClient $client, $queueName, $base64 = TRUE)
{
$this->queueName = $queueName;
$this->client = $client;
$this->base64 = $base64;
}
public function setBase64($base64)
{
$this->base64 = $base64;
}
public function isBase64()
{
return ($this->base64 == TRUE);
}
public function getQueueName()
{
return $this->queueName;
}
/**
* Set the QueueAttributes, detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&queue_operation
*
* @param QueueAttributes $attributes: the QueueAttributes to set
*
* @return SetQueueAttributeResponse: the response
*
* @throws QueueNotExistException if queue does not exist
* @throws InvalidArgumentException if any argument value is invalid
* @throws MnsException if any other exception happends
*/
public function setAttribute(QueueAttributes $attributes)
{
$request = new SetQueueAttributeRequest($this->queueName, $attributes);
$response = new SetQueueAttributeResponse();
return $this->client->sendRequest($request, $response);
}
public function setAttributeAsync(QueueAttributes $attributes,
AsyncCallback $callback = NULL)
{
$request = new SetQueueAttributeRequest($this->queueName, $attributes);
$response = new SetQueueAttributeResponse();
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* Get the QueueAttributes, detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&queue_operation
*
* @return GetQueueAttributeResponse: containing the attributes
*
* @throws QueueNotExistException if queue does not exist
* @throws MnsException if any other exception happends
*/
public function getAttribute()
{
$request = new GetQueueAttributeRequest($this->queueName);
$response = new GetQueueAttributeResponse();
return $this->client->sendRequest($request, $response);
}
public function getAttributeAsync(AsyncCallback $callback = NULL)
{
$request = new GetQueueAttributeRequest($this->queueName);
$response = new GetQueueAttributeResponse();
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* SendMessage, the messageBody will be automatically encoded in base64
* If you do not need the message body to be encoded in Base64,
* please specify the $base64 = FALSE in Queue
*
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param SendMessageRequest: containing the message body and properties
*
* @return SendMessageResponse: containing the messageId and bodyMD5
*
* @throws QueueNotExistException if queue does not exist
* @throws InvalidArgumentException if any argument value is invalid
* @throws MalformedXMLException if any error in xml
* @throws MnsException if any other exception happends
*/
public function sendMessage(SendMessageRequest $request)
{
$request->setQueueName($this->queueName);
$request->setBase64($this->base64);
$response = new SendMessageResponse();
return $this->client->sendRequest($request, $response);
}
public function sendMessageAsync(SendMessageRequest $request,
AsyncCallback $callback = NULL)
{
$request->setQueueName($this->queueName);
$request->setBase64($this->base64);
$response = new SendMessageResponse();
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* PeekMessage, the messageBody will be automatically decoded as base64 if the $base64 in Queue is TRUE
*
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @return PeekMessageResponse: containing the messageBody and properties
*
* @throws QueueNotExistException if queue does not exist
* @throws MessageNotExistException if no message exists in the queue
* @throws MnsException if any other exception happends
*/
public function peekMessage()
{
$request = new PeekMessageRequest($this->queueName);
$response = new PeekMessageResponse($this->base64);
return $this->client->sendRequest($request, $response);
}
public function peekMessageAsync(AsyncCallback $callback = NULL)
{
$request = new PeekMessageRequest($this->queueName);
$response = new PeekMessageResponse($this->base64);
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* ReceiveMessage, the messageBody will be automatically decoded as base64 if $base64 = TRUE in Queue
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param waitSeconds: the long polling waitseconds
*
* @return ReceiveMessageResponse: containing the messageBody and properties
* the response is same as PeekMessageResponse,
* except that the receiptHandle is also returned in receiveMessage
*
* @throws QueueNotExistException if queue does not exist
* @throws MessageNotExistException if no message exists in the queue
* @throws MnsException if any other exception happends
*/
public function receiveMessage($waitSeconds = NULL)
{
$request = new ReceiveMessageRequest($this->queueName, $waitSeconds);
$response = new ReceiveMessageResponse($this->base64);
return $this->client->sendRequest($request, $response);
}
public function receiveMessageAsync(AsyncCallback $callback = NULL)
{
$request = new ReceiveMessageRequest($this->queueName);
$response = new ReceiveMessageResponse($this->base64);
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* DeleteMessage
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param $receiptHandle: the receiptHandle returned from receiveMessage
*
* @return ReceiveMessageResponse
*
* @throws QueueNotExistException if queue does not exist
* @throws InvalidArgumentException if the argument is invalid
* @throws ReceiptHandleErrorException if the $receiptHandle is invalid
* @throws MnsException if any other exception happends
*/
public function deleteMessage($receiptHandle)
{
$request = new DeleteMessageRequest($this->queueName, $receiptHandle);
$response = new DeleteMessageResponse();
return $this->client->sendRequest($request, $response);
}
public function deleteMessageAsync($receiptHandle,
AsyncCallback $callback = NULL)
{
$request = new DeleteMessageRequest($this->queueName, $receiptHandle);
$response = new DeleteMessageResponse();
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* ChangeMessageVisibility, set the nextVisibleTime for the message
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param $receiptHandle: the receiptHandle returned from receiveMessage
*
* @return ChangeMessageVisibilityResponse
*
* @throws QueueNotExistException if queue does not exist
* @throws MessageNotExistException if the message does not exist
* @throws InvalidArgumentException if the argument is invalid
* @throws ReceiptHandleErrorException if the $receiptHandle is invalid
* @throws MnsException if any other exception happends
*/
public function changeMessageVisibility($receiptHandle, $visibilityTimeout)
{
$request = new ChangeMessageVisibilityRequest($this->queueName, $receiptHandle, $visibilityTimeout);
$response = new ChangeMessageVisibilityResponse();
return $this->client->sendRequest($request, $response);
}
/**
* BatchSendMessage, message body will be automatically encoded in base64
* If you do not need the message body to be encoded in Base64,
* please specify the $base64 = FALSE in Queue
*
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param BatchSendMessageRequest:
* the requests containing an array of SendMessageRequestItems
*
* @return BatchSendMessageResponse
*
* @throws QueueNotExistException if queue does not exist
* @throws MalformedXMLException if any error in the xml
* @throws InvalidArgumentException if the argument is invalid
* @throws BatchSendFailException if some messages are not sent
* @throws MnsException if any other exception happends
*/
public function batchSendMessage(BatchSendMessageRequest $request)
{
$request->setQueueName($this->queueName);
$request->setBase64($this->base64);
$response = new BatchSendMessageResponse();
return $this->client->sendRequest($request, $response);
}
public function batchSendMessageAsync(BatchSendMessageRequest $request,
AsyncCallback $callback = NULL)
{
$request->setQueueName($this->queueName);
$request->setBase64($this->base64);
$response = new BatchSendMessageResponse();
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* BatchReceiveMessage, message body will be automatically decoded as base64 if $base64 = TRUE in Queue
*
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param BatchReceiveMessageRequest:
* containing numOfMessages and waitSeconds
*
* @return BatchReceiveMessageResponse:
* the received messages
*
* @throws QueueNotExistException if queue does not exist
* @throws MessageNotExistException if no message exists
* @throws MnsException if any other exception happends
*/
public function batchReceiveMessage(BatchReceiveMessageRequest $request)
{
$request->setQueueName($this->queueName);
$response = new BatchReceiveMessageResponse($this->base64);
return $this->client->sendRequest($request, $response);
}
public function batchReceiveMessageAsync(BatchReceiveMessageRequest $request, AsyncCallback $callback = NULL)
{
$request->setQueueName($this->queueName);
$response = new BatchReceiveMessageResponse($this->base64);
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* BatchPeekMessage, message body will be automatically decoded as base64 is $base64 = TRUE in Queue
*
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param BatchPeekMessageRequest:
* containing numOfMessages and waitSeconds
*
* @return BatchPeekMessageResponse:
* the received messages
*
* @throws QueueNotExistException if queue does not exist
* @throws MessageNotExistException if no message exists
* @throws MnsException if any other exception happends
*/
public function batchPeekMessage($numOfMessages)
{
$request = new BatchPeekMessageRequest($this->queueName, $numOfMessages);
$response = new BatchPeekMessageResponse($this->base64);
return $this->client->sendRequest($request, $response);
}
public function batchPeekMessageAsync($numOfMessages, AsyncCallback $callback = NULL)
{
$request = new BatchPeekMessageRequest($this->queueName, $numOfMessages);
$response = new BatchPeekMessageResponse($this->base64);
return $this->client->sendRequestAsync($request, $response, $callback);
}
/**
* BatchDeleteMessage
* detail API sepcs:
* https://docs.aliyun.com/?spm=#/pub/mns/api_reference/api_spec&message_operation
*
* @param $receiptHandles:
* array of $receiptHandle, which is got from receiveMessage
*
* @return BatchDeleteMessageResponse
*
* @throws QueueNotExistException if queue does not exist
* @throws ReceiptHandleErrorException if the receiptHandle is invalid
* @throws InvalidArgumentException if the argument is invalid
* @throws BatchDeleteFailException if any message not deleted
* @throws MnsException if any other exception happends
*/
public function batchDeleteMessage($receiptHandles)
{
$request = new BatchDeleteMessageRequest($this->queueName, $receiptHandles);
$response = new BatchDeleteMessageResponse();
return $this->client->sendRequest($request, $response);
}
public function batchDeleteMessageAsync($receiptHandles, AsyncCallback $callback = NULL)
{
$request = new BatchDeleteMessageRequest($this->queueName, $receiptHandles);
$response = new BatchDeleteMessageResponse();
return $this->client->sendRequestAsync($request, $response, $callback);
}
}
?>