-
Notifications
You must be signed in to change notification settings - Fork 1
/
RabbitPhpAmqpLib.php
157 lines (119 loc) · 4.39 KB
/
RabbitPhpAmqpLib.php
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
<?php
namespace cperdana\rabbitmq;
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Connection\AMQPStreamConnection;
// If this is enabled you can see AMQP output on the CLI
defined('AMQP_DEBUG') or define('AMQP_DEBUG', false);
/**
* Class Rabbit
* @package cperdana\rabbitmq
*/
class RabbitPhpAmqpLib
{
/**
* @var string Server IP
*/
public $host = '127.0.0.1';
/**
* @var int AMQP port
*/
public $port = '5672';
/**
* @var string Login
*/
public $user = 'guest';
/**
* @var string Password
*/
public $pass = 'guest';
/**
* @var string Virtual host
*/
public $vhost = '/';
public $debug = false;
/**
* @var string ExchangeName to Send
*/
public $sendExchangeName = 'exchange_name';
/**
* @var string Exchange type to Send
*/
public $sendExchangeType = 'exchange_type'; // fanout/direct
/**
* @var string Queue name to send (to be bind with $sendExchangeName)
*/
public $sendQueueName = 'send_queue_name';
/**
*@var string queue name to Read
*/
public $readQueueName = 'read_queue_name';
private $connection;
public function send($themsg)
{
$channel = $this->getChannel('send');
$messageBody = $themsg;
$message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
$channel->basic_publish($message, $this->sendExchangeName);
$channel->close();
$this->connection->close();
}
public function sendAck($message){
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
// Send a message with the string "quit" to cancel the consumer.
if ($message->body === 'quit') {
$message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']);
}
}
private function getChannel($forReadOrSend){
$this->connection = new AMQPStreamConnection(
$this->host, $this->port, $this->user, $this->pass, $this->vhost);
$channel = $this->connection->channel();
if ($forReadOrSend == 'read'){
// $this->readQueue = $this->readQueueName;
$channel->queue_declare($this->readQueueName, false, true, false, false);
}else if($forReadOrSend == 'send'){
// $this->exchange = $this->sendExchangeName;
// $exchangeType = $this->sendExchangeType;
// $this->sendQueue = $this->sendQueueName;
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($this->sendQueueName, false, true, false, false);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$channel->exchange_declare($this->sendExchangeName, $this->sendExchangeType, false, true, false);
$channel->queue_bind($this->sendQueueName, $this->sendExchangeName);
}
return $channel;
}
public function read($callback)
{
$channel = $this->getChannel('read');
/*
queue: Queue from where to get the messages
consumer_tag: Consumer identifier
no_local: Don't receive messages published by this consumer.
no_ack: Tells the server if the consumer will acknowledge the messages.
exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
nowait:
callback: A PHP Callback
*/
$consumerTag = 'consumer';
$channel->basic_consume($this->readQueueName, $consumerTag, false, false, false, false, $callback);
// Loop as long as the channel has callbacks registered
while (count($channel->callbacks)) {
$channel->wait();
}
}
}