Skip to content

Instantly share code, notes, and snippets.

View dgafka's full-sized avatar

Dariusz Gafka dgafka

View GitHub Profile
@dgafka
dgafka / command-controller.php
Created August 20, 2025 20:13
command-controller.php
<?php
class CommandController
{
public function execute(Request $request, CommandBus $commandBus): Response
{
$routingKey = $request->headers->get('X-Routing-Key');
$commandBus->sendWithRouting(
routingKey: $routingKey,
@dgafka
dgafka / rabbitmq-and-kafka-with-ecotone-03.php
Last active July 26, 2025 13:17
rabbitmq-and-kafka-with-ecotone-03.php
/** RabbitMQ Consumer example */
#[RabbitConsumer(
endpointId: 'transaction_handler',
queueName: 'transactions',
finalFailureStrategy: FinalFailureStrategy::STOP
)]
public function processTransactionEvent(TransactionEvent $event): void
{
// Handle Transaction Event
}
@dgafka
dgafka / rabbitmq-and-kafka-with-ecotone-02.php
Last active July 26, 2025 15:56
rabbitmq-and-kafka-with-ecotone-02.php
/** RabbitMQ Consumer example */
#[InstantRetry(retryTimes: 2, exceptions: [NetworkException::class])]
#[RabbitConsumer(endpointId: 'transaction_handler', queueName: 'transactions')]
public function processTransactionEvent(TransactionEvent $event): void
{
// handle
}
/** Kafka Consumer example */
#[InstantRetry(retryTimes: 2)] // no exceptions given, meaing retry on all exceptions
@dgafka
dgafka / rabbitmq-and-kafka-with-ecotone-01.php
Last active July 26, 2025 13:11
rabbitmq-and-kafka-with-ecotone-01.php
#[InstantRetry(retryTimes: 2, exceptions: [NetworkException::class])]
#[RabbitConsumer(endpointId: 'transaction_handler', queueName: 'transactions')]
public function processTransactionEvent(TransactionEvent $event): void
{
// handle
}
@dgafka
dgafka / asynchronous-messaging-13.php
Last active May 14, 2022 12:41
asynchronous-messaging-13.php
<?php
// Configuration on Publisher Side
public function changeBillingDetails(DistributedBus $distributedCommandBus)
{
$distributedCommandBus->sendCommand(
"billing", // destination
"billing.changeDetails", // routingKey
'["personId":"123","billingDetails":"01111"]',
"application/json"
@dgafka
dgafka / asynchronous-messaging-12.php
Last active May 13, 2022 17:43
asynchronous-messaging-12.php
<?php
class PlaceOrdernHandler
{
// default behaviour, first parameter is payload, second are headers
#[CommandHandler("place_order")]
public function placeOrder(PlaceOrder $payload, array $headers) {}
}
@dgafka
dgafka / asynchronous-messaging-11.php
Last active May 13, 2022 17:34
asynchronous-messaging-11.php
<?php
class MessagingConfiguration
{
#[ServiceContext]
public function asyncChannel()
{
// this channel can be reused for different message handlers
return AmqpBackedMessageChannelBuilder::create("async_channel");
}
@dgafka
dgafka / asynchronous-messaging-10.php
Created May 13, 2022 17:24
asynchronous-messaging-10.php
<?php
class AddExecutorIdMiddleware
{
/**
* @param mixed $job
* @param callable $next
* @return mixed
*/
public function handle($job, $next)
@dgafka
dgafka / asynchronous-messaging-09.php
Created May 13, 2022 17:18
asynchronous-messaging-09.php
<?php
PlaceOrder::dispatch($order)->onQueue('async_channel');
@dgafka
dgafka / asynchronous-messaging-08.php
Created May 13, 2022 17:01
asynchronous-messaging-08.php
<?php
class PlaceOrder implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
protected Order $order;
public function __construct(Order $order)
{