1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the mucts.com. |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the MIT license that is bundled |
6
|
|
|
* with this source code in the file LICENSE. |
7
|
|
|
* |
8
|
|
|
* @version 1.0 |
9
|
|
|
* @author herry<[email protected]> |
10
|
|
|
* @copyright © 2020 MuCTS.com All Rights Reserved. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace MuCTS\Laravel\AMQP\Commands; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use ErrorException; |
17
|
|
|
use Exception; |
18
|
|
|
use Illuminate\Console\Command; |
19
|
|
|
use Illuminate\Support\Facades\Log; |
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use MuCTS\Laravel\AMQP\Facades\AMQP; |
22
|
|
|
use MuCTS\Laravel\AMQP\Message; |
23
|
|
|
use PhpAmqpLib\Exchange\AMQPExchangeType; |
24
|
|
|
|
25
|
|
|
abstract class AMQPCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* AMQPMessage Connection Name |
29
|
|
|
* |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
protected ?string $connection = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* AMQPExchange Name |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected string $exchange = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* AMQPExchange Type |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected string $exchangeType = AMQPExchangeType::TOPIC; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* AMQPQueue Name |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected string $queue = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Consumer identifier |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected string $consumerTag = ''; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* AMQPMessage Route Key |
60
|
|
|
* 路由键 |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected string $routeKey = ''; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Auto Ack |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
protected bool $autoAsk = false; |
71
|
|
|
|
72
|
|
|
public function handle() |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
AMQP::connection($this->connection) |
76
|
|
|
->setExchange($this->exchange) |
77
|
|
|
->setExchangeType($this->exchangeType) |
78
|
|
|
->setQueue($this->queue) |
79
|
|
|
->setConsumerTag($this->consumerTag) |
80
|
|
|
->setAutoAck($this->autoAsk) |
81
|
|
|
->setRouteKey($this->routeKey) |
82
|
|
|
->consume(function ($message) { |
83
|
|
|
/** @var Message $message */ |
84
|
|
|
return static::processMessage($message); |
85
|
|
|
}); |
86
|
|
|
} catch (InvalidArgumentException|ErrorException|Exception $exception) { |
87
|
|
|
Log::error('AMQPMessage consume error:' . $exception->getMessage()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Process Message |
93
|
|
|
* |
94
|
|
|
* @param Message $message |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
abstract protected function processMessage(Message $message); |
98
|
|
|
} |