Skip to content

Package: ErrorMessageListener

ErrorMessageListener

nameinstructionbranchcomplexitylinemethod
ErrorMessageListener()
M: 40 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
lifeCycleMessage(Exchange, Object)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
logError(Exchange, Object, String)
M: 72 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
processMessage(Exchange, Object)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.broker.core.listener;
14:
15: import com.codahale.metrics.Counter;
16: import org.apache.camel.Exchange;
17: import org.apache.camel.spi.UriEndpoint;
18: import org.apache.commons.lang3.SerializationUtils;
19: import org.eclipse.kapua.KapuaException;
20: import org.eclipse.kapua.broker.core.message.MessageConstants;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.util.Base64;
25:
26: @UriEndpoint(title = "error message processor", syntax = "bean:errorMessageListener", scheme = "bean")
27: /**
28: * Error message listener endpoint.
29: *
30: * @since 1.0
31: */
32: public class ErrorMessageListener extends AbstractListener {
33:
34: private static final Logger LOG = LoggerFactory.getLogger(ErrorMessageListener.class);
35:
36: private static final String NO_EXCEPTION_FOUND_MESSAGE = "NO EXCEPTION FOUND!";
37:
38: private Counter metricError;
39: private Counter metricErrorLifeCycleMessage;
40:
41: public ErrorMessageListener() {
42: super("error");
43: metricError = registerCounter("messages", "generic", "count");
44: metricErrorLifeCycleMessage = registerCounter("messages", "life_cycle", "count");
45: }
46:
47: /**
48: * Process an error condition for an elaboration of a generic message
49: *
50: * @param exchange
51: * @param message
52: * @throws KapuaException
53: */
54: public void processMessage(Exchange exchange, Object message) throws KapuaException {
55: metricError.inc();
56: logError(exchange, message, "generic");
57: }
58:
59: /**
60: * Process an error condition for an elaboration of a life cycle message
61: *
62: * @param exchange
63: * @param message
64: * @throws KapuaException
65: */
66: public void lifeCycleMessage(Exchange exchange, Object message) throws KapuaException {
67: metricErrorLifeCycleMessage.inc();
68: logError(exchange, message, "LifeCycle");
69: }
70:
71: private void logError(Exchange exchange, Object message, String serviceName) {
72: Throwable t = null;
73: // looking at the property filled by the KapuaCamelFilter#bridgeError)
74: String encodedException = exchange.getIn().getHeader(MessageConstants.HEADER_KAPUA_PROCESSING_EXCEPTION, String.class);
75:• if (encodedException != null) {
76: t = SerializationUtils.deserialize(Base64.getDecoder().decode(exchange.getIn().getHeader(MessageConstants.HEADER_KAPUA_PROCESSING_EXCEPTION, String.class)));
77: } else {
78: // otherwise fallback to the exchange property or the exchange exception
79: t = ((Throwable) exchange.getProperty(CamelConstants.JMS_EXCHANGE_FAILURE_EXCEPTION));
80:• if (t == null) {
81: t = exchange.getException();
82: }
83: }
84:• LOG.warn("Processing error message for service {}... Message type {} - Endpoint {} - Error message {}",
85: serviceName,
86: (message != null ? message.getClass().getName() : null),
87:• exchange.getProperty(CamelConstants.JMS_EXCHANGE_FAILURE_ENDPOINT),
88: t != null ? t.getMessage() : NO_EXCEPTION_FOUND_MESSAGE);
89: LOG.warn("Exception: ", t);
90: }
91: }