-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.php
90 lines (79 loc) · 3.22 KB
/
ping.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
<?php
declare(strict_types=1);
namespace Telephantast\Demo;
use Telephantast\BunnyTransport\BunnyConnectionPool;
use Telephantast\BunnyTransport\BunnyConsume;
use Telephantast\BunnyTransport\BunnyPublish;
use Telephantast\BunnyTransport\BunnySetup;
use Telephantast\MessageBus\Async\AddExchangeMiddleware;
use Telephantast\MessageBus\Async\Consumer;
use Telephantast\MessageBus\Async\MessageClassBasedExchangeResolver;
use Telephantast\MessageBus\Async\ObjectSerializer;
use Telephantast\MessageBus\Async\Publisher;
use Telephantast\MessageBus\CreatedAt\AddCreatedAtMiddleware;
use Telephantast\MessageBus\Handler\CallableHandler;
use Telephantast\MessageBus\Handler\HandlerWithMiddlewares;
use Telephantast\MessageBus\HandlerRegistry\ArrayHandlerRegistry;
use Telephantast\MessageBus\MessageBus;
use Telephantast\MessageBus\MessageId\AddCausationIdMiddleware;
use Telephantast\MessageBus\MessageId\AddCorrelationIdMiddleware;
use Telephantast\MessageBus\MessageId\AddMessageIdMiddleware;
use Telephantast\MessageBus\Outbox\OutboxConsumerMiddleware;
use Telephantast\MessageBus\Outbox\TryPublishViaOutboxMiddleware;
use Telephantast\PdoPersistence\PdoTransactionProvider;
use Telephantast\PdoPersistence\PostgresOutboxPdoStorage;
use function Amp\trapSignal;
/** @psalm-suppress MissingFile */
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/messages.php';
const QUEUE = 'ping';
// Setup Queue
$exchangeResolver = new MessageClassBasedExchangeResolver();
$objectNormalizer = new ObjectSerializer();
$publishPool = new BunnyConnectionPool(host: 'rabbitmq');
$consumePool = new BunnyConnectionPool(host: 'rabbitmq');
$transportSetup = new BunnySetup($publishPool);
$transportPublish = new BunnyPublish($publishPool, $objectNormalizer);
$transportConsume = new BunnyConsume($consumePool, $objectNormalizer);
$transportSetup->setup([
$exchangeResolver->resolve(Ping::class) => [],
$exchangeResolver->resolve(Pong::class) => [QUEUE],
]);
// Setup Outbox
$postgres = new \PDO('pgsql:host=postgres;port=5432;dbname=app;user=app;password=!ChangeMe!');
$transactionProvider = new PdoTransactionProvider($postgres);
$outboxStorage = new PostgresOutboxPdoStorage($postgres, table: 'outbox');
$outboxStorage->setup();
// Dispatch Ping
$messageBus = new MessageBus(
handlerRegistry: new ArrayHandlerRegistry([
Ping::class => new HandlerWithMiddlewares(new Publisher($transportPublish), [
new TryPublishViaOutboxMiddleware(),
]),
]),
middlewares: [
new AddMessageIdMiddleware(),
new AddCausationIdMiddleware(),
new AddCorrelationIdMiddleware(),
new AddCreatedAtMiddleware(),
new AddExchangeMiddleware(),
],
);
$messageBus->dispatch(new Ping());
// Consume Pong
$consumer = new Consumer(
queue: QUEUE,
handlerRegistry: new ArrayHandlerRegistry([
Pong::class => new CallableHandler('pong subscriber', static function (Pong $pong): void {
var_dump($pong);
}),
]),
middlewares: [
new OutboxConsumerMiddleware($outboxStorage, $transactionProvider, $transportPublish),
],
messageBus: $messageBus,
);
$transportConsume->runConsumer($consumer);
trapSignal([SIGINT, SIGTERM]);
$publishPool->disconnect();
$consumePool->disconnect();