-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer_auto_commit.php
More file actions
45 lines (34 loc) · 992 Bytes
/
consumer_auto_commit.php
File metadata and controls
45 lines (34 loc) · 992 Bytes
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
<?php
declare(strict_types=1);
use CrazyGoat\RabbitStream\Client\Connection;
use CrazyGoat\RabbitStream\VO\OffsetSpec;
require_once __DIR__ . '/../vendor/autoload.php';
$host = getenv('RABBITMQ_HOST') ?: '127.0.0.1';
$port = (int)(getenv('RABBITMQ_PORT') ?: 5552);
$connection = Connection::create(
host: $host,
port: $port,
user: 'guest',
password: 'guest',
);
// Resume from last stored offset, auto-commit every 1000 messages
$consumer = $connection->createConsumer(
'my-stream',
offset: OffsetSpec::first(),
name: 'my-consumer',
autoCommit: 1000,
);
$running = true;
pcntl_signal(SIGINT, function () use (&$running) {
$running = false;
});
while ($running) {
pcntl_signal_dispatch();
$message = $consumer->readOne(timeout: 5);
if ($message === null) {
continue;
}
echo "offset={$message->getOffset()} body={$message->getBody()}\n";
}
$consumer->close(); // stores final offset automatically
$connection->close();