Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.component.kamelet;

import java.lang.reflect.Field;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.util.concurrent.ThreadType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;

import static org.assertj.core.api.Assertions.assertThat;

/**
* CAMEL-24320: Kamelet route creation must not NPE when virtual threads are enabled on JDK 25+.
* <p>
* Regression guard for {@link ContextValueFactory.ScopedValueContextValue#orElse(Object)} when the fallback is
* {@code null} (see CAMEL-24320). ScopedValue unit coverage lives in the integration path because MRJ classes are
* packaged under {@code META-INF/versions/25}.
*/
@EnabledForJreRange(min = JRE.JAVA_25)
@ResourceLock(Resources.SYSTEM_PROPERTIES)
class KameletVirtualThreadsRouteCreationTest {

private static final String VIRTUAL_THREADS_PROPERTY = "camel.threads.virtual.enabled";

private String previousVirtualThreadsProperty;

@BeforeEach
void enableVirtualThreads() throws Exception {
previousVirtualThreadsProperty = System.getProperty(VIRTUAL_THREADS_PROPERTY);
System.setProperty(VIRTUAL_THREADS_PROPERTY, "true");
resetThreadTypeField();
}

@AfterEach
void restoreVirtualThreadsProperty() throws Exception {
if (previousVirtualThreadsProperty == null) {
System.clearProperty(VIRTUAL_THREADS_PROPERTY);
} else {
System.setProperty(VIRTUAL_THREADS_PROPERTY, previousVirtualThreadsProperty);
}
resetThreadTypeField();
}

@Test
void mainStartsKameletRouteWithVirtualThreadsEnabled() {
Main main = new Main();
main.configure().withVirtualThreadsEnabled(true).addRoutesBuilder(new RouteBuilder() {
@Override
public void configure() {
from("kamelet:vt-repro-source").routeId("vt-kamelet-repro").to("mock:vt-out");
}
});
main.start();
try {
assertThat(main.getCamelContext().getRoute("vt-kamelet-repro")).isNotNull();
} finally {
main.stop();
}
}

private static void resetThreadTypeField() throws Exception {
Field field = ThreadType.class.getDeclaredField("current");
field.setAccessible(true);
field.set(null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# 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.
#

apiVersion: camel.apache.org/v1
kind: Kamelet
metadata:
name: vt-repro-source
labels:
camel.apache.org/kamelet.type: source
spec:
definition:
title: VT repro source
template:
from:
uri: timer:tick
parameters:
period: "1000"
repeatCount: "1"
delay: "-1"
steps:
- setBody:
constant: hello
- to:
uri: kamelet:sink
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.impl.engine;

import java.lang.reflect.Field;

import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.util.concurrent.ThreadType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;

import static org.assertj.core.api.Assertions.assertThat;

/**
* CAMEL-24320: {@link ExtendedCamelContext#getCreateRoute()} and {@link ExtendedCamelContext#getCreateProcessor()} must
* return null outside a binding scope (used by Kamelet endpoint init).
*/
class CreateContextValueTest {

private static final String VIRTUAL_THREADS_PROPERTY = "camel.threads.virtual.enabled";

private String previousVirtualThreadsProperty;

@Test
void getCreateRouteReturnsNullOutsideScope() {
ExtendedCamelContext extension = new DefaultCamelContext().getCamelContextExtension();

assertThat(extension.getCreateRoute()).isNull();
}

@Test
void getCreateProcessorReturnsNullOutsideScope() {
ExtendedCamelContext extension = new DefaultCamelContext().getCamelContextExtension();

assertThat(extension.getCreateProcessor()).isNull();
}

@EnabledForJreRange(min = JRE.JAVA_25)
@ResourceLock(Resources.SYSTEM_PROPERTIES)
@Test
void getCreateRouteDoesNotThrowWithVirtualThreadsEnabled() throws Exception {
enableVirtualThreads();
try {
ExtendedCamelContext extension = new DefaultCamelContext().getCamelContextExtension();

assertThat(extension.getCreateRoute()).isNull();
assertThat(extension.getCreateProcessor()).isNull();
} finally {
restoreVirtualThreadsProperty();
}
}

private void enableVirtualThreads() throws Exception {
previousVirtualThreadsProperty = System.getProperty(VIRTUAL_THREADS_PROPERTY);
System.setProperty(VIRTUAL_THREADS_PROPERTY, "true");
resetThreadTypeField();
}

private void restoreVirtualThreadsProperty() throws Exception {
if (previousVirtualThreadsProperty == null) {
System.clearProperty(VIRTUAL_THREADS_PROPERTY);
} else {
System.setProperty(VIRTUAL_THREADS_PROPERTY, previousVirtualThreadsProperty);
}
resetThreadTypeField();
}

private static void resetThreadTypeField() throws Exception {
Field field = ThreadType.class.getDeclaredField("current");
field.setAccessible(true);
field.set(null, null);
}
}
11 changes: 11 additions & 0 deletions core/camel-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ public interface ContextValue<T> {
/**
* Returns the value of this context variable for the current thread, or the given default value if no value is
* bound.
* <p>
* {@code defaultValue} may be {@code null}; implementations must return {@code null} when unbound and the caller
* passes {@code null} as the fallback (for example {@code getCreateRoute()} outside a binding scope).
*
* @param defaultValue the value to return if no value is bound
* @param defaultValue the value to return if no value is bound (may be {@code null})
* @return the current value, or {@code defaultValue} if not bound
*/
T orElse(T defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,19 @@ class ContextValueFactory {

private static final Logger LOG = LoggerFactory.getLogger(ContextValueFactory.class);

// Use lazy holder pattern to avoid resolving ThreadType before configuration is loaded
private static final class ScopedValueHolder {
static final boolean USE_SCOPED_VALUES = shouldUseScopedValues();
private static volatile boolean scopedValueUsageLogged;

static {
if (useScopedValues()) {
private static boolean useScopedValues() {
boolean use = ThreadType.current() == ThreadType.VIRTUAL;
if (!scopedValueUsageLogged) {
scopedValueUsageLogged = true;
if (use) {
LOG.info("ContextValue will use ScopedValue for virtual thread optimization");
} else {
LOG.debug("ContextValue will use ThreadLocal");
}
}

private static boolean shouldUseScopedValues() {
return ThreadType.current() == ThreadType.VIRTUAL;
}
}

private static boolean useScopedValues() {
return ScopedValueHolder.USE_SCOPED_VALUES;
return use;
}

/**
Expand Down Expand Up @@ -138,7 +132,7 @@ public T get() {

@Override
public T orElse(T defaultValue) {
return scopedValue.orElse(defaultValue);
return scopedValue.isBound() ? scopedValue.get() : defaultValue;
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions core/camel-util/src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Multi-Release: true