import io.cloudevents.CloudEvent;
import java.util.Map;
+import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.SerializationUtils;
-import org.springframework.util.concurrent.ListenableFuture;
-import org.springframework.util.concurrent.ListenableFutureCallback;
/**
* EventsPublisher to publish events.
@RequiredArgsConstructor
public class EventsPublisher<T> {
- /** Once all cps events will be modified to cloud compliant, will remove legacyKafkaEventTemplate with
- it's java configuration file KafkaTemplateConfig. **/
+ /**
+ * KafaTemplate for legacy (non-cloud) events.
+ *
+ * @deprecated Cloud events should be used. Will address soon as part of https://jira.onap.org/browse/CPS-1717
+ */
@Deprecated(forRemoval = true)
private final KafkaTemplate<String, T> legacyKafkaEventTemplate;
* @param event message payload
*/
public void publishCloudEvent(final String topicName, final String eventKey, final CloudEvent event) {
- final ListenableFuture<SendResult<String, CloudEvent>> eventFuture
- = cloudEventKafkaTemplate.send(topicName, eventKey, event);
- eventFuture.addCallback(handleCallback(topicName));
+ final CompletableFuture<SendResult<String, CloudEvent>> eventFuture =
+ cloudEventKafkaTemplate.send(topicName, eventKey, event);
+ eventFuture.whenComplete((result, e) -> {
+ if (e == null) {
+ log.debug("Successfully published event to topic : {} , Event : {}",
+ result.getRecordMetadata().topic(), result.getProducerRecord().value());
+
+ } else {
+ log.error("Unable to publish event to topic : {} due to {}", topicName, e.getMessage());
+ }
+ });
}
/**
* @param topicName valid topic name
* @param eventKey message key
* @param event message payload
- * @deprecated This method is not needed anymore since the use of headers will be in place.
+ * @deprecated Cloud events should be used. Will address soon as part of https://jira.onap.org/browse/CPS-1717
*/
@Deprecated(forRemoval = true)
public void publishEvent(final String topicName, final String eventKey, final T event) {
- final ListenableFuture<SendResult<String, T>> eventFuture
- = legacyKafkaEventTemplate.send(topicName, eventKey, event);
- eventFuture.addCallback(handleCallback(topicName));
+ final CompletableFuture<SendResult<String, T>> eventFuture =
+ legacyKafkaEventTemplate.send(topicName, eventKey, event);
+ eventFuture.whenComplete((result, e) -> {
+ if (e == null) {
+ log.debug("Successfully published event to topic : {} , Event : {}",
+ result.getRecordMetadata().topic(), result.getProducerRecord().value());
+ } else {
+ log.error("Unable to publish event to topic : {} due to {}", topicName, e.getMessage());
+ }
+ });
}
/**
final ProducerRecord<String, T> producerRecord =
new ProducerRecord<>(topicName, null, eventKey, event, eventHeaders);
- final ListenableFuture<SendResult<String, T>> eventFuture = legacyKafkaEventTemplate.send(producerRecord);
- eventFuture.addCallback(handleCallback(topicName));
+ final CompletableFuture<SendResult<String, T>> eventFuture =
+ legacyKafkaEventTemplate.send(producerRecord);
+ eventFuture.whenComplete((result, ex) -> {
+ if (ex != null) {
+ log.error("Unable to publish event to topic : {} due to {}", topicName, ex.getMessage());
+ } else {
+ log.debug("Successfully published event to topic : {} , Event : {}",
+ result.getRecordMetadata().topic(), result.getProducerRecord().value());
+ }
+ });
}
/**
publishEvent(topicName, eventKey, convertToKafkaHeaders(eventHeaders), event);
}
- private ListenableFutureCallback<SendResult<String, ?>> handleCallback(final String topicName) {
- return new ListenableFutureCallback<>() {
- @Override
- public void onFailure(final Throwable throwable) {
- log.error("Unable to publish event to topic : {} due to {}", topicName, throwable.getMessage());
- }
-
- @Override
- public void onSuccess(final SendResult<String, ?> sendResult) {
- log.debug("Successfully published event to topic : {} , Event : {}",
- sendResult.getRecordMetadata().topic(), sendResult.getProducerRecord().value());
- }
- };
- }
private Headers convertToKafkaHeaders(final Map<String, Object> eventMessageHeaders) {
final Headers eventHeaders = new RecordHeaders();