com.ibm.cp4waiops.connectors.sdk.CloudEventDeserializerCustom Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connectors-sdk Show documentation
Show all versions of connectors-sdk Show documentation
A developer SDK for creating connectors for CP4WAIOps.
package com.ibm.cp4waiops.connectors.sdk;
import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.message.MessageReader;
import io.cloudevents.kafka.KafkaMessageFactory;
import io.cloudevents.rw.CloudEventDataMapper;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.serialization.Deserializer;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* Based on CloudEventDeserializer, with some additional error handling for
* deserialiation problem. The original CloudEventDeserializer, when the deserialization
* error occurs (e.g. if a non-CloudEvent is put in the Kafka topic) the Kafka Consumer
* will be stuck reading the same message forever. It will not skip it. This class ensures the
* record is skipped by returning null and not throwing an error, so the Kafka Consumer can
* read the next record.
*
* Reference source: https://github.com/cloudevents/sdk-java/blob/main/kafka/src/main/java/io/cloudevents/kafka/CloudEventDeserializer.java
*
*/
public class CloudEventDeserializerCustom implements Deserializer {
/**
* The configuration key for the {@link CloudEventDataMapper}.
*/
public final static String MAPPER_CONFIG = "cloudevents.datamapper";
private static final Logger logger = Logger.getLogger(CloudEventDeserializerCustom.class.getName());
private CloudEventDataMapper extends CloudEventData> mapper = null;
@Override
public void configure(Map configs, boolean isKey) {
Object mapperConfig = configs.get(MAPPER_CONFIG);
if (mapperConfig instanceof CloudEventDataMapper) {
this.mapper = (CloudEventDataMapper extends CloudEventData>) mapperConfig;
} else if (mapperConfig != null) {
throw new IllegalArgumentException(
MAPPER_CONFIG + " can be of type String or " + CloudEventDataMapper.class.getCanonicalName());
}
}
@Override
public CloudEvent deserialize(String topic, byte[] data) {
throw new UnsupportedOperationException(
"CloudEventDeserializer supports only the signature deserialize(String, Headers, byte[])");
}
@Override
public CloudEvent deserialize(String topic, Headers headers, byte[] data) {
try {
MessageReader reader = KafkaMessageFactory.createReader(headers, data);
if (mapper == null) {
return reader.toEvent();
} else {
return reader.toEvent(mapper);
}
} catch (Exception e) {
// This would not be reported as an error, since invalid CloudEvents
// could occur in Kafka
logger.log(Level.FINEST, "Invalid CloudEvent read from Kafka", e);
}
// Return null so reading from Kafka can continue. If this throws an error, then
// the problematic Kafka message will be read forever and the offset won't be increased
// This null return is to deal with the "poison pill" scenario in Java Kafka consumption
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy