1616use Jobcloud \Kafka \Exception \KafkaConsumerConsumeException ;
1717use Jobcloud \Kafka \Exception \KafkaConsumerEndOfPartitionException ;
1818use Jobcloud \Kafka \Exception \KafkaConsumerTimeoutException ;
19+ use Jobcloud \Kafka \Message \Decoder \AvroDecoder ;
20+ use Jobcloud \Kafka \Message \Encoder \AvroEncoder ;
21+ use Jobcloud \Kafka \Message \Registry \AvroSchemaRegistry ;
1922use Symfony \Component \Console \Attribute \AsCommand ;
2023use Symfony \Component \Console \Command \Command ;
2124use Symfony \Component \Console \Input \InputArgument ;
@@ -46,14 +49,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4649 {
4750 $ io = new SymfonyStyle ($ input , $ output );
4851
49-
5052 $ schemaRegistryClient = new CachedRegistry (
5153 new PromisingRegistry (
5254 new Client (['base_uri ' => 'schema-registry:8081 ' ])
5355 ),
5456 new AvroObjectCacheAdapter ()
5557 );
5658
59+ $ registry = new AvroSchemaRegistry ($ schemaRegistryClient );
60+
5761 $ recordSerializer = new RecordSerializer (
5862 $ schemaRegistryClient ,
5963 [
@@ -62,19 +66,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6266 ]
6367 );
6468
65- $ consumer = KafkaConsumerBuilder::create ()
66- ->withAdditionalConfig (
67- [
68- 'enable.auto.commit ' => false
69- ]
70- )
71- ->withAdditionalBroker ('kafka:9092 ' )
72- ->withConsumerGroup ('testGroup ' )
73- ->withAdditionalSubscription ('users ' )
74- ->build ();
75-
76- $ consumer ->subscribe ();
77-
7869 $ schema = <<<'JSON'
7970 {
8071 "type": "record",
@@ -89,6 +80,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8980
9081 $ avroSchema = AvroSchema::parse ($ schema );
9182
83+ $ decoder = new AvroDecoder ($ registry , $ recordSerializer );
84+
85+ $ consumer = KafkaConsumerBuilder::create ()
86+ ->withAdditionalConfig (
87+ [
88+ 'enable.auto.commit ' => false ,
89+ ]
90+ )
91+ ->withDecoder ($ decoder )
92+ ->withAdditionalBroker ('kafka:9092 ' )
93+ ->withConsumerGroup ('testGroup ' )
94+ ->withAdditionalSubscription ('users ' )
95+ ->build ();
96+
97+ $ consumer ->subscribe ();
98+
9299 while (true ) {
93100 try {
94101 $ message = $ consumer ->consume ();
0 commit comments