This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27197-pulsar-batcher-builder in repository https://gitbox.apache.org/repos/asf/camel.git
commit c9d4da0b004e7da3ceab9466e88f8120a422c516 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 19 17:49:49 2026 +0100 CAMEL-27197: camel-pulsar - set batcherBuilder from DEFAULT or KEY_BASED The catalog lists DEFAULT and KEY_BASED for the batcherBuilder option, but BatcherBuilder is an interface with static fields, and no type converter existed, so the endpoint failed to resolve with either value. Add a type converter from String to BatcherBuilder; a bean reference still works. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/catalog/components/pulsar.json | 4 +- .../pulsar/PulsarTypeConvertersLoader.java | 60 +++++++++ .../org/apache/camel/component/pulsar/pulsar.json | 4 +- .../services/org/apache/camel/TypeConverterLoader | 2 + .../component/pulsar/PulsarConfiguration.java | 8 +- .../component/pulsar/PulsarTypeConverters.java | 46 +++++++ .../pulsar/PulsarProducerBatcherBuilderTest.java | 145 +++++++++++++++++++++ .../dsl/PulsarComponentBuilderFactory.java | 6 +- .../endpoint/dsl/PulsarEndpointBuilderFactory.java | 12 +- 9 files changed, 278 insertions(+), 9 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pulsar.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pulsar.json index ac0396f1cdcf..ef995075e797 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pulsar.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pulsar.json @@ -55,7 +55,7 @@ "subscriptionType": { "index": 28, "kind": "property", "displayName": "Subscription Type", "group": "consumer", "label": "consumer", "required": false, "type": "enum", "javaType": "org.apache.camel.component.pulsar.utils.consumers.SubscriptionType", "enum": [ "EXCLUSIVE", "SHARED", "FAILOVER", "KEY_SHARED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "EXCLUSIVE", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurat [...] "topicsPattern": { "index": 29, "kind": "property", "displayName": "Topics Pattern", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Whether the topic is a pattern (regular expression) that allows the consumer to subsc [...] "pulsarMessageReceiptFactory": { "index": 30, "kind": "property", "displayName": "Pulsar Message Receipt Factory", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.component.pulsar.PulsarMessageReceiptFactory", "deprecated": false, "autowired": false, "secret": false, "description": "Provide a factory to create an alternate implementation of PulsarMessageReceipt." }, - "batcherBuilder": { "index": 31, "kind": "property", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Control ba [...] + "batcherBuilder": { "index": 31, "kind": "property", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Control ba [...] "batchingEnabled": { "index": 32, "kind": "property", "displayName": "Batching Enabled", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Control whether automatic batching of messages is enabled for the producer." }, "batchingMaxMessages": { "index": 33, "kind": "property", "displayName": "Batching Max Messages", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "The maximum size to batch messages." }, "batchingMaxPublishDelayMicros": { "index": 34, "kind": "property", "displayName": "Batching Max Publish Delay Micros", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "long", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "The maximum time period within which the messages sent [...] @@ -138,7 +138,7 @@ "bridgeErrorHandler": { "index": 32, "kind": "parameter", "displayName": "Bridge Error Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming [...] "exceptionHandler": { "index": 33, "kind": "parameter", "displayName": "Exception Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.", "deprecated": false, "autowired": false, "secret": false, "description": "To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By de [...] "exchangePattern": { "index": 34, "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "enum", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ], "deprecated": false, "autowired": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." }, - "batcherBuilder": { "index": 35, "kind": "parameter", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "Con [...] + "batcherBuilder": { "index": 35, "kind": "parameter", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "Con [...] "batchingEnabled": { "index": 36, "kind": "parameter", "displayName": "Batching Enabled", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "Control whether automatic batching of messages is enabled for the producer." }, "batchingMaxMessages": { "index": 37, "kind": "parameter", "displayName": "Batching Max Messages", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "The maximum size to batch messages." }, "batchingMaxPublishDelayMicros": { "index": 38, "kind": "parameter", "displayName": "Batching Max Publish Delay Micros", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "long", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "The maximum time period within which the messag [...] diff --git a/components/camel-pulsar/src/generated/java/org/apache/camel/component/pulsar/PulsarTypeConvertersLoader.java b/components/camel-pulsar/src/generated/java/org/apache/camel/component/pulsar/PulsarTypeConvertersLoader.java new file mode 100644 index 000000000000..5c596d5fffca --- /dev/null +++ b/components/camel-pulsar/src/generated/java/org/apache/camel/component/pulsar/PulsarTypeConvertersLoader.java @@ -0,0 +1,60 @@ +/* Generated by camel build tools - do NOT edit this file! */ +package org.apache.camel.component.pulsar; + +import javax.annotation.processing.Generated; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.DeferredContextBinding; +import org.apache.camel.Exchange; +import org.apache.camel.TypeConversionException; +import org.apache.camel.TypeConverterLoaderException; +import org.apache.camel.spi.TypeConverterLoader; +import org.apache.camel.spi.TypeConverterRegistry; +import org.apache.camel.support.SimpleTypeConverter; +import org.apache.camel.support.TypeConverterSupport; +import org.apache.camel.util.DoubleMap; + +/** + * Generated by camel build tools - do NOT edit this file! + */ +@Generated("org.apache.camel.maven.packaging.TypeConverterLoaderGeneratorMojo") +@SuppressWarnings("unchecked") +@DeferredContextBinding +public final class PulsarTypeConvertersLoader implements TypeConverterLoader, CamelContextAware { + + private CamelContext camelContext; + + public PulsarTypeConvertersLoader() { + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void load(TypeConverterRegistry registry) throws TypeConverterLoaderException { + registerConverters(registry); + } + + private void registerConverters(TypeConverterRegistry registry) { + addTypeConverter(registry, org.apache.pulsar.client.api.BatcherBuilder.class, java.lang.String.class, false, + (type, exchange, value) -> { + Object answer = org.apache.camel.component.pulsar.PulsarTypeConverters.toBatcherBuilder((java.lang.String) value); + if (false && answer == null) { + answer = Void.class; + } + return answer; + }); + } + + private static void addTypeConverter(TypeConverterRegistry registry, Class<?> toType, Class<?> fromType, boolean allowNull, SimpleTypeConverter.ConversionMethod method) { + registry.addTypeConverter(toType, fromType, new SimpleTypeConverter(allowNull, method)); + } +} diff --git a/components/camel-pulsar/src/generated/resources/META-INF/org/apache/camel/component/pulsar/pulsar.json b/components/camel-pulsar/src/generated/resources/META-INF/org/apache/camel/component/pulsar/pulsar.json index ac0396f1cdcf..ef995075e797 100644 --- a/components/camel-pulsar/src/generated/resources/META-INF/org/apache/camel/component/pulsar/pulsar.json +++ b/components/camel-pulsar/src/generated/resources/META-INF/org/apache/camel/component/pulsar/pulsar.json @@ -55,7 +55,7 @@ "subscriptionType": { "index": 28, "kind": "property", "displayName": "Subscription Type", "group": "consumer", "label": "consumer", "required": false, "type": "enum", "javaType": "org.apache.camel.component.pulsar.utils.consumers.SubscriptionType", "enum": [ "EXCLUSIVE", "SHARED", "FAILOVER", "KEY_SHARED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "EXCLUSIVE", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurat [...] "topicsPattern": { "index": 29, "kind": "property", "displayName": "Topics Pattern", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Whether the topic is a pattern (regular expression) that allows the consumer to subsc [...] "pulsarMessageReceiptFactory": { "index": 30, "kind": "property", "displayName": "Pulsar Message Receipt Factory", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.component.pulsar.PulsarMessageReceiptFactory", "deprecated": false, "autowired": false, "secret": false, "description": "Provide a factory to create an alternate implementation of PulsarMessageReceipt." }, - "batcherBuilder": { "index": 31, "kind": "property", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Control ba [...] + "batcherBuilder": { "index": 31, "kind": "property", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Control ba [...] "batchingEnabled": { "index": 32, "kind": "property", "displayName": "Batching Enabled", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "Control whether automatic batching of messages is enabled for the producer." }, "batchingMaxMessages": { "index": 33, "kind": "property", "displayName": "Batching Max Messages", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "The maximum size to batch messages." }, "batchingMaxPublishDelayMicros": { "index": 34, "kind": "property", "displayName": "Batching Max Publish Delay Micros", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "long", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "configuration", "description": "The maximum time period within which the messages sent [...] @@ -138,7 +138,7 @@ "bridgeErrorHandler": { "index": 32, "kind": "parameter", "displayName": "Bridge Error Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming [...] "exceptionHandler": { "index": 33, "kind": "parameter", "displayName": "Exception Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.", "deprecated": false, "autowired": false, "secret": false, "description": "To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By de [...] "exchangePattern": { "index": 34, "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "enum", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ], "deprecated": false, "autowired": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." }, - "batcherBuilder": { "index": 35, "kind": "parameter", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "Con [...] + "batcherBuilder": { "index": 35, "kind": "parameter", "displayName": "Batcher Builder", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.pulsar.client.api.BatcherBuilder", "enum": [ "DEFAULT", "KEY_BASED" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "DEFAULT", "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "Con [...] "batchingEnabled": { "index": 36, "kind": "parameter", "displayName": "Batching Enabled", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "Control whether automatic batching of messages is enabled for the producer." }, "batchingMaxMessages": { "index": 37, "kind": "parameter", "displayName": "Batching Max Messages", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "int", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "The maximum size to batch messages." }, "batchingMaxPublishDelayMicros": { "index": 38, "kind": "parameter", "displayName": "Batching Max Publish Delay Micros", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "long", "deprecated": false, "autowired": false, "secret": false, "defaultValue": 1000, "configurationClass": "org.apache.camel.component.pulsar.PulsarConfiguration", "configurationField": "pulsarConfiguration", "description": "The maximum time period within which the messag [...] diff --git a/components/camel-pulsar/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader b/components/camel-pulsar/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader new file mode 100644 index 000000000000..c6bf2da3c560 --- /dev/null +++ b/components/camel-pulsar/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +org.apache.camel.component.pulsar.PulsarTypeConvertersLoader diff --git a/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarConfiguration.java b/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarConfiguration.java index 282c91ac405b..042ce4567a35 100644 --- a/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarConfiguration.java +++ b/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarConfiguration.java @@ -139,8 +139,12 @@ public class PulsarConfiguration implements Cloneable { @UriParam(label = "producer", description = "Control whether automatic batching of messages is enabled for the producer.", defaultValue = "true") private boolean batchingEnabled = true; - @UriParam(label = "producer", description = "Control batching method used by the producer.", defaultValue = "DEFAULT", - enums = "DEFAULT,KEY_BASED") + @UriParam(label = "producer", + description = "Control batching method used by the producer. DEFAULT batches the messages together" + + " regardless of their key. KEY_BASED batches the messages of each key separately, so that a" + + " Key_Shared subscription spreads the keys over its consumers. A custom BatcherBuilder can be" + + " given as a bean reference.", + defaultValue = "DEFAULT", enums = "DEFAULT,KEY_BASED") private BatcherBuilder batcherBuilder = BatcherBuilder.DEFAULT; @UriParam(label = "producer", description = "The first message published will have a sequence Id of initialSequenceId plus one. The" diff --git a/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarTypeConverters.java b/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarTypeConverters.java new file mode 100644 index 000000000000..bec3a554c7c7 --- /dev/null +++ b/components/camel-pulsar/src/main/java/org/apache/camel/component/pulsar/PulsarTypeConverters.java @@ -0,0 +1,46 @@ +/* + * 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.pulsar; + +import java.util.Locale; + +import org.apache.camel.Converter; +import org.apache.pulsar.client.api.BatcherBuilder; + +/** + * Type converters for the Pulsar options whose values are not Java enums. + */ +@Converter(generateLoader = true) +public final class PulsarTypeConverters { + + private PulsarTypeConverters() { + } + + /** + * Converts DEFAULT or KEY_BASED (any case) to the batcher builder of the same name in {@link BatcherBuilder}. + */ + @Converter + public static BatcherBuilder toBatcherBuilder(String value) { + String name = value.trim().toUpperCase(Locale.ROOT); + return switch (name) { + case "DEFAULT" -> BatcherBuilder.DEFAULT; + case "KEY_BASED" -> BatcherBuilder.KEY_BASED; + default -> throw new IllegalArgumentException( + "Unknown batcherBuilder: " + value + ". Use DEFAULT, KEY_BASED or a reference to a BatcherBuilder bean."); + }; + } +} diff --git a/components/camel-pulsar/src/test/java/org/apache/camel/component/pulsar/PulsarProducerBatcherBuilderTest.java b/components/camel-pulsar/src/test/java/org/apache/camel/component/pulsar/PulsarProducerBatcherBuilderTest.java new file mode 100644 index 000000000000..7b69648b2932 --- /dev/null +++ b/components/camel-pulsar/src/test/java/org/apache/camel/component/pulsar/PulsarProducerBatcherBuilderTest.java @@ -0,0 +1,145 @@ +/* + * 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.pulsar; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import org.apache.camel.CamelContext; +import org.apache.camel.ResolveEndpointFailedException; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.pulsar.client.api.BatcherBuilder; +import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.Producer; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.TypedMessageBuilder; +import org.apache.pulsar.client.impl.ProducerBuilderImpl; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Answers.RETURNS_SELF; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +/** + * The batcherBuilder option is set from the names DEFAULT and KEY_BASED, as listed in the catalog. + */ +public class PulsarProducerBatcherBuilderTest extends CamelTestSupport { + + private static final String KEY_BASED_TOPIC = "persistent://public/default/camel-batcher-key-based"; + private static final String DEFAULT_TOPIC = "persistent://public/default/camel-batcher-default"; + + private final List<ProducerBuilderImpl<byte[]>> builders = new ArrayList<>(); + + private List<ProducerBuilderImpl<byte[]>> builders(String topic) { + return builders.stream().filter(builder -> topic.equals(builder.getConf().getTopicName())).toList(); + } + + @Test + public void testKeyBasedFromTheUri() { + template.sendBody("direct:keyBased", "Hello"); + + List<ProducerBuilderImpl<byte[]>> keyBased = builders(KEY_BASED_TOPIC); + assertEquals(1, keyBased.size()); + assertSame(BatcherBuilder.KEY_BASED, keyBased.get(0).getConf().getBatcherBuilder()); + } + + @Test + public void testDefaultFromTheUri() { + template.sendBody("direct:default", "Hello"); + + List<ProducerBuilderImpl<byte[]>> defaults = builders(DEFAULT_TOPIC); + assertEquals(1, defaults.size()); + assertSame(BatcherBuilder.DEFAULT, defaults.get(0).getConf().getBatcherBuilder()); + } + + @Test + public void testNameIsNotCaseSensitive() { + PulsarEndpoint endpoint = context.getEndpoint("pulsar:" + KEY_BASED_TOPIC + "?batcherBuilder=key_based", + PulsarEndpoint.class); + assertSame(BatcherBuilder.KEY_BASED, endpoint.getPulsarConfiguration().getBatcherBuilder()); + } + + @Test + public void testDefaultWhenNotSet() { + PulsarEndpoint endpoint = context.getEndpoint("pulsar:" + DEFAULT_TOPIC + "?producerName=unset", + PulsarEndpoint.class); + assertSame(BatcherBuilder.DEFAULT, endpoint.getPulsarConfiguration().getBatcherBuilder()); + } + + @Test + public void testUnknownNameFails() { + ResolveEndpointFailedException e = assertThrows(ResolveEndpointFailedException.class, + () -> context.getEndpoint("pulsar:" + DEFAULT_TOPIC + "?batcherBuilder=BY_KEY")); + Throwable cause = e; + while (cause.getCause() != null && !(cause instanceof IllegalArgumentException)) { + cause = cause.getCause(); + } + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause); + assertTrue(iae.getMessage().contains("Unknown batcherBuilder: BY_KEY"), iae.getMessage()); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + final CamelContext context = super.createCamelContext(); + + final TypedMessageBuilder<byte[]> messageBuilder = mock(TypedMessageBuilder.class, RETURNS_SELF); + when(messageBuilder.sendAsync()).thenReturn(CompletableFuture.completedFuture(mock(MessageId.class))); + + final Producer<byte[]> pulsarProducer = mock(Producer.class); + when(pulsarProducer.newMessage()).thenReturn(messageBuilder); + + final PulsarClient pulsarClient = mock(PulsarClient.class); + when(pulsarClient.newProducer()).thenAnswer(invocation -> { + // the real builder of the Pulsar client, which keeps the configuration the producer is created with + final ProducerBuilderImpl<byte[]> builder = spy(new ProducerBuilderImpl<>(null, Schema.BYTES)); + doReturn(pulsarProducer).when(builder).create(); + builders.add(builder); + return builder; + }); + + final PulsarComponent component = new PulsarComponent(context); + component.setPulsarClient(pulsarClient); + context.addComponent("pulsar", component); + + return context; + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:keyBased") + .to("pulsar:" + KEY_BASED_TOPIC + "?producerName=camel-key-based&batcherBuilder=KEY_BASED"); + + from("direct:default") + .to("pulsar:" + DEFAULT_TOPIC + "?producerName=camel-default&batcherBuilder=DEFAULT"); + } + }; + } +} diff --git a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/PulsarComponentBuilderFactory.java b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/PulsarComponentBuilderFactory.java index dc0a540eaa1b..52f941325acb 100644 --- a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/PulsarComponentBuilderFactory.java +++ b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/PulsarComponentBuilderFactory.java @@ -607,7 +607,11 @@ public interface PulsarComponentBuilderFactory { /** - * Control batching method used by the producer. + * Control batching method used by the producer. DEFAULT batches the + * messages together regardless of their key. KEY_BASED batches the + * messages of each key separately, so that a Key_Shared subscription + * spreads the keys over its consumers. A custom BatcherBuilder can be + * given as a bean reference. * * The option is a: * <code>org.apache.pulsar.client.api.BatcherBuilder</code> diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PulsarEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PulsarEndpointBuilderFactory.java index f1adf0899fcb..69dd313f31da 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PulsarEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PulsarEndpointBuilderFactory.java @@ -1039,7 +1039,11 @@ public interface PulsarEndpointBuilderFactory { return this; } /** - * Control batching method used by the producer. + * Control batching method used by the producer. DEFAULT batches the + * messages together regardless of their key. KEY_BASED batches the + * messages of each key separately, so that a Key_Shared subscription + * spreads the keys over its consumers. A custom BatcherBuilder can be + * given as a bean reference. * * The option is a: * <code>org.apache.pulsar.client.api.BatcherBuilder</code> type. @@ -1055,7 +1059,11 @@ public interface PulsarEndpointBuilderFactory { return this; } /** - * Control batching method used by the producer. + * Control batching method used by the producer. DEFAULT batches the + * messages together regardless of their key. KEY_BASED batches the + * messages of each key separately, so that a Key_Shared subscription + * spreads the keys over its consumers. A custom BatcherBuilder can be + * given as a bean reference. * * The option will be converted to a * <code>org.apache.pulsar.client.api.BatcherBuilder</code> type.
