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);
}
}
}
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);
}
}
}
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);
}
}
});
}
}
}
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);
}
}
});
}
}
}
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;
}
};
}
Aggregations