-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed96e9a
commit 759a404
Showing
16 changed files
with
313 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 | ||
* | ||
* This file is part of the milkmeowo/laravel-mns. | ||
* | ||
* (c) Milkmeowo <[email protected]> | ||
* @link: https://github.com/milkmeowo/laravel-queue-aliyun-mns | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Milkmeowo\LaravelMns\Adaptors; | ||
|
||
use AliyunMNS\AsyncCallback; | ||
|
@@ -27,7 +39,7 @@ | |
use AliyunMNS\Responses\SetQueueAttributeResponse; | ||
|
||
/** | ||
* Class MNSAdapter | ||
* Class MNSAdapter. | ||
* | ||
* @method string getQueueName() | ||
* @method SetQueueAttributeResponse setAttribute(QueueAttributes $attributes) | ||
|
@@ -61,22 +73,23 @@ class MnsAdapter | |
*/ | ||
const ADAPTER_TO_ALIYUN_MNS_SDK_VERSION = '1.3.5@2017-06-06'; | ||
/** | ||
* Aliyun MNS Client | ||
* Aliyun MNS Client. | ||
* | ||
* @var MnsClient $client | ||
* @var MnsClient | ||
*/ | ||
private $client; | ||
/** | ||
* Aliyun MNS SDK Queue. | ||
* | ||
* @var Queue $queue | ||
* @var Queue | ||
*/ | ||
private $queue; | ||
|
||
/** | ||
* MnsAdapter constructor. | ||
* | ||
* @param MnsClient $client | ||
* @param string $queue | ||
* @param string $queue | ||
*/ | ||
public function __construct(MnsClient $client, string $queue) | ||
{ | ||
|
@@ -111,11 +124,12 @@ public function useQueue($queue) | |
if (null != $queue) { | ||
$this->queue = $this->client->getQueueRef($queue); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* 创建队列 | ||
* 创建队列. | ||
* | ||
* @param string $queueName 队列名 | ||
*/ | ||
|
@@ -124,16 +138,17 @@ public function createQueue($queueName) | |
try { | ||
$request = new CreateQueueRequest($queueName); | ||
$response = $this->client->createQueue($request); | ||
|
||
return $response->isSucceed(); | ||
} catch (MnsException $e) { | ||
} | ||
} | ||
|
||
/** | ||
* 异步创建队列 | ||
* 异步创建队列. | ||
* | ||
* @param string $queueName 队列名 | ||
* @param AsyncCallback|null $callback 异步回调 | ||
* @param string $queueName 队列名 | ||
* @param AsyncCallback|null $callback 异步回调 | ||
*/ | ||
public function createQueueAsync($queueName, AsyncCallback $callback = null) | ||
{ | ||
|
@@ -145,64 +160,66 @@ public function createQueueAsync($queueName, AsyncCallback $callback = null) | |
} | ||
|
||
/** | ||
* 获取队列列表 | ||
* 获取队列列表. | ||
* | ||
* @param null $retNum 单次请求结果的最大返回个数,可以取1-1000范围内的整数值,默认值为1000。 | ||
* @param null $prefix 按照该前缀开头的 queueName 进行查找。 | ||
* @param null $marker 请求下一个分页的开始位置,一般从上次分页结果返回的NextMarker获取。 | ||
* | ||
* @return \AliyunMNS\Responses\ListQueueResponse | ||
*/ | ||
public function listQueue($retNum = null, $prefix = null, $marker = null) | ||
{ | ||
try { | ||
$request = new ListQueueRequest($retNum, $prefix, $marker); | ||
|
||
return $this->client->listQueue($request); | ||
} catch (MnsException $e) { | ||
|
||
} | ||
} | ||
|
||
/** | ||
* 获取队列列表 | ||
* 获取队列列表. | ||
* | ||
* @param int $retNum 单次请求结果的最大返回个数,可以取1-1000范围内的整数值,默认值为1000。 | ||
* @param string $prefix 按照该前缀开头的 queueName 进行查找。 | ||
* @param string $marker 请求下一个分页的开始位置,一般从上次分页结果返回的NextMarker获取。 | ||
* @param AsyncCallback|null $callback | ||
* | ||
* @param int $retNum 单次请求结果的最大返回个数,可以取1-1000范围内的整数值,默认值为1000。 | ||
* @param string $prefix 按照该前缀开头的 queueName 进行查找。 | ||
* @param string $marker 请求下一个分页的开始位置,一般从上次分页结果返回的NextMarker获取。 | ||
* @param AsyncCallback|NULL $callback | ||
* @return mixed | ||
*/ | ||
public function listQueueAsync($retNum = NULL, $prefix = NULL, $marker = NULL, AsyncCallback $callback = NULL) | ||
public function listQueueAsync($retNum = null, $prefix = null, $marker = null, AsyncCallback $callback = null) | ||
{ | ||
try { | ||
$request = new ListQueueRequest($retNum, $prefix, $marker); | ||
|
||
return $this->client->listQueueAsync($request, $callback); | ||
} catch (MnsException $e) { | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* 删除队列 | ||
* 删除队列. | ||
* | ||
* @param string $queueName 队列名 | ||
*/ | ||
public function deleteQueue($queueName) | ||
{ | ||
try { | ||
$response = $this->client->deleteQueue($queueName); | ||
|
||
return $response->isSucceed(); | ||
} catch (MnsException $e) { | ||
} | ||
} | ||
|
||
/** | ||
* 异步删除队列 | ||
* 异步删除队列. | ||
* | ||
* @param string $queueName 队列名 | ||
* @param AsyncCallback|NULL $callback | ||
* @param string $queueName 队列名 | ||
* @param AsyncCallback|null $callback | ||
*/ | ||
public function deleteQueueAsync($queueName, AsyncCallback $callback = NULL) | ||
public function deleteQueueAsync($queueName, AsyncCallback $callback = null) | ||
{ | ||
try { | ||
$this->client->deleteQueueAsync($queueName, $callback); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 | ||
* | ||
* This file is part of the milkmeowo/laravel-mns. | ||
* | ||
* (c) Milkmeowo <[email protected]> | ||
* @link: https://github.com/milkmeowo/laravel-queue-aliyun-mns | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Milkmeowo\LaravelMns\Connectors; | ||
|
||
use AliyunMNS\Client as MnsClient; | ||
|
@@ -10,9 +22,10 @@ | |
class MnsConnector implements ConnectorInterface | ||
{ | ||
/** | ||
* 接口方法,连接器 | ||
* 接口方法,连接器. | ||
* | ||
* @param array $config | ||
* | ||
* @return \Illuminate\Contracts\Queue\Queue|MnsQueue | ||
*/ | ||
public function connect(array $config) | ||
|
@@ -23,23 +36,24 @@ public function connect(array $config) | |
} | ||
|
||
/** | ||
* Mns 适配器 | ||
* Mns 适配器. | ||
* | ||
* @param array $config | ||
* | ||
* @return MnsAdapter | ||
*/ | ||
public function getAdapter(array $config) | ||
{ | ||
$client = $this->getClient($config); | ||
|
||
return new MnsAdapter($client, $config['queue']); | ||
|
||
} | ||
|
||
/** | ||
* Mns Client | ||
* Mns Client. | ||
* | ||
* @param array $config | ||
* | ||
* @return MnsClient | ||
*/ | ||
public function getClient(array $config) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
<?php | ||
|
||
namespace Milkmeowo\LaravelMns\Console; | ||
/* | ||
* Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 | ||
* | ||
* This file is part of the milkmeowo/laravel-mns. | ||
* | ||
* (c) Milkmeowo <[email protected]> | ||
* @link: https://github.com/milkmeowo/laravel-queue-aliyun-mns | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Milkmeowo\LaravelMns\Console; | ||
|
||
use AliyunMNS\Client; | ||
use AliyunMNS\Exception\MnsException; | ||
|
@@ -36,14 +47,15 @@ public function handle() | |
if (!$queue) { | ||
$queue = $this->ask('请输入队列名称'); | ||
} | ||
|
||
try { | ||
$client = new Client($config['endpoint'], $config['key'], $config['secret']); | ||
$request = new CreateQueueRequest($queue); | ||
$client->createQueue($request); | ||
$this->info('队列创建成功'); | ||
$this->alert($queue); | ||
} catch (MnsException $e) { | ||
$this->error('队列创建失败:' . $e); | ||
$this->error('队列创建失败:'.$e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 | ||
* | ||
* This file is part of the milkmeowo/laravel-mns. | ||
* | ||
* (c) Milkmeowo <[email protected]> | ||
* @link: https://github.com/milkmeowo/laravel-queue-aliyun-mns | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Milkmeowo\LaravelMns\Console; | ||
|
||
use AliyunMNS\Client; | ||
|
@@ -34,13 +46,14 @@ public function handle() | |
if (!$queue) { | ||
$queue = $this->ask('请输入队列名称'); | ||
} | ||
|
||
try { | ||
$client = new Client($config['endpoint'], $config['key'], $config['secret']); | ||
$client->deleteQueue($queue); | ||
$this->info('队列删除成功'); | ||
$this->alert($queue); | ||
} catch (MnsException $e) { | ||
$this->error('队列删除失败:' . $e); | ||
$this->error('队列删除失败:'.$e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* Laravel-Mns -- 阿里云消息队列(MNS)的 Laravel 适配。 | ||
* | ||
* This file is part of the milkmeowo/laravel-mns. | ||
* | ||
* (c) Milkmeowo <[email protected]> | ||
* @link: https://github.com/milkmeowo/laravel-queue-aliyun-mns | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Milkmeowo\LaravelMns\Console; | ||
|
||
use AliyunMNS\Client; | ||
|
@@ -41,6 +53,7 @@ public function handle() | |
$hasMessage = true; | ||
while ($hasMessage) { | ||
$this->info('拉取信息中...'); | ||
|
||
try { | ||
$response = $queue->batchPeekMessage(15); | ||
if ($response->getMessages()) { | ||
|
@@ -55,15 +68,15 @@ public function handle() | |
$response = $queue->batchReceiveMessage(new BatchReceiveMessageRequest(15, 30)); | ||
$handles = []; | ||
/** | ||
* @var Message $message | ||
* @var Message | ||
*/ | ||
foreach ($response->getMessages() as $message) { | ||
$handles[] = $message->getReceiptHandle(); | ||
} | ||
$response = $queue->batchDeleteMessage($handles); | ||
if ($response->isSucceed()) { | ||
foreach ($handles as $handle) { | ||
$this->info(sprintf("信息: %s 删除成功", $handle)); | ||
$this->info(sprintf('信息: %s 删除成功', $handle)); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.