PHP code example of limlabs / kafka-bundle
1. Go to this page and download the library: Download limlabs/kafka-bundle library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
limlabs / kafka-bundle example snippets
namespace App \Controller ;
use LimLabs \KafkaBundle \Factory \KafkaFactory ;
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
use Symfony \Component \HttpFoundation \JsonResponse ;
use Symfony \Component \Routing \Annotation \Route ;
class ExampleController extends AbstractController
{
public function __construct (
private readonly KafkaFactory $kafkaFactory
) {
}
public function index () : JsonResponse
{
$client = $this ->kafkaFactory->getKafkaClient();
$producer = $client->getProducer();
$producer->createTopic('test' )->produce('TestMessage' );
$producer->flush();
[...]
}
$client = $this ->kafkaFactory->getKafkaClient('different_client' );
namespace App \Consumers ;
use LimLabs \KafkaBundle \Enum \ConsumerResponse ;
use LimLabs \KafkaBundle \Kafka \Consumer \ConsumerConfiguration ;
use LimLabs \KafkaBundle \Kafka \Consumer \KafkaConsumer ;
class ExampleQueueConsumer implements KafkaConsumer
{
public function consume (string $message) : ConsumerResponse
{
var_dump($message);
return ConsumerResponse::SUCCESS;
}
public function getConsumerConfiguration () : ConsumerConfiguration
{
return ConsumerConfiguration::createConfiguration([
'consumer_group' => 'example_consumer_group' ,
'connection' => 'different_client' ,
'subscribed_topics' => [
'example_topic'
]
]);
}
}
public function getConsumerConfiguration () : ConsumerConfiguration
{
$configuration = new ConsumerConfiguration();
$configuration->setConnection('different_client' );
$configuration->setConsumerGroup('example_consumer_group' );
$configuration->setSubscribedTopics(['example_topic' ]);
return $configuration;
}
ConsumerResponse::SUCCESS;
ConsumerResponse::ERROR_DROP;
ConsumerResponse::ERROR_REQUEUE;