Search in sources :

Example 1 with SendResult

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);
        }
    });
}
Also used : MessageException(org.openkilda.messaging.error.MessageException) SendResult(org.springframework.kafka.support.SendResult) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with SendResult

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);
    }
}
Also used : MessageException(org.openkilda.messaging.error.MessageException) SendResult(org.springframework.kafka.support.SendResult) ExecutionException(java.util.concurrent.ExecutionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 MessageException (org.openkilda.messaging.error.MessageException)2 SendResult (org.springframework.kafka.support.SendResult)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1