-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.php
68 lines (54 loc) · 1.55 KB
/
test.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
<?php
require 'vendor/autoload.php';
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class Queue
{
private string $subject = 'exchange_event';
private AMQPChannel $channel;
public function __construct()
{
$connection = new AMQPStreamConnection('rabbitmq', 5672, 'guest', 'guest');
$this->channel = $connection->channel();
}
public function listen(): void
{
}
public function publish($totalMsg = 1000): void
{
$i = 0;
while (true) {
$i++;
$data = $this->getJsonData();
$this->sendToRabbit($data);
if ($i % 1000 === 0) {
print $i . PHP_EOL;
}
if ($i % $totalMsg === 0) {
return;
}
}
}
private function getJsonData(): string
{
$data = [
'company_id' => PHP_INT_MAX,
'product_id' => PHP_INT_MAX,
'event_date' => (new \DateTimeImmutable())->getTimestamp(),
];
return json_encode($data, JSON_THROW_ON_ERROR);
}
private function sendToRabbit(string $data): void
{
$properties = [
// 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
];
$amqpMessage = new AMQPMessage($data, $properties);
$this->channel->basic_publish($amqpMessage, $this->subject, 'key');
}
}
$queue = new Queue();
echo "Send {$argv[1]} messages:" . PHP_EOL;
$queue->publish($argv[1]);
echo "Done" . PHP_EOL;