1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Transport\Subscriber; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPChannel; |
6
|
|
|
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPConnectionInterface; |
7
|
|
|
use Cmobi\RabbitmqBundle\Connection\ConnectionManager; |
8
|
|
|
use Cmobi\RabbitmqBundle\Queue\CmobiAMQPMessage; |
9
|
|
|
use Cmobi\RabbitmqBundle\Queue\QueueProducerInterface; |
10
|
|
|
|
11
|
|
|
class Publisher implements QueueProducerInterface |
12
|
|
|
{ |
13
|
|
|
private $connectionManager; |
14
|
|
|
private $channel; |
15
|
|
|
private $fromName; |
16
|
|
|
private $queueName; |
17
|
|
|
private $exchange; |
18
|
|
|
private $exchangeType; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
$exchange, |
22
|
|
|
$exchangeType = ExchangeType::FANOUT, |
23
|
|
|
ConnectionManager $manager, |
24
|
|
|
$fromName = '', |
25
|
|
|
$queueName = '' |
26
|
|
|
) { |
27
|
|
|
$this->exchange = $exchange; |
28
|
|
|
$this->exchangeType = $exchangeType; |
29
|
|
|
$this->queueName = $queueName; |
30
|
|
|
$this->fromName = $fromName; |
31
|
|
|
$this->connectionManager = $manager; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $data |
36
|
|
|
* @param $expire |
37
|
|
|
* @param $priority |
38
|
|
|
*/ |
39
|
|
|
public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW) |
40
|
|
|
{ |
41
|
|
|
$this->refreshChannel(); |
42
|
|
|
$queueBag = new SubscriberQueueBag($this->getExchange(), $this->getExchangeType(), $this->getQueueName()); |
43
|
|
|
$this->getChannel()->exchangeDeclare($queueBag->getExchangeDeclare()); |
|
|
|
|
44
|
|
|
$msg = new CmobiAMQPMessage((string) $data); |
45
|
|
|
$this->getChannel()->basic_publish($msg, $queueBag->getExchange()); |
46
|
|
|
|
47
|
|
|
$this->getChannel()->close(); |
48
|
|
|
$this->connectionManager->getConnection()->close(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return CmobiAMQPChannel |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function refreshChannel() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
/** @var CmobiAMQPConnectionInterface $connection */ |
57
|
|
|
$connection = $this->connectionManager->getConnection(); |
58
|
|
|
|
59
|
|
|
if (!$connection->isConnected()) { |
60
|
|
|
$connection->reconnect(); |
61
|
|
|
} |
62
|
|
|
$this->channel = $connection->channel(); |
63
|
|
|
|
64
|
|
|
return $this->channel; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return CmobiAMQPChannel |
69
|
|
|
*/ |
70
|
|
|
public function getChannel() |
71
|
|
|
{ |
72
|
|
|
return $this->channel; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getQueueName() |
79
|
|
|
{ |
80
|
|
|
return $this->queueName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return caller name. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getFromName() |
89
|
|
|
{ |
90
|
|
|
return $this->fromName; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getExchange() |
97
|
|
|
{ |
98
|
|
|
return $this->exchange; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getExchangeType() |
105
|
|
|
{ |
106
|
|
|
return $this->exchangeType; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|