Search in sources :

Example 1 with MDCCloseable

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

the class AmazonSQSRemoteMessageDispatcherFactory method dispatch.

/* (non-Javadoc)
     * @see org.opennms.core.ipc.sink.common.AbstractMessageDispatcherFactory#dispatch(org.opennms.core.ipc.sink.api.SinkModule, java.lang.Object, org.opennms.core.ipc.sink.api.Message)
     */
@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);
        try {
            final String queueUrl = awsSqsManager.getSinkQueueUrlAndCreateIfNecessary(module.getId());
            final String messageId = awsSqsManager.sendMessage(queueUrl, new String(module.marshal((T) message), StandardCharsets.UTF_8));
            LOG.debug("SQS Message with ID {} has been successfully sent to {}", messageId, queueUrl);
        } catch (InterruptedException ex) {
            LOG.warn("Interrupted while trying to send message. Aborting.", ex);
        } catch (RuntimeException ex) {
            LOG.error("Unexpected AWS SDK exception while sending a message. Aborting.", ex);
        }
    }
}
Also used : MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable)

Example 2 with MDCCloseable

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

the class KafkaRemoteMessageDispatcherFactory method init.

public void init() throws IOException {
    try (MDCCloseable mdc = Logging.withPrefixCloseable(MessageConsumerManager.LOG_PREFIX)) {
        registerJmxReporter();
        // Defaults
        kafkaConfig.clear();
        kafkaConfig.put("key.serializer", StringSerializer.class.getCanonicalName());
        kafkaConfig.put("value.serializer", ByteArraySerializer.class.getCanonicalName());
        // Retrieve all of the properties from org.opennms.core.ipc.sink.kafka.cfg
        final Dictionary<String, Object> properties = configAdmin.getConfiguration(KafkaSinkConstants.KAFKA_CONFIG_PID).getProperties();
        if (properties != null) {
            final Enumeration<String> keys = properties.keys();
            while (keys.hasMoreElements()) {
                final String key = keys.nextElement();
                kafkaConfig.put(key, properties.get(key));
            }
        }
        LOG.info("KafkaRemoteMessageDispatcherFactory: initializing the Kafka producer with: {}", kafkaConfig);
        final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            // Class-loader hack for accessing the org.apache.kafka.common.serialization.ByteArraySerializer
            Thread.currentThread().setContextClassLoader(null);
            producer = new KafkaProducer<>(kafkaConfig);
        } finally {
            Thread.currentThread().setContextClassLoader(currentClassLoader);
        }
    }
}
Also used : MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer)

Example 3 with MDCCloseable

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

the class AbstractMessageConsumerManager method unregisterConsumer.

@SuppressWarnings("unchecked")
@Override
public synchronized <S extends Message, T extends Message> void unregisterConsumer(MessageConsumer<S, T> consumer) throws Exception {
    if (consumer == null) {
        return;
    }
    try (MDCCloseable mdc = Logging.withPrefixCloseable(MessageConsumerManager.LOG_PREFIX)) {
        LOG.info("Unregistering consumer: {}", consumer);
        final SinkModule<?, Message> module = (SinkModule<?, Message>) consumer.getModule();
        consumersByModule.remove(module, (MessageConsumer<?, Message>) consumer);
        if (consumersByModule.get(module).size() < 1) {
            waitForStartup.thenRunAsync(new Runnable() {

                @Override
                public void run() {
                    try {
                        LOG.info("Stopping consumption of messages for module: {}", module);
                        stopConsumingForModule(module);
                    } catch (Exception e) {
                        LOG.error("Unexpected exception while trying to stop consumer for module: {}", module, e);
                    }
                }
            });
        }
    }
}
Also used : SinkModule(org.opennms.core.ipc.sink.api.SinkModule) Message(org.opennms.core.ipc.sink.api.Message) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable)

Example 4 with MDCCloseable

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

the class AbstractMessageConsumerManager method registerConsumer.

