diff --git a/components/camel-kamelet/src/generated/java/org/apache/camel/component/kamelet/KameletEndpointConfigurer.java b/components/camel-kamelet/src/generated/java/org/apache/camel/component/kamelet/KameletEndpointConfigurer.java index dc0964d3a..cdbc56ea9 100644 --- a/components/camel-kamelet/src/generated/java/org/apache/camel/component/kamelet/KameletEndpointConfigurer.java +++ b/components/camel-kamelet/src/generated/java/org/apache/camel/component/kamelet/KameletEndpointConfigurer.java @@ -24,6 +24,7 @@ public class KameletEndpointConfigurer extends PropertyConfigurerSupport impleme map.put("exceptionHandler", org.apache.camel.spi.ExceptionHandler.class); map.put("exchangePattern", org.apache.camel.ExchangePattern.class); map.put("block", boolean.class); + map.put("failIfNoConsumers", boolean.class); map.put("kameletProperties", java.util.Map.class); map.put("lazyStartProducer", boolean.class); map.put("timeout", long.class); @@ -45,6 +46,8 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj case "exceptionHandler": target.setExceptionHandler(property(camelContext, org.apache.camel.spi.ExceptionHandler.class, value)); return true; case "exchangepattern": case "exchangePattern": target.setExchangePattern(property(camelContext, org.apache.camel.ExchangePattern.class, value)); return true; + case "failifnoconsumers": + case "failIfNoConsumers": target.setFailIfNoConsumers(property(camelContext, boolean.class, value)); return true; case "kameletproperties": case "kameletProperties": target.setKameletProperties(property(camelContext, java.util.Map.class, value)); return true; case "lazystartproducer": @@ -73,6 +76,8 @@ public Object getOptionValue(Object obj, String name, boolean ignoreCase) { case "exceptionHandler": return target.getExceptionHandler(); case "exchangepattern": case "exchangePattern": return target.getExchangePattern(); + case "failifnoconsumers": + case "failIfNoConsumers": return target.isFailIfNoConsumers(); case "kameletproperties": case "kameletProperties": return target.getKameletProperties(); case "lazystartproducer": diff --git a/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json b/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json index eff69118c..91854be58 100644 --- a/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json +++ b/components/camel-kamelet/src/generated/resources/org/apache/camel/component/kamelet/kamelet.json @@ -35,7 +35,8 @@ "exceptionHandler": { "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, "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 default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored." }, "exchangePattern": { "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut", "InOptionalOut" ], "deprecated": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." }, "block": { "kind": "parameter", "displayName": "Block", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": true, "description": "If sending a message to a direct endpoint which has no active consumer, then we can tell the producer to block and wait for the consumer to become active." }, - "kameletProperties": { "kind": "parameter", "displayName": "Kamelet Properties", "group": "producer", "label": "producer", "required": false, "type": "object", "javaType": "java.util.Map", "deprecated": false, "secret": false, "defaultValue": "true", "description": "Custom properties for kamelet" }, + "failIfNoConsumers": { "kind": "parameter", "displayName": "Fail If No Consumers", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": true, "description": "Whether the producer should fail by throwing an exception, when sending to a kamelet endpoint with no active consumers." }, + "kameletProperties": { "kind": "parameter", "displayName": "Kamelet Properties", "group": "producer", "label": "producer", "required": false, "type": "object", "javaType": "java.util.Map", "deprecated": false, "secret": false, "description": "Custom properties for kamelet" }, "lazyStartProducer": { "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing." }, "timeout": { "kind": "parameter", "displayName": "Timeout", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "long", "deprecated": false, "secret": false, "defaultValue": 30000, "description": "The timeout value to use if block is enabled." }, "basicPropertyBinding": { "kind": "parameter", "displayName": "Basic Property Binding", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": false, "description": "Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities" }, diff --git a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java index 7f243af5b..ea4767cd0 100644 --- a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java +++ b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java @@ -18,10 +18,10 @@ import java.net.URI; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.camel.AfterPropertiesConfigured; @@ -38,6 +38,7 @@ import org.apache.camel.support.DefaultEndpoint; import org.apache.camel.support.LifecycleStrategySupport; import org.apache.camel.support.service.ServiceHelper; +import org.apache.camel.util.StopWatch; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; import org.slf4j.Logger; @@ -54,8 +55,15 @@ public class KameletComponent extends DefaultComponent { private static final Logger LOGGER = LoggerFactory.getLogger(KameletComponent.class); - private final Map consumers; - private final LifecycleHandler lifecycleHandler; + // active consumers + private final Map consumers = new HashMap<>(); + // counter that is used for producers to keep track if any consumer was added/removed since they last checked + // this is used for optimization to avoid each producer to get consumer for each message processed + // (locking via synchronized, and then lookup in the map as the cost) + // consumers and producers are only added/removed during startup/shutdown or if routes is manually controlled + private volatile int stateCounter; + + private final LifecycleHandler lifecycleHandler = new LifecycleHandler(); @Metadata(label = "producer", defaultValue = "true") private boolean block = true; @@ -63,8 +71,6 @@ public class KameletComponent extends DefaultComponent { private long timeout = 30000L; public KameletComponent() { - this.lifecycleHandler = new LifecycleHandler(); - this.consumers = new ConcurrentHashMap<>(); } @Override @@ -194,7 +200,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map kameletProperties; - private final Map consumers; - private final String key; + @UriParam(label = "producer", defaultValue = "true") + private boolean failIfNoConsumers = true; public KameletEndpoint( String uri, KameletComponent component, String templateId, - String routeId, - Map consumers) { + String routeId) { super(uri, component); @@ -73,7 +73,6 @@ public KameletEndpoint( this.routeId = routeId; this.key = templateId + "/" + routeId; this.kameletProperties = new HashMap<>(); - this.consumers = consumers; } public boolean isBlock() { @@ -101,6 +100,18 @@ public void setTimeout(long timeout) { this.timeout = timeout; } + public boolean isFailIfNoConsumers() { + return failIfNoConsumers; + } + + /** + * Whether the producer should fail by throwing an exception, when sending to a kamelet endpoint with no active + * consumers. + */ + public void setFailIfNoConsumers(boolean failIfNoConsumers) { + this.failIfNoConsumers = failIfNoConsumers; + } + @Override public KameletComponent getComponent() { return (KameletComponent) super.getComponent(); @@ -140,58 +151,14 @@ public Map getKameletProperties() { @Override public Producer createProducer() throws Exception { - return new KameletProducer(this); + return new KameletProducer(this, key); } @Override public Consumer createConsumer(Processor processor) throws Exception { - Consumer answer = new KameletConsumer(this, processor); + Consumer answer = new KameletConsumer(this, processor, key); configureConsumer(answer); return answer; } - // ********************************* - // - // Helpers - // - // ********************************* - - void addConsumer(KameletConsumer consumer) { - synchronized (consumers) { - if (consumers.putIfAbsent(key, consumer) != null) { - throw new IllegalArgumentException( - "Cannot add a 2nd consumer to the same endpoint. Endpoint " + this + " only allows one consumer."); - } - consumers.notifyAll(); - } - } - - void removeConsumer(KameletConsumer consumer) { - synchronized (consumers) { - consumers.remove(key, consumer); - consumers.notifyAll(); - } - } - - KameletConsumer getConsumer() throws InterruptedException { - synchronized (consumers) { - KameletConsumer answer = consumers.get(key); - if (answer == null && block) { - StopWatch watch = new StopWatch(); - for (; ; ) { - answer =consumers.get(key); - if (answer != null) { - break; - } - long rem = timeout - watch.taken(); - if (rem <= 0) { - break; - } - consumers.wait(rem); - } - } - - return answer; - } - } } \ No newline at end of file diff --git a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java index 10bd42cb0..726c22dd7 100644 --- a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java +++ b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java @@ -17,49 +17,68 @@ package org.apache.camel.component.kamelet; import org.apache.camel.AsyncCallback; -import org.apache.camel.CamelExchangeException; import org.apache.camel.Exchange; import org.apache.camel.support.DefaultAsyncProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; final class KameletProducer extends DefaultAsyncProducer { - public KameletProducer(KameletEndpoint endpoint) { - super(endpoint); - } - @Override - public KameletEndpoint getEndpoint() { - return (KameletEndpoint)super.getEndpoint(); + private static final Logger LOG = LoggerFactory.getLogger(KameletProducer.class); + + private volatile KameletConsumer consumer; + private int stateCounter; + + private final KameletEndpoint endpoint; + private final KameletComponent component; + private final String key; + private final boolean block; + private final long timeout; + + public KameletProducer(KameletEndpoint endpoint, String key) { + super(endpoint); + this.endpoint = endpoint; + this.component = endpoint.getComponent(); + this.key = key; + this.block = endpoint.isBlock(); + this.timeout = endpoint.getTimeout(); } @Override public void process(Exchange exchange) throws Exception { - final KameletConsumer consumer = getEndpoint().getConsumer(); - - if (consumer != null) { - consumer.getProcessor().process(exchange); + if (consumer == null || stateCounter != component.getStateCounter()) { + stateCounter = component.getStateCounter(); + consumer = component.getConsumer(key, block, timeout); + } + if (consumer == null) { + if (endpoint.isFailIfNoConsumers()) { + throw new KameletConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange); + } else { + LOG.debug("message ignored, no consumers available on endpoint: {}", endpoint); + } } else { - exchange.setException( - new CamelExchangeException( - "No consumers available on endpoint: " + getEndpoint(), exchange) - ); + consumer.getProcessor().process(exchange); } } @Override public boolean process(Exchange exchange, AsyncCallback callback) { try { - final KameletConsumer consumer = getEndpoint().getConsumer(); - - if (consumer != null) { - return consumer.getAsyncProcessor().process(exchange, callback); - } else { - exchange.setException( - new CamelExchangeException( - "No consumers available on endpoint: " + getEndpoint(), exchange) - ); - + if (consumer == null || stateCounter != component.getStateCounter()) { + stateCounter = component.getStateCounter(); + consumer = component.getConsumer(key, block, timeout); + } + if (consumer == null) { + if (endpoint.isFailIfNoConsumers()) { + exchange.setException(new KameletConsumerNotAvailableException( + "No consumers available on endpoint: " + endpoint, exchange)); + } else { + LOG.debug("message ignored, no consumers available on endpoint: {}", endpoint); + } callback.done(true); return true; + } else { + return consumer.getAsyncProcessor().process(exchange, callback); } } catch (Exception e) { exchange.setException(e); @@ -67,4 +86,5 @@ public boolean process(Exchange exchange, AsyncCallback callback) { return true; } } + }