use of org.springframework.kafka.support.SendResult in project open-kilda by telstra.
the class KafkaMessageProducer method send.
/**
* Sends messages to WorkFlowManager.
*
* @param topic kafka topic
* @param object object to serialize and send
*/
public void send(final String topic, final Object object) {
String message;
try {
message = MAPPER.writeValueAsString(object);
} catch (JsonProcessingException exception) {
logger.error("Unable to serialize object: object={}", object, exception);
throw new MessageException(DATA_INVALID, System.currentTimeMillis());
}
kafkaTemplate.send(topic, message).addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onSuccess(SendResult<String, String> result) {
logger.debug("Message sent: topic={}, message={}", topic, message);
}
@Override
public void onFailure(Throwable exception) {
logger.error("Unable to send message: topic={}, message={}", topic, message, exception);
}
});
}
use of org.springframework.kafka.support.SendResult in project open-kilda by telstra.
the class KafkaMessageProducer method send.
/**
* {@inheritDoc}
*/
@Override
public void send(final String topic, final Object object) {
ListenableFuture<SendResult<String, String>> future;
String message;
try {
message = MAPPER.writeValueAsString(object);
} catch (JsonProcessingException exception) {
String errorMessage = "Unable to serialize object";
logger.error("{}: object={}", errorMessage, object, exception);
throw new MessageException(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), DATA_INVALID, errorMessage, object.toString());
}
future = kafkaTemplate.send(topic, message);
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onSuccess(SendResult<String, String> result) {
logger.debug("Message sent: topic={}, message={}", topic, message);
}
@Override
public void onFailure(Throwable exception) {
logger.error("Unable to send message: topic={}, message={}", topic, message, exception);
}
});
try {
SendResult<String, String> result = future.get(TIMEOUT, TimeUnit.MILLISECONDS);
logger.debug("Record sent: record={}, metadata={}", result.getProducerRecord(), result.getRecordMetadata());
} catch (TimeoutException | ExecutionException | InterruptedException exception) {
String errorMessage = "Unable to send message";
logger.error("{}: topic={}, message={}", errorMessage, topic, message, exception);
throw new MessageException(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), INTERNAL_ERROR, errorMessage, message);
}
}
Aggregations