Skip to content

Commit

Permalink
Introduce RuntimeCamelContextCustomizerBuildItem to allow to customiz…
Browse files Browse the repository at this point in the history
…e the camel context before it is started
  • Loading branch information
lburgazzoli committed May 13, 2020
1 parent 349cbb2 commit 3fb86f1
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.apache.camel.quarkus.core.deployment.spi.CamelTypeConverterLoaderBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelTypeConverterRegistryBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.ContainerBeansBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.RuntimeCamelContextCustomizerBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.UploadAttacherBuildItem;
import org.apache.camel.quarkus.core.deployment.util.CamelSupport;
import org.apache.camel.quarkus.core.deployment.util.PathFilter;
Expand Down Expand Up @@ -429,10 +430,9 @@ CamelContextBuildItem context(
CamelSupport.getCamelVersion(),
config);

customizerBuildItems.stream()
.forEach(customizer -> {
recorder.customize(context, customizer.getCamelContextCustomizer());
});
customizerBuildItems.forEach(customizer -> {
recorder.customize(context, customizer.getCamelContextCustomizer());
});

return new CamelContextBuildItem(context);
}
Expand Down Expand Up @@ -604,18 +604,20 @@ CamelMainBuildItem main(
/**
* This method is responsible to start camel-main ar runtime.
*
* @param recorder the recorder.
* @param main a reference to a {@link CamelMain}.
* @param registry a reference to a {@link org.apache.camel.spi.Registry}; note that this parameter is here as
* placeholder to
* ensure the {@link org.apache.camel.spi.Registry} is fully configured before starting camel-main.
* @param executor the {@link org.apache.camel.spi.ReactiveExecutor} to be configured on camel-main, this
* happens during {@link ExecutionTime#RUNTIME_INIT} because the executor may need to start
* threads and so on.
* @param shutdown a reference to a {@link io.quarkus.runtime.ShutdownContext} used to register shutdown logic.
* @param startList a placeholder to ensure camel-main start after the ArC container is fully initialized. This
* is required as under the hoods the camel registry may look-up beans form the
* container thus we need it to be fully initialized to avoid unexpected behaviors.
* @param recorder the recorder.
* @param main a reference to a {@link CamelMain}.
* @param registry a reference to a {@link org.apache.camel.spi.Registry}; note that this parameter is here as
* placeholder to
* ensure the {@link org.apache.camel.spi.Registry} is fully configured before starting camel-main.
* @param executor the {@link org.apache.camel.spi.ReactiveExecutor} to be configured on camel-main, this
* happens during {@link ExecutionTime#RUNTIME_INIT} because the executor may need to start
* threads and so on.
* @param shutdown a reference to a {@link io.quarkus.runtime.ShutdownContext} used to register shutdown logic.
* @param startList a placeholder to ensure camel-main start after the ArC container is fully initialized. This
* is required as under the hoods the camel registry may look-up beans form the
* container thus we need it to be fully initialized to avoid unexpected behaviors.
* @param customizers a list of {@link org.apache.camel.quarkus.core.CamelContextCustomizer} that will be executed
* during {@link ExecutionTime#RUNTIME_INIT} before starting the camel context.
*/
@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep(onlyIf = Flags.MainEnabled.class)
Expand All @@ -625,7 +627,12 @@ void start(
CamelRuntimeRegistryBuildItem registry,
CamelReactiveExecutorBuildItem executor,
ShutdownContextBuildItem shutdown,
List<ServiceStartBuildItem> startList) {
List<ServiceStartBuildItem> startList,
List<RuntimeCamelContextCustomizerBuildItem> customizers) {

for (RuntimeCamelContextCustomizerBuildItem customizer : customizers) {
recorder.customize(main.getInstance(), customizer.getCamelContextCustomizer());
}

recorder.setReactiveExecutor(main.getInstance(), executor.getInstance());
recorder.start(shutdown, main.getInstance());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.quarkus.core.deployment.spi;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.quarkus.core.CamelContextCustomizer;

/**
* A {@link MultiBuildItem} holding the {@link CamelContextCustomizer} {@link RuntimeValue} and could be used
* to customize the camel context before starting it.
*/
public final class RuntimeCamelContextCustomizerBuildItem extends MultiBuildItem {
private final RuntimeValue<CamelContextCustomizer> value;

public RuntimeCamelContextCustomizerBuildItem(RuntimeValue<CamelContextCustomizer> value) {
this.value = value;
}

public RuntimeValue<CamelContextCustomizer> getCamelContextCustomizer() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.camel.RoutesBuilder;
import org.apache.camel.impl.engine.DefaultReactiveExecutor;
import org.apache.camel.main.MainListener;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.RoutesCollector;
import org.apache.camel.spi.ReactiveExecutor;
import org.apache.camel.spi.XMLRoutesDefinitionLoader;
Expand Down Expand Up @@ -106,4 +107,15 @@ public RuntimeValue<RoutesCollector> newRoutesCollector(
return new RuntimeValue<>(new CamelRoutesCollector(registryRoutesLoader.getValue(), xmlRoutesLoader.getValue()));
}

public void customize(RuntimeValue<CamelMain> main, RuntimeValue<CamelContextCustomizer> contextCustomizer) {
final CamelContextCustomizer customizer = contextCustomizer.getValue();

main.getValue().addMainListener(new MainListenerSupport() {
@Override
public void configure(CamelContext context) {
customizer.customize(context);
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.camel.quarkus.component.microprofile.metrics.runtime.CamelMicroProfileMetricsConfig;
import org.apache.camel.quarkus.component.microprofile.metrics.runtime.CamelMicroProfileMetricsRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelMainListenerBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.RuntimeCamelContextCustomizerBuildItem;
import org.eclipse.microprofile.metrics.MetricRegistry;

class MicroProfileMetricsProcessor {
Expand All @@ -48,17 +48,19 @@ CamelBeanBuildItem metricRegistry(CamelMicroProfileMetricsRecorder recorder) {

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelMainListenerBuildItem contextConfigurerListener(
CamelContextCustomizerBuildItem contextCustomizer(
CamelMicroProfileMetricsRecorder recorder,
CamelMicroProfileMetricsConfig config) {

return new CamelMainListenerBuildItem(recorder.createContextConfigurerListener(config));
return new CamelContextCustomizerBuildItem(recorder.createContextCustomizer(config));
}

@Record(ExecutionTime.STATIC_INIT)
@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
public void configureCamelContext(CamelMicroProfileMetricsRecorder recorder, CamelMicroProfileMetricsConfig config,
CamelContextBuildItem camelContextBuildItem) {
recorder.configureCamelContext(config, camelContextBuildItem.getCamelContext());
RuntimeCamelContextCustomizerBuildItem runtimeContextCustomizer(
CamelMicroProfileMetricsRecorder recorder,
CamelMicroProfileMetricsConfig config) {

return new RuntimeCamelContextCustomizerBuildItem(recorder.createRuntimeContextCustomizer(config));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.apache.camel.component.microprofile.metrics.event.notifier.route.MicroProfileMetricsRouteEventNotifier;
import org.apache.camel.component.microprofile.metrics.message.history.MicroProfileMetricsMessageHistoryFactory;
import org.apache.camel.component.microprofile.metrics.route.policy.MicroProfileMetricsRoutePolicyFactory;
import org.apache.camel.main.MainListener;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.quarkus.core.CamelContextCustomizer;
import org.apache.camel.spi.ManagementStrategy;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.jboss.logging.Logger;
Expand All @@ -38,42 +37,52 @@ public RuntimeValue<MetricRegistry> createApplicationRegistry() {
return new RuntimeValue<>(MetricRegistries.get(MetricRegistry.Type.APPLICATION));
}

public RuntimeValue<MainListener> createContextConfigurerListener(CamelMicroProfileMetricsConfig config) {
return new RuntimeValue<>(new MicroProfileMetricsContextConfigurerListener(config));
public RuntimeValue<CamelContextCustomizer> createContextCustomizer(CamelMicroProfileMetricsConfig config) {
return new RuntimeValue<>(new MicroProfileMetricsContextCustomizer(config));
}

public void configureCamelContext(CamelMicroProfileMetricsConfig config,
RuntimeValue<CamelContext> camelContextRuntimeValue) {
CamelContext camelContext = camelContextRuntimeValue.getValue();
ManagementStrategy managementStrategy = camelContext.getManagementStrategy();
public RuntimeValue<CamelContextCustomizer> createRuntimeContextCustomizer(CamelMicroProfileMetricsConfig config) {
return new RuntimeValue<>(new MicroProfileMetricsRuntimeContextCustomizer(config));
}

if (config.enableRoutePolicy) {
camelContext.addRoutePolicyFactory(new MicroProfileMetricsRoutePolicyFactory());
}
private static class MicroProfileMetricsContextCustomizer implements CamelContextCustomizer {
private final CamelMicroProfileMetricsConfig config;

if (config.enableExchangeEventNotifier) {
managementStrategy.addEventNotifier(new MicroProfileMetricsExchangeEventNotifier());
public MicroProfileMetricsContextCustomizer(CamelMicroProfileMetricsConfig config) {
this.config = config;
}

if (config.enableRouteEventNotifier) {
managementStrategy.addEventNotifier(new MicroProfileMetricsRouteEventNotifier());
}
@Override
public void customize(CamelContext camelContext) {
if (config.enableRoutePolicy) {
camelContext.addRoutePolicyFactory(new MicroProfileMetricsRoutePolicyFactory());
}

ManagementStrategy managementStrategy = camelContext.getManagementStrategy();
if (config.enableExchangeEventNotifier) {
managementStrategy.addEventNotifier(new MicroProfileMetricsExchangeEventNotifier());
}

if (config.enableCamelContextEventNotifier) {
managementStrategy.addEventNotifier(new MicroProfileMetricsCamelContextEventNotifier());
if (config.enableRouteEventNotifier) {
managementStrategy.addEventNotifier(new MicroProfileMetricsRouteEventNotifier());
}

if (config.enableCamelContextEventNotifier) {
managementStrategy.addEventNotifier(new MicroProfileMetricsCamelContextEventNotifier());
}
}
}

private static class MicroProfileMetricsContextConfigurerListener extends MainListenerSupport {
private static final Logger LOGGER = Logger.getLogger(MicroProfileMetricsContextConfigurerListener.class);
private static class MicroProfileMetricsRuntimeContextCustomizer implements CamelContextCustomizer {
private static final Logger LOGGER = Logger.getLogger(MicroProfileMetricsRuntimeContextCustomizer.class);
private final CamelMicroProfileMetricsConfig config;

public MicroProfileMetricsContextConfigurerListener(CamelMicroProfileMetricsConfig config) {
public MicroProfileMetricsRuntimeContextCustomizer(CamelMicroProfileMetricsConfig config) {
this.config = config;
}

@Override
public void configure(CamelContext camelContext) {
public void customize(CamelContext camelContext) {
if (!config.enableMessageHistory) {
return;
}
Expand Down

0 comments on commit 3fb86f1

Please sign in to comment.