gnodet-bot commented on code in PR #24844: URL: https://github.com/apache/camel/pull/24844#discussion_r4014420536
########## components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryConsumer.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.io.InputStream; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +import io.apicurio.registry.rest.client.RegistryClient; +import io.apicurio.registry.rest.client.models.SearchedVersion; +import io.apicurio.registry.rest.client.models.VersionSearchResults; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.support.ScheduledPollConsumer; + +public class ApicurioRegistryConsumer extends ScheduledPollConsumer { + + private final ApicurioRegistryEndpoint endpoint; + private final ApicurioRegistryConfiguration configuration; + private volatile Long lastSeenGlobalId; + + public ApicurioRegistryConsumer(ApicurioRegistryEndpoint endpoint, Processor processor, + ApicurioRegistryConfiguration configuration) { + super(endpoint, processor); + this.endpoint = endpoint; + this.configuration = configuration; + } + + @Override + protected int poll() throws Exception { + String groupId = endpoint.getGroupId(); + String artifactId = endpoint.getArtifactId(); + + if (groupId == null || artifactId == null) { + throw new IllegalArgumentException( + "Both groupId and artifactId are required for the consumer"); + } + + RegistryClient client = endpoint.getRegistryClient(); + VersionSearchResults results = client.groups().byGroupId(groupId) + .artifacts().byArtifactId(artifactId).versions().get(); + + if (results == null || results.getVersions() == null) { + return 0; + } + + List<SearchedVersion> versions = new ArrayList<>(results.getVersions()); + versions.sort(Comparator.comparingLong(SearchedVersion::getGlobalId)); + + int count = 0; + for (SearchedVersion version : versions) { + Long globalId = version.getGlobalId(); + if (lastSeenGlobalId == null || globalId > lastSeenGlobalId) { + Exchange exchange = createExchange(true); + Message message = exchange.getIn(); + + message.setHeader(ApicurioRegistryConstants.HEADER_GROUP_ID, groupId); + message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_ID, artifactId); + message.setHeader(ApicurioRegistryConstants.HEADER_VERSION, version.getVersion()); + message.setHeader(ApicurioRegistryConstants.HEADER_GLOBAL_ID, globalId); + message.setHeader(ApicurioRegistryConstants.HEADER_CONTENT_ID, version.getContentId()); + message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_TYPE, version.getArtifactType()); + if (version.getState() != null) { + message.setHeader(ApicurioRegistryConstants.HEADER_VERSION_STATE, + version.getState().getValue()); + } + + if (configuration.isFetchContent()) { + try (InputStream content = client.groups().byGroupId(groupId).artifacts() + .byArtifactId(artifactId).versions() + .byVersionExpression(version.getVersion()).content().get()) { + message.setBody(content.readAllBytes()); + } + } else { Review Comment: ⚠️ **Missing `releaseExchange()`** — exchange pooling leak on every poll. `createExchange(true)` acquires a pooled exchange. The canonical pattern in Camel scheduled consumers (see `TimerConsumer`, `GenericFileConsumer`) is to pair every `createExchange(true)` with `releaseExchange(exchange, false)` in a `finally` block. Without it, pooled exchanges are never returned. When exchange pooling is enabled (`camel.main.exchange-factory=pooled`), this will silently exhaust the pool over time under sustained load. ```suggestion Exchange exchange = createExchange(true); try { Message message = exchange.getIn(); message.setHeader(ApicurioRegistryConstants.HEADER_GROUP_ID, groupId); message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_ID, artifactId); message.setHeader(ApicurioRegistryConstants.HEADER_VERSION, version.getVersion()); message.setHeader(ApicurioRegistryConstants.HEADER_GLOBAL_ID, globalId); message.setHeader(ApicurioRegistryConstants.HEADER_CONTENT_ID, version.getContentId()); message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_TYPE, version.getArtifactType()); if (version.getState() != null) { message.setHeader(ApicurioRegistryConstants.HEADER_VERSION_STATE, version.getState().getValue()); } if (configuration.isFetchContent()) { try (InputStream content = client.groups().byGroupId(groupId).artifacts() .byArtifactId(artifactId).versions() .byVersionExpression(version.getVersion()).content().get()) { message.setBody(content.readAllBytes()); } } else { message.setBody(version); } getProcessor().process(exchange); lastSeenGlobalId = globalId; count++; } finally { releaseExchange(exchange, false); } ``` ########## components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryComponent.java: ########## @@ -0,0 +1,66 @@ +/* + * 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 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(); + + 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]; + if (parts.length > 1 && !parts[1].isEmpty()) { + artifactId = parts[1]; + } Review Comment: **Nit:** `remaining.split("/", 2)` will set `groupId = ""` (empty string, not `null`) when the URI is `apicurio-registry:/someArtifact`. Downstream code checks `groupId == null`, so the empty-string case slips through and produces a confusing SDK error rather than a clear `IllegalArgumentException`. Normalize empty strings: ```suggestion 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]; } } ``` -- 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]
