This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new c332e8c71ffc CAMEL-24822: camel-vertx-http creates its managed Vert.x
on first use, so an endpoint that starts before the component has one (#26624)
c332e8c71ffc is described below
commit c332e8c71ffc9bebb12679b0e9b718d4f6366018
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Sep 20 13:21:27 2026 +0200
CAMEL-24822: camel-vertx-http creates its managed Vert.x on first use, so
an endpoint that starts before the component has one (#26624)
A component resolved during route startup is built and initialized but not
started; vertx-http created its Vert.x in doStart only, and the endpoint's
WebClient then failed with an NPE (a rest-openapi producer without
componentName). The managed Vert.x is created on first use while the component
is new, initialized, starting or started, re-checked under the monitor doStop
uses to close it. Found with the openapi-client example of camel-jbang-examples.
---
.../component/vertx/http/VertxHttpComponent.java | 38 ++++++++----
.../http/VertxHttpComponentLazyVertxTest.java | 67 ++++++++++++++++++++++
2 files changed, 95 insertions(+), 10 deletions(-)
diff --git
a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpComponent.java
b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpComponent.java
index ef3f8c2db557..1b04d91f10af 100644
---
a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpComponent.java
+++
b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpComponent.java
@@ -71,7 +71,7 @@ public class VertxHttpComponent extends
HeaderFilterStrategyComponent
private String proxyPassword;
@Metadata(label = "advanced")
- private Vertx vertx;
+ private volatile Vertx vertx;
@Metadata(label = "advanced")
private VertxOptions vertxOptions;
@Metadata(label = "advanced")
@@ -234,26 +234,44 @@ public class VertxHttpComponent extends
HeaderFilterStrategyComponent
super.doStart();
if (vertx == null) {
- if (vertxOptions != null) {
- vertx = Vertx.vertx(vertxOptions);
- } else {
- vertx = Vertx.vertx();
- }
- managedVertx = true;
+ createManagedVertx();
+ }
+ }
+
+ private void createManagedVertx() {
+ if (vertxOptions != null) {
+ vertx = Vertx.vertx(vertxOptions);
+ } else {
+ vertx = Vertx.vertx();
}
+ managedVertx = true;
}
@Override
protected void doStop() throws Exception {
super.doStop();
- if (managedVertx && vertx != null) {
- vertx.close();
+ synchronized (this) {
+ if (managedVertx && vertx != null) {
+ vertx.close();
+ }
+ vertx = null;
}
- vertx = null;
}
public Vertx getVertx() {
+ if (vertx == null && (isNew() || isInit() || isStarting() ||
isStarted())) {
+ // an endpoint can start before this component: a component
resolved while the routes start (the
+ // rest-openapi producer picks its HTTP client at that point) is
built and initialized but not started,
+ // and its endpoint then found no Vert.x (CAMEL-24822); the
managed instance is created on first use
+ synchronized (this) {
+ // re-checked under the monitor doStop uses: a stop that won
the race must not be followed by an
+ // instance nobody closes
+ if (vertx == null && (isNew() || isInit() || isStarting() ||
isStarted())) {
+ createManagedVertx();
+ }
+ }
+ }
return vertx;
}
diff --git
a/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpComponentLazyVertxTest.java
b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpComponentLazyVertxTest.java
new file mode 100644
index 000000000000..406a4ab01541
--- /dev/null
+++
b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpComponentLazyVertxTest.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.component.vertx.http;
+
+import io.vertx.core.Vertx;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * A component resolved while the routes start is built and initialized but
not started, and an endpoint that starts
+ * before it found no Vert.x (CAMEL-24822): the managed Vert.x is created on
first use.
+ */
+public class VertxHttpComponentLazyVertxTest {
+
+ @Test
+ public void testVertxBeforeStart() throws Exception {
+ try (DefaultCamelContext context = new DefaultCamelContext()) {
+ VertxHttpComponent component = new VertxHttpComponent();
+ component.setCamelContext(context);
+ component.init();
+
+ Vertx vertx = component.getVertx();
+ assertNotNull(vertx, "a Vert.x before the component is started");
+
+ component.start();
+ assertSame(vertx, component.getVertx(), "the same Vert.x after
start");
+
+ component.stop();
+ assertNull(component.getVertx(), "no new Vert.x once stopped");
+ }
+ }
+
+ @Test
+ public void testConfiguredVertxIsKept() throws Exception {
+ Vertx own = Vertx.vertx();
+ try (DefaultCamelContext context = new DefaultCamelContext()) {
+ VertxHttpComponent component = new VertxHttpComponent();
+ component.setCamelContext(context);
+ component.setVertx(own);
+ component.init();
+ assertSame(own, component.getVertx());
+ component.start();
+ assertSame(own, component.getVertx());
+ component.stop();
+ } finally {
+ own.close();
+ }
+ }
+}