This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 6c88633  Added doc for FluentProducerTemplate about cannot use both 
withBody/withProcessor at same time.
6c88633 is described below

commit 6c88633223c2c28f0eaf4b6c166964537da16dd3
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Sep 1 08:57:38 2021 +0200

    Added doc for FluentProducerTemplate about cannot use both 
withBody/withProcessor at same time.
---
 .../org/apache/camel/FluentProducerTemplate.java   | 31 ++++++++++++++++++++--
 .../impl/engine/DefaultFluentProducerTemplate.java | 18 +++++++++++++
 .../camel/builder/FluentProducerTemplateTest.java  | 17 +++++++++++-
 3 files changed, 63 insertions(+), 3 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/FluentProducerTemplate.java 
b/core/camel-api/src/main/java/org/apache/camel/FluentProducerTemplate.java
index f4de0ce..7d415b8 100644
--- a/core/camel-api/src/main/java/org/apache/camel/FluentProducerTemplate.java
+++ b/core/camel-api/src/main/java/org/apache/camel/FluentProducerTemplate.java
@@ -33,6 +33,7 @@ import org.apache.camel.util.ObjectHelper;
  * <p/>
  * The {@link FluentProducerTemplate} is <b>thread safe</b> with the 
assumption that its the same (single) thread that
  * builds the message (via the fluent methods) that also sends the message. 
<br/>
+ * <p/>
  * When using the fluent template its required to chain the methods such as:
  * 
  * <pre>
@@ -62,10 +63,12 @@ import org.apache.camel.util.ObjectHelper;
  *     fluent.send();
  * </pre>
  * <p/>
+ * You can either only use either withExchange, or withProcessor or a 
combination of withBody/withHeaders to construct
+ * the message to be sent.<br/>
+ * <p/>
  * All the methods which sends a message may throw {@link 
FailedToCreateProducerException} in case the {@link Producer}
  * could not be created. Or a {@link NoSuchEndpointException} if the endpoint 
could not be resolved. There may be other
- * related exceptions being thrown which occurs <i>before</i> the {@link 
Producer} has started sending the message.
- * <br/>
+ * related exceptions being thrown which occurs <i>before</i> the {@link 
Producer} has started sending the message.<br/>
  * <p/>
  * All the send or request methods will return the content according to this 
strategy:
  * <ul>
@@ -178,6 +181,9 @@ public interface FluentProducerTemplate extends Service {
     /**
      * Set the headers
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param headers the headers
      */
     FluentProducerTemplate withHeaders(Map<String, Object> headers);
@@ -185,6 +191,9 @@ public interface FluentProducerTemplate extends Service {
     /**
      * Set the header
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param key   the key of the header
      * @param value the value of the header
      */
@@ -201,6 +210,9 @@ public interface FluentProducerTemplate extends Service {
     /**
      * Set the message body
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param body the body
      */
     FluentProducerTemplate withBody(Object body);
@@ -208,6 +220,9 @@ public interface FluentProducerTemplate extends Service {
     /**
      * Set the message body after converting it to the given type
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param body the body
      * @param type the type which the body should be converted to
      */
@@ -249,6 +264,9 @@ public interface FluentProducerTemplate extends Service {
      *
      * When using withExchange then you must use the send method (request is 
not supported).
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param exchange the exchange
      */
     FluentProducerTemplate withExchange(Exchange exchange);
@@ -258,6 +276,9 @@ public interface FluentProducerTemplate extends Service {
      *
      * When using withExchange then you must use the send method (request is 
not supported).
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param exchangeSupplier the supplier
      */
     FluentProducerTemplate withExchange(Supplier<Exchange> exchangeSupplier);
@@ -279,6 +300,9 @@ public interface FluentProducerTemplate extends Service {
      *     .request()}
      * </pre>
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param processor the processor
      */
     FluentProducerTemplate withProcessor(Processor processor);
@@ -286,6 +310,9 @@ public interface FluentProducerTemplate extends Service {
     /**
      * Set the processorSupplier which will be invoke to get the processor to 
be used for send/request.
      *
+     * <b>Important:</b> You can either only use either withExchange, or 
withProcessor or a combination of
+     * withBody/withHeaders to construct the message to be sent.
+     *
      * @param processorSupplier the supplier
      */
     FluentProducerTemplate withProcessor(Supplier<Processor> 
processorSupplier);
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
index 182433c..d688871 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
@@ -184,6 +184,10 @@ public class DefaultFluentProducerTemplate extends 
ServiceSupport implements Flu
     public FluentProducerTemplate withHeaders(Map<String, Object> headers) {
         DefaultFluentProducerTemplate clone = checkCloned();
 
+        if (clone.processorSupplier != null) {
+            throw new IllegalArgumentException("Cannot use both withBody and 
withProcessor with FluentProducerTemplate");
+        }
+
         Map<String, Object> map = clone.headers;
         if (map == null) {
             map = new LinkedHashMap<>();
@@ -197,6 +201,10 @@ public class DefaultFluentProducerTemplate extends 
ServiceSupport implements Flu
     public FluentProducerTemplate withHeader(String key, Object value) {
         DefaultFluentProducerTemplate clone = checkCloned();
 
+        if (clone.processorSupplier != null) {
+            throw new IllegalArgumentException("Cannot use both withBody and 
withProcessor with FluentProducerTemplate");
+        }
+
         Map<String, Object> map = clone.headers;
         if (map == null) {
             map = new LinkedHashMap<>();
@@ -220,6 +228,9 @@ public class DefaultFluentProducerTemplate extends 
ServiceSupport implements Flu
     public FluentProducerTemplate withBody(Object body) {
         DefaultFluentProducerTemplate clone = checkCloned();
 
+        if (clone.processorSupplier != null) {
+            throw new IllegalArgumentException("Cannot use both withBody and 
withProcessor with FluentProducerTemplate");
+        }
         clone.body = body;
         return clone;
     }
@@ -228,6 +239,10 @@ public class DefaultFluentProducerTemplate extends 
ServiceSupport implements Flu
     public FluentProducerTemplate withBodyAs(Object body, Class<?> type) {
         DefaultFluentProducerTemplate clone = checkCloned();
 
+        if (clone.processorSupplier != null) {
+            throw new IllegalArgumentException("Cannot use both withBody and 
withProcessor with FluentProducerTemplate");
+        }
+
         Object b = type != null
                 ? clone.context.getTypeConverter().convertTo(type, body)
                 : body;
@@ -309,6 +324,9 @@ public class DefaultFluentProducerTemplate extends 
ServiceSupport implements Flu
     public FluentProducerTemplate withProcessor(final Supplier<Processor> 
processorSupplier) {
         DefaultFluentProducerTemplate clone = checkCloned();
 
+        if (clone.body != null) {
+            throw new IllegalArgumentException("Cannot use both withBody and 
withProcessor with FluentProducerTemplate");
+        }
         clone.processorSupplier = processorSupplier;
         return clone;
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
index ee46ec2..7181051 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
@@ -28,7 +28,10 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.engine.DefaultFluentProducerTemplate;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
 /**
  * Unit test for FluentProducerTemplate
@@ -236,6 +239,18 @@ public class FluentProducerTemplateTest extends 
ContextTestSupport {
     }
 
     @Test
+    public void testExceptionUsingProcessorAndBody() throws Exception {
+        try {
+            DefaultFluentProducerTemplate.on(context)
+                    .withBody("World")
+                    .withProcessor(exchange -> 
exchange.getIn().setHeader("foo", 123)).to("direct:async").send();
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    @Test
     public void testRequestExceptionUsingBody() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(0);

Reply via email to