@SuppressWarnings("unchecked")
@Override
public synchronized <S extends Message, T extends Message> void registerConsumer(MessageConsumer<S, T> consumer) throws Exception {
    if (consumer == null) {
        return;
    }
    try (MDCCloseable mdc = Logging.withPrefixCloseable(MessageConsumerManager.LOG_PREFIX)) {
        LOG.info("Registering consumer: {}", consumer);
        final SinkModule<?, Message> module = (SinkModule<?, Message>) consumer.getModule();
        final int numConsumersBefore = consumersByModule.get(module).size();
        consumersByModule.put(module, (MessageConsumer<?, Message>) consumer);
        if (numConsumersBefore < 1) {
            waitForStartup.thenRunAsync(new Runnable() {

                @Override
                public void run() {
                    try {
                        LOG.info("Starting to consume messages for module: {}", module);
                        startConsumingForModule(module);
                    } catch (Exception e) {
                        LOG.error("Unexpected exception while trying to start consumer for module: {}", module, e);
                    }
                }
            });
        }
    }
}
Also used : SinkModule(org.opennms.core.ipc.sink.api.SinkModule) Message(org.opennms.core.ipc.sink.api.Message) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable)

Example 5 with MDCCloseable

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

the class CamelRpcClientFactory method getClient.

@Override
public <S extends RpcRequest, T extends RpcResponse> RpcClient<S, T> getClient(RpcModule<S, T> module) {
    return new RpcClient<S, T>() {

        @Override
        public CompletableFuture<T> execute(S request) {
            if (request.getLocation() == null || request.getLocation().equals(location)) {
                // The request is for the current location, invoke it directly
                return module.execute(request);
            }
            // Save the context map and restore it on callback
            final Map<String, String> clientContextMap = Logging.getCopyOfContextMap();
            // Wrap the request in a CamelRpcRequest and forward it to the Camel route
            final CompletableFuture<T> future = new CompletableFuture<>();
            try {
                template.asyncCallbackSendBody(endpoint, new CamelRpcRequest<>(module, request), new Synchronization() {

                    @Override
                    public void onComplete(Exchange exchange) {
                        try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
                            final T response = module.unmarshalResponse(exchange.getOut().getBody(String.class));
                            if (response.getErrorMessage() != null) {
                                future.completeExceptionally(new RemoteExecutionException(response.getErrorMessage()));
                            } else {
                                future.complete(response);
                            }
                        } catch (Throwable ex) {
                            LOG.error("Unmarshalling a response in RPC module {} failed.", module, ex);
                            future.completeExceptionally(ex);
                        }
                        // Ensure that future log statements on this thread are routed properly
                        Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
                    }

                    @Override
                    public void onFailure(Exchange exchange) {
                        try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
                            final ExchangeTimedOutException timeoutException = exchange.getException(ExchangeTimedOutException.class);
                            final DirectConsumerNotAvailableException directConsumerNotAvailableException = exchange.getException(DirectConsumerNotAvailableException.class);
                            if (timeoutException != null) {
                                // Wrap timeout exceptions within a RequestTimedOutException
                                future.completeExceptionally(new RequestTimedOutException(exchange.getException()));
                            } else if (directConsumerNotAvailableException != null) {
                                // Wrap consumer not available exceptions with a RequestRejectedException
                                future.completeExceptionally(new RequestRejectedException(exchange.getException()));
                            } else {
                                future.completeExceptionally(exchange.getException());
                            }
                        }
                        // Ensure that future log statements on this thread are routed properly
                        Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
                    }
                });
            } catch (IllegalStateException e) {
                try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
                    // Wrap ProducerTemplate exceptions with a RequestRejectedException
                    future.completeExceptionally(new RequestRejectedException(e));
                }
                // Ensure that future log statements on this thread are routed properly
                Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
            }
            return future;
        }
    };
}
Also used : DirectConsumerNotAvailableException(org.apache.camel.component.direct.DirectConsumerNotAvailableException) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) Synchronization(org.apache.camel.spi.Synchronization) Exchange(org.apache.camel.Exchange) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) RpcClient(org.opennms.core.rpc.api.RpcClient)

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