This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch on-header in repository https://gitbox.apache.org/repos/asf/camel.git
commit 8cc00547aacd91ff56890ced1a0fa60b43e54d85 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Feb 6 13:03:29 2021 +0100 CAMEL-16102: Source code generate @InvokeOnHeader for reflection free --- .../java/org/apache/camel/spi/InvokeOnHeader.java | 35 +++++++++++----------- .../camel/support/HeaderSelectorProducer.java | 19 ++++++++++-- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/InvokeOnHeader.java b/core/camel-api/src/main/java/org/apache/camel/spi/InvokeOnHeader.java index 55008ce..57cf986 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/InvokeOnHeader.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/InvokeOnHeader.java @@ -27,22 +27,25 @@ import org.apache.camel.Message; /** * Marks a method as being invoked for a specific header value. * <p/> - * The method must have either of the following method signatures: - * - * <pre> - * void theMethodName(Message message) throws Exception; - * - * Object theMethodName(Message message) throws Exception; - * - * boolean theMethodName(Message message, AsyncCallback callback) throws Exception; - * </pre> - * - * If the method includes the {@link AsyncCallback} type, then the return value must be boolean, as part of the async - * callback contract. Throwing exceptions is optional and can be omitted. + * The method can either be executed synchronously or asynchronously (with {@link AsyncCallback}. * <p/> - * This can be used by Component implementations that uses org.apache.camel.support.HeaderSelectorProducer. - * - * This requires to use Camel maven tooling (camel-package-maven-plugin) to generate java source code + * A method is only asynchronously executed if the method has {@link AsyncCallback} as a parameter. + * In this situation then the method should not return a value (void). And its the responsible + * of the method to invoke <pre>callback.done(false)</pre> when to continue routing. + * <p/> + * Synchronous methods can either be void or return a value. If a value is returned then the value + * will be set as the response body. + * <p/> + * The method accepts the following parameters: + * <ul> + * <li>Exchange - the current exchange</li> + * <li>Message - the current message</li> + * <li>CamelContext - the camel context</li> + * <li>AsyncCallback - for asynchronous processing</li> + * <li>Object - Object or any other type is regarded as the current message body, converted to the given type</li> + * </ul> + * Component implementation producers should extend org.apache.camel.support.HeaderSelectorProducer. + * And use Camel maven tooling (camel-package-maven-plugin) to generate java source code * that selects and invokes the method at runtime. * * @see Message#getHeader(String) @@ -51,8 +54,6 @@ import org.apache.camel.Message; @Target(ElementType.METHOD) public @interface InvokeOnHeader { - // TODO: Update javadoc as parameter binding has improved - /** * Name of header. */ diff --git a/core/camel-support/src/main/java/org/apache/camel/support/HeaderSelectorProducer.java b/core/camel-support/src/main/java/org/apache/camel/support/HeaderSelectorProducer.java index 96e8bc3..ffe21ca 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/HeaderSelectorProducer.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/HeaderSelectorProducer.java @@ -32,6 +32,9 @@ import org.slf4j.LoggerFactory; /** * A selector-based producer which uses a header value to determine which processor should be invoked. + * + * @see org.apache.camel.spi.InvokeOnHeader + * @see InvokeOnHeaderStrategy */ public abstract class HeaderSelectorProducer extends DefaultAsyncProducer implements CamelContextAware { @@ -186,12 +189,10 @@ public abstract class HeaderSelectorProducer extends DefaultAsyncProducer implem } if (sync) { LOGGER.trace("Invoked @InvokeOnHeader method: {} -> {}", action, answer); + processResult(exchange, answer); } else { LOGGER.trace("Invoked @InvokeOnHeader method: {} is continuing asynchronously", action); } - if (answer != null) { - exchange.getMessage().setBody(answer); - } } catch (Exception e) { exchange.setException(e); } @@ -203,4 +204,16 @@ public abstract class HeaderSelectorProducer extends DefaultAsyncProducer implem return sync; } + /** + * Process the result. Will by default set the result as the message body. + * + * @param exchange the exchange + * @param result the result (may be null) + */ + protected void processResult(Exchange exchange, Object result) { + if (result != null) { + exchange.getMessage().setBody(result); + } + } + }