This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
commit 3e6058205c4647a143591fd79a99626696c43200 Author: Pasquale Congiusti <pasquale.congiu...@gmail.com> AuthorDate: Thu Mar 25 15:25:11 2021 +0100 feat(core): global default error handler Adding a new configurer that will look for a camel.k.global-error-handler property that can be used to set a default global error handler. Any route builder will inherit this error handler unless it is explicitly specified. --- .../k/listener/GlobalErrorHandlerConfigurer.java | 67 ++++++++++++++++++++++ .../services/org.apache.camel.k.Runtime$Listener | 1 + .../support/GlobalErrorHandlerConfigurerTest.java | 47 +++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/listener/GlobalErrorHandlerConfigurer.java b/camel-k-core/support/src/main/java/org/apache/camel/k/listener/GlobalErrorHandlerConfigurer.java new file mode 100644 index 0000000..794f794 --- /dev/null +++ b/camel-k-core/support/src/main/java/org/apache/camel/k/listener/GlobalErrorHandlerConfigurer.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.k.listener; + +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.builder.DeadLetterChannelBuilder; +import org.apache.camel.k.Runtime; +import org.apache.camel.k.support.PropertiesSupport; +import org.apache.camel.spi.Configurer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Configurer +public class GlobalErrorHandlerConfigurer extends AbstractPhaseListener { + private static final Logger LOGGER = LoggerFactory.getLogger(GlobalErrorHandlerConfigurer.class); + + public static final String CAMEL_K_PREFIX = "camel.k."; + public static final String CAMEL_K_GLOBAL_ERROR_HANDLER_PREFIX = "camel.k.global-error-handler"; + + private String globalErrorHandler; + + public GlobalErrorHandlerConfigurer() { + super(Runtime.Phase.ConfigureRoutes); + } + + public String getGlobalErrorHandler(){ + return this.globalErrorHandler; + } + + public void setGlobalErrorHandler(String globalErrorHandler){ + this.globalErrorHandler = globalErrorHandler; + } + + @Override + protected void accept(Runtime runtime) { + PropertiesSupport.bindProperties( + runtime.getCamelContext(), + this, + k -> k.startsWith(CAMEL_K_GLOBAL_ERROR_HANDLER_PREFIX), + CAMEL_K_PREFIX); + if (ObjectHelper.isNotEmpty(this.globalErrorHandler)) { + loadGlobalErrorHandler(runtime, this.getGlobalErrorHandler()); + } + } + + public static void loadGlobalErrorHandler(Runtime runtime, String uri) { + LOGGER.info("Setting a default global error handler factory to deadletter channel {}", uri); + runtime.getCamelContext().adapt(ExtendedCamelContext.class) + .setErrorHandlerFactory(new DeadLetterChannelBuilder(uri)); + } + +} diff --git a/camel-k-core/support/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener b/camel-k-core/support/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener index f0c50b7..9fc5078 100644 --- a/camel-k-core/support/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener +++ b/camel-k-core/support/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener @@ -16,5 +16,6 @@ # org.apache.camel.k.listener.ContextConfigurer +org.apache.camel.k.listener.GlobalErrorHandlerConfigurer org.apache.camel.k.listener.SourcesConfigurer org.apache.camel.k.listener.PropertiesConfigurer diff --git a/camel-k-core/support/src/test/java/org/apache/camel/k/support/GlobalErrorHandlerConfigurerTest.java b/camel-k-core/support/src/test/java/org/apache/camel/k/support/GlobalErrorHandlerConfigurerTest.java new file mode 100644 index 0000000..fdb7446 --- /dev/null +++ b/camel-k-core/support/src/test/java/org/apache/camel/k/support/GlobalErrorHandlerConfigurerTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.k.support; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.k.listener.GlobalErrorHandlerConfigurer; +import org.apache.camel.k.listener.SourcesConfigurer; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.k.test.CamelKTestSupport.asProperties; +import static org.assertj.core.api.Assertions.assertThat; + +public class GlobalErrorHandlerConfigurerTest { + + @Test + public void globalErrorHandlerIsBoundToSourcesConfigurer() { + CamelContext context = new DefaultCamelContext(); + context.getPropertiesComponent().setInitialProperties(asProperties( + "camel.k.global-error-handler", "someUriError" + )); + + GlobalErrorHandlerConfigurer configuration = new GlobalErrorHandlerConfigurer(); + + PropertiesSupport.bindProperties( + context, + configuration, + k -> k.startsWith(GlobalErrorHandlerConfigurer.CAMEL_K_GLOBAL_ERROR_HANDLER_PREFIX), + SourcesConfigurer.CAMEL_K_PREFIX); + + assertThat(configuration.getGlobalErrorHandler()).isEqualTo("someUriError"); + } +}