Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Problem accessing protected properties of Consumed Message #11

Closed
zensabbah opened this issue Nov 2, 2021 · 2 comments
Closed
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@zensabbah
Copy link

zensabbah commented Nov 2, 2021

Hi Mateus, thanks for your work.
I successfully installed your package and I'm able to produce and send a message in this way:

$message = new Message(
    'topicname',
    0,
    ['header-key' => 'header-value'],
    ['key' => 'value']
);

$producer =  Kafka::publishOn('....:9092', 'topicname')->withMessage($message);

$producer->send();

I successfully consume the message but I cannot access the properties because they are protected in this way:

$consumer = Kafka::createConsumer('....:9092')
    ->withAutoCommit()
    ->withConsumerGroupId('group')->subscribe('topicname')
    ->withHandler(function(\Junges\Kafka\Contracts\KafkaConsumerMessage $message) {
        // Handle your message here
        print_r($message);
    })->build();
        
$consumer->consume();

What am I missing?

Thanks in advance

P.S. This is an example of the message I consume:

Object
(
    [topicName:protected] => topicname
    [partition:protected] => 0
    [headers:protected] => Array
        (
            [header-key] => header-value
        )

    [body:protected] => Array
        (
            [key] => value
        )

    [key:protected] => 
    [offset:protected] => 164
    [timestamp:protected] => 1635866426370
)
@mateusjunges
Copy link
Owner

mateusjunges commented Nov 2, 2021

Hi @zensabbah, you can use the message getters to get message properties.

Here is an working example:

This is my producer:

$message = new Message(
    'default',
    0,
    ['header-key' => 'header-value'],
    ['key' => 'value']
);

$producer =  Kafka::publishOn('localhost:9092', 'default')->withMessage($message);

$producer->send();

And this is my consumer:

$consumer = Kafka::createConsumer('localhost:9092')->withAutoCommit()
    ->withConsumerGroupId('group')
    ->subscribe('default')
    ->withHandler(function(\Junges\Kafka\Contracts\KafkaConsumerMessage $message) {
        dd([
            'body' => $message->getBody(),
            'headers' => $message->getHeaders(),
            'key' => $message->getKey(),
            'topic-name' => $message->getTopicName(),
            'timestamp' => $message->getTimestamp(),
            'partition' => $message->getPartition(),
            'offset' => $message->getOffset()
        ]);
    })->build();


$consumer->consume();

This is the result dd gives to me:

array:7 [
  "body" => array:1 [
    "key" => "value"
  ]
  "headers" => array:1 [
    "header-key" => "header-value"
  ]
  "key" => null
  "topic-name" => "default"
  "timestamp" => 1635857918545
  "partition" => 0
  "offset" => 1
]

@zensabbah
Copy link
Author

Thanks a lot Mateus!

@mateusjunges mateusjunges added documentation Improvements or additions to documentation question Further information is requested labels Nov 6, 2021
@mateusjunges mateusjunges changed the title Problem accessing protected properties of Consumed Message [QUESTION] Problem accessing protected properties of Consumed Message Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants