Search in sources :

Example 6 with MDCCloseable

use of org.opennms.core.logging.Logging.MDCCloseable in project opennms by OpenNMS.

the class KafkaRemoteMessageDispatcherFactory method dispatch.

@Override
public <S extends Message, T extends Message> void dispatch(SinkModule<S, T> module, String topic, T message) {
    try (MDCCloseable mdc = Logging.withPrefixCloseable(MessageConsumerManager.LOG_PREFIX)) {
        LOG.trace("dispatch({}): sending message {}", topic, message);
        final ProducerRecord<String, byte[]> record = new ProducerRecord<>(topic, module.marshal(message));
        try {
            // From KafkaProducer's JavaDoc: The producer is thread safe and should generally be shared among all threads for best performance.
            final Future<RecordMetadata> future = producer.send(record);
            // The call to dispatch() is synchronous, so we block until the message was sent
            future.get();
        } catch (InterruptedException e) {
            LOG.warn("Interrupted while sending message to topic {}.", topic, e);
        } catch (ExecutionException e) {
            LOG.error("Error occured while sending message to topic {}.", topic, e);
        }
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

MDCCloseable (org.opennms.core.logging.Logging.MDCCloseable)6 Message (org.opennms.core.ipc.sink.api.Message)2 SinkModule (org.opennms.core.ipc.sink.api.SinkModule)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Exchange (org.apache.camel.Exchange)1 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)1 DirectConsumerNotAvailableException (org.apache.camel.component.direct.DirectConsumerNotAvailableException)1 Synchronization (org.apache.camel.spi.Synchronization)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 ByteArraySerializer (org.apache.kafka.common.serialization.ByteArraySerializer)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 RemoteExecutionException (org.opennms.core.rpc.api.RemoteExecutionException)1 RequestRejectedException (org.opennms.core.rpc.api.RequestRejectedException)1 RequestTimedOutException (org.opennms.core.rpc.api.RequestTimedOutException)1 RpcClient (org.opennms.core.rpc.api.RpcClient)1