Skip to content

Commit

Permalink
Activate Camel dev profile when running in development mode
Browse files Browse the repository at this point in the history
Fixes #6083
  • Loading branch information
jamesnetherton committed Sep 16, 2024
1 parent 8383b39 commit 9b6665f
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.BeanDiscoveryFinishedBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
Expand Down Expand Up @@ -105,24 +104,6 @@ CamelContextBuildItem context(
return new CamelContextBuildItem(context);
}

/**
* This step customizes camel context for development mode.
*
* @param recorder the recorder
* @param producer producer of context customizer build item
*/
@Record(ExecutionTime.STATIC_INIT)
@BuildStep(onlyIf = IsDevelopment.class)
public void devModeCamelContextCustomizations(
CamelContextRecorder recorder,
BuildProducer<CamelContextCustomizerBuildItem> producer) {
String val = CamelSupport.getOptionalConfigValue("camel.main.shutdownTimeout", String.class, null);
if (val == null) {
//if no graceful timeout is set in development mode, graceful shutdown is replaced with no shutdown
producer.produce(new CamelContextCustomizerBuildItem(recorder.createNoShutdownStrategyCustomizer()));
}
}

/**
* Enable source location if quarkus.camel.source-location-enabled=true
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.devmode;

import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import org.apache.camel.quarkus.core.CamelContextRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem;

import static org.apache.camel.quarkus.core.CamelCapabilities.DSL_MODELINE;

/**
* Build steps relating to customizations that should only be made in Dev Mode.
*/
@BuildSteps(onlyIf = IsDevelopment.class)
class CamelDevModeProcessor {
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void customizeDevModeCamelContext(
CamelContextBuildItem camelContext,
Capabilities capabilities,
CamelContextRecorder recorder) {
recorder.customizeDevModeCamelContext(camelContext.getCamelContext(), capabilities.isMissing(DSL_MODELINE));
}
}
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.main.devmode;

import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import org.apache.camel.quarkus.core.deployment.main.spi.CamelMainBuildItem;
import org.apache.camel.quarkus.main.CamelMainRecorder;

