davsclaus commented on code in PR #24844:
URL: https://github.com/apache/camel/pull/24844#discussion_r4132778063


##########
components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryEndpoint.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.apicurioregistry;
+
+import io.apicurio.registry.client.RegistryClientFactory;
+import io.apicurio.registry.client.common.RegistryClientOptions;
+import io.apicurio.registry.rest.client.RegistryClient;
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.spi.EndpointServiceLocation;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.spi.UriPath;
+import org.apache.camel.support.ScheduledPollEndpoint;
+
+/**
+ * Manage artifacts, versions, and groups in Apicurio Registry v3.
+ */
+@UriEndpoint(firstVersion = "4.23.0", scheme = "apicurio-registry", title = 
"Apicurio Registry",
+             syntax = "apicurio-registry:groupId/artifactId",
+             category = { Category.CLOUD, Category.API }, headersClass = 
ApicurioRegistryConstants.class)
+public class ApicurioRegistryEndpoint extends ScheduledPollEndpoint implements 
EndpointServiceLocation {
+
+    @UriPath(description = "The artifact group ID")
+    private String groupId;
+
+    @UriPath(description = "The artifact ID")
+    private String artifactId;
+
+    @UriParam
+    private ApicurioRegistryConfiguration configuration;
+
+    @UriParam(label = "advanced", description = "To use a pre-configured 
RegistryClient instance")
+    private RegistryClient registryClient;
+
+    private boolean managedRegistryClient;
+
+    ApicurioRegistryEndpoint(String uri, ApicurioRegistryComponent component,
+                             ApicurioRegistryConfiguration configuration,
+                             String groupId, String artifactId) {
+        super(uri, component);
+        this.configuration = configuration;
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        return new ApicurioRegistryProducer(this, configuration);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        ApicurioRegistryConsumer consumer = new ApicurioRegistryConsumer(this, 
processor, configuration);
+        configureConsumer(consumer);
+        return consumer;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        if (registryClient == null) {
+            registryClient = createRegistryClient();
+            managedRegistryClient = true;
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        if (managedRegistryClient) {
+            registryClient = null;

Review Comment:
   The Apicurio SDK's `VertxAdapterFactory` creates a new `WebClient` (with its 
own HTTP connection pool) on the shared Vert.x for every 
`RegistryClientFactory.create()`. Before, closing the per-endpoint Vert.x 
cleaned that up; now `doStop()` only drops the reference, so every endpoint 
stop/start (route restart, `toD` endpoints evicted from the cache, 
ProducerTemplate endpoints) leaks a `WebClient` and its pooled sockets until 
the component stops. Suggest building the `WebClient` in Camel, passing it via 
`RegistryClientOptions.customWebClient(...)`, and closing it here.



##########
components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryComponent.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.apicurioregistry;
+
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import io.vertx.core.Vertx;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.DefaultComponent;
+
+@Component("apicurio-registry")
+public class ApicurioRegistryComponent extends DefaultComponent {
+
+    @Metadata(label = "advanced", description = "The component configuration")
+    private ApicurioRegistryConfiguration configuration = new 
ApicurioRegistryConfiguration();
+
+    @Metadata(label = "advanced", autowired = true,
+              description = "To use an existing Vert.x instance for the 
registry clients. If not set, the component creates"
+                            + " one shared Vert.x instance on first use and 
closes it when the component stops.")
+    private Vertx vertx;
+
+    private boolean managedVertx;
+
+    public ApicurioRegistryComponent() {
+    }
+
+    public ApicurioRegistryComponent(CamelContext context) {
+        super(context);
+    }
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+        ApicurioRegistryConfiguration config = this.configuration.copy();
+
+        String groupId = null;
+        String artifactId = null;
+        if (remaining != null && !remaining.isEmpty()) {
+            String[] parts = remaining.split("/", 2);
+            groupId = parts[0].isEmpty() ? null : parts[0];
+            if (parts.length > 1 && !parts[1].isEmpty()) {
+                artifactId = parts[1];
+            }
+        }
+
+        ApicurioRegistryEndpoint endpoint = new ApicurioRegistryEndpoint(uri, 
this, config, groupId, artifactId);
+        setProperties(endpoint, parameters);
+        return endpoint;
+    }
+
+    synchronized Vertx getOrCreateVertx() {
+        if (vertx == null) {
+            vertx = Vertx.vertx();
+            managedVertx = true;
+        }
+        return vertx;
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        Vertx toClose = null;
+        synchronized (this) {
+            if (managedVertx && vertx != null) {
+                toClose = vertx;
+                vertx = null;
+                managedVertx = false;
+            }
+        }
+        if (toClose != null) {
+            toClose.close().toCompletionStage().toCompletableFuture().get(30, 
TimeUnit.SECONDS);
+        }
+    }
+
+    public Vertx getVertx() {
+        return vertx;
+    }
+
+    public void setVertx(Vertx vertx) {

Review Comment:
   If a Vert.x is set after the component already created its own, 
`managedVertx` stays true and `doStop()` closes the user's instance. Also, 
`getVertx()`/`setVertx()` are unsynchronized while `getOrCreateVertx()` and 
`doStop()` synchronize. Suggest making this `synchronized` and setting 
`managedVertx = false`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to