How to add Kafka Message Headers?

Share with Love

To work with Kafka headers in Pega, you can use the built-in integration with Apache Kafka provided by Pega.

First, you need to configure a data set to define the Kafka connection and topic details. This can be done in the Pega Designer Studio by creating a new Data Set rule with the data set type “Kafka Data Set”. You can specify the connection details, such as the broker host and port, and the topic details, such as the name and message format.

Once the data set is configured, you can use the “Kafka Data Flow” rule to read messages from the Kafka topic and process them in Pega. In the Kafka Data Flow, you can use the “Kafka Message” data type to access the message content, key, and headers.

To access the headers, you can use the “Headers” property of the “Kafka Message” data type. This property returns a “Map<String, String>” object that represents the headers of the Kafka message. You can use the standard Java methods to read and modify the header values as needed.

For example, you can use the following code to read the value of a header named “my-header” from a Kafka message:

KafkaMessage msg = …; // get the Kafka message
String value = msg.getHeaders().get(“my-header”);
Similarly, you can use the “put” method to add or modify a header value:

msg.getHeaders().put(“my-header”, “new-value”);
Once you have processed the Kafka message in Pega, you can use the “Kafka Producer” rule to write messages back to the Kafka topic, including any modified header values.