/**
* Build steps relating to camel-main customizations that should only be made in Dev Mode.
*/
@BuildSteps(onlyIf = IsDevelopment.class)
class CamelMainDevModeProcessor {
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void customizeDevModeCamelMain(CamelMainBuildItem camelMain, CamelMainRecorder recorder) {
recorder.customizeDevModeCamelMain(camelMain.getInstance());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.runtime;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import io.quarkus.test.QuarkusDevModeTest;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class CamelDevModeProfileTest {
@RegisterExtension
static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.setLogRecordPredicate(record -> record.getLevel().equals(Level.INFO))
.withEmptyApplication();

@Test
void camelMainDevProfileConfigured() {
Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> {
return TEST.getLogRecords().stream()
.anyMatch(logRecord -> logRecord.getMessage().contains("The application is starting with profile: dev"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class CamelCapabilities {
public static final String BEAN = "org.apache.camel.bean";
public static final String CLOUD_EVENTS = "org.apache.camel.cloudevents";
public static final String CORE = "org.apache.camel";
public static final String DSL_MODELINE = "org.apache.camel.dsl.modeline";
public static final String XML = "org.apache.camel.xml";
public static final String XML_IO_DSL = "org.apache.camel.xml.io.dsl";
public static final String XML_JAXB = "org.apache.camel.xml.jaxb";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.quarkus.core;

import java.util.Optional;
import java.util.Set;

import io.quarkus.arc.runtime.BeanContainer;
Expand All @@ -26,17 +27,21 @@
import org.apache.camel.catalog.RuntimeCamelCatalog;
import org.apache.camel.impl.debugger.BacklogTracer;
import org.apache.camel.impl.engine.DefaultVariableRepositoryFactory;
import org.apache.camel.quarkus.core.devmode.NoOpModelineFactory;
import org.apache.camel.quarkus.core.devmode.NoShutdownStrategy;
import org.apache.camel.spi.CamelContextCustomizer;
import org.apache.camel.spi.ComponentNameResolver;
import org.apache.camel.spi.FactoryFinderResolver;
import org.apache.camel.spi.ModelJAXBContextFactory;
import org.apache.camel.spi.ModelReifierFactory;
import org.apache.camel.spi.ModelToXMLDumper;
import org.apache.camel.spi.ModelToYAMLDumper;
import org.apache.camel.spi.ModelineFactory;
import org.apache.camel.spi.PackageScanClassResolver;
import org.apache.camel.spi.Registry;
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.camel.spi.VariableRepositoryFactory;
import org.eclipse.microprofile.config.ConfigProvider;

@Recorder
public class CamelContextRecorder {
Expand Down Expand Up @@ -91,8 +96,20 @@ public void customize(RuntimeValue<CamelContext> context, RuntimeValue<CamelCont
contextCustomizer.getValue().configure(context.getValue());
}

public RuntimeValue<CamelContextCustomizer> createNoShutdownStrategyCustomizer() {
return new RuntimeValue<>(context -> context.setShutdownStrategy(new NoShutdownStrategy()));
public void customizeDevModeCamelContext(RuntimeValue<CamelContext> camelContextRuntimeValue, boolean isModeLineDslAbsent) {
CamelContext camelContext = camelContextRuntimeValue.getValue();

// If no graceful timeout is set in development mode, graceful shutdown is replaced with NoShutdownStrategy
Optional<String> shutdownTimeout = ConfigProvider.getConfig().getOptionalValue("camel.main.shutdownTimeout",
String.class);
if (shutdownTimeout.isEmpty()) {
camelContext.setShutdownStrategy(new NoShutdownStrategy());
}

if (isModeLineDslAbsent) {
// Camel dev profile attempts to enable modeline support, so we need to provide a NoOp impl if the modeline extension is not present
camelContext.getCamelContextExtension().addContextPlugin(ModelineFactory.class, new NoOpModelineFactory());
}
}

public RuntimeValue<CamelContextCustomizer> createSourceLocationEnabledCustomizer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.devmode;

import org.apache.camel.spi.ModelineFactory;
import org.apache.camel.spi.Resource;

/**
* A NoOp ModelineFactory for use in dev mode when camel-quarkus-modeline-dsl is not on the classpath.
*/
public class NoOpModelineFactory implements ModelineFactory {
@Override
public void parseModeline(Resource resource) {
// Parsing is disabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.core;
package org.apache.camel.quarkus.core.devmode;

import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.quarkus.main;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import io.quarkus.arc.runtime.BeanContainer;
Expand All @@ -33,6 +34,7 @@
import org.apache.camel.quarkus.core.CamelRuntime;
import org.apache.camel.quarkus.core.RegistryRoutesLoader;
import org.apache.camel.spi.CamelContextCustomizer;
import org.eclipse.microprofile.config.ConfigProvider;

@Recorder
public class CamelMainRecorder {
Expand Down Expand Up @@ -99,4 +101,9 @@ public RuntimeValue<CamelRuntime> createRuntime(
public void registerCamelMainEventBridge(RuntimeValue<CamelMain> main, Set<String> observedMainEvents) {
main.getValue().addMainListener(new CamelMainEventBridge(observedMainEvents));
}

public void customizeDevModeCamelMain(RuntimeValue<CamelMain> main) {
Optional<String> profile = ConfigProvider.getConfig().getOptionalValue("camel.main.profile", String.class);
main.getValue().getMainConfigurationProperties().setProfile(profile.orElse("dev"));
}
}
5 changes: 5 additions & 0 deletions extensions-jvm/dsl-modeline/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-maven-plugin</artifactId>
<configuration>
<capabilities>
<provides>org.apache.camel.dsl.modeline</provides>
</capabilities>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;
import org.apache.camel.quarkus.core.NoShutdownStrategy;
import org.apache.camel.quarkus.core.devmode.NoShutdownStrategy;
import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
Expand Down

0 comments on commit 9b6665f

Please sign in to comment.