Repository navigation
Expand file tree
/
Copy pathfetch-batch.php
More file actions
58 lines (44 loc) · 1.57 KB
/
Copy pathfetch-batch.php
File metadata and controls
58 lines (44 loc) · 1.57 KB
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
<?php
/**
* Fetch Batch - pull several messages in one request.
*
* Publishes several messages, then retrieves up to N of them in a single pull with
* fetchBatch() (bounded by a pull expiry) and acks each one.
*
* Mirrors the README "Fetch Batch" example. Run: php examples/fetch-batch.php
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use IDCT\NATS\Connection\NatsOptions;
use IDCT\NATS\Core\NatsClient;
$url = getenv('NATS_URL') ?: 'nats://127.0.0.1:4222';
$client = new NatsClient(new NatsOptions(servers: [$url], name: 'example-fetch-batch'));
$client->connect()->await();
$stream = 'EX_FETCH_BATCH_LOGS';
$consumer = 'EX_FETCH_BATCH';
$js = $client->jetStream();
try {
$js->createStream($stream, ['ex.fetchbatch.logs.>'])->await();
$js->createConsumer($stream, $consumer, 'ex.fetchbatch.logs.>')->await();
for ($i = 0; $i < 5; $i++) {
$js->publish('ex.fetchbatch.logs.app', "log entry $i")->await();
}
// Fetch up to 5 messages in one batch (3s pull expiry).
$messages = $js->fetchBatch($stream, $consumer, batch: 5, expiresMs: 3000)->await();
foreach ($messages as $message) {
$js->ack($message)->await();
}
echo 'OK fetch-batch: fetched and acked ' . count($messages) . " message(s) in one batch\n";
} finally {
try {
$js->deleteConsumer($stream, $consumer)->await();
} catch (\Throwable) {
// best-effort cleanup
}
try {
$js->deleteStream($stream)->await();
} catch (\Throwable) {
// best-effort cleanup
}
$client->disconnect()->await();
}