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 20398a25b58a CAMEL-26953: camel-core - SendProcessor, sync send and
send hook: a producer that calls the callback and then throws completes the
send once (#30001)
20398a25b58a is described below
commit 20398a25b58a8267e805ac6d0774e99a0f1bbc66
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 15 17:14:43 2026 +0530
CAMEL-26953: camel-core - SendProcessor, sync send and send hook: a
producer that calls the callback and then throws completes the send once
(#30001)
A producer that called the callback and then threw completed the exchange
twice: SendProcessor set the exception and called the callback again, the sync
send of the producer cache failed an exchange the producer already completed,
and the send hook ran afterSend twice. The send now completes once in each of
these paths, the asynchronous ProducerTemplate sends fire one sending/sent
event pair instead of two, and a transacted exchange still goes through the
sync method of the producer.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/processor/SendProcessor.java | 97 +++++++++++---
.../DefaultProducerCacheProducerThrowsTest.java | 122 +++++++++++++++++
.../org/apache/camel/processor/SendHookTest.java | 5 +-
.../processor/SendProcessorProducerThrowsTest.java | 145 +++++++++++++++++++++
.../org/apache/camel/support/SendHelperTest.java | 45 +++++++
.../java/org/apache/camel/support/SendHelper.java | 66 +++++++++-
.../camel/support/cache/DefaultProducerCache.java | 91 ++++++++++---
.../camel/support/cache/EventNotifierCallback.java | 59 ---------
design/chaos.adoc | 6 +-
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 16 +++
10 files changed, 552 insertions(+), 100 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendProcessor.java
index e3de9c00bc8e..e72802f5bac1 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendProcessor.java
@@ -17,6 +17,7 @@
package org.apache.camel.processor;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.camel.AsyncCallback;
@@ -228,25 +229,11 @@ public class SendProcessor extends BaseProcessorSupport
// optimize to only create a new callback if really needed, otherwise
we can use the provided callback as-is
AsyncCallback ac = callback;
+ SentCallback sent = null;
boolean newCallback = watch != null || existingPattern !=
target.getPattern() || variableReceive != null;
if (newCallback) {
- ac = doneSync -> {
- try {
- // result should be stored in variable instead of message
body/headers
- if (ExchangeHelper.shouldSetVariableResult(target,
variableReceive)) {
-
ExchangeHelper.setVariableFromMessageBodyAndHeaders(target, variableReceive,
- target.getMessage());
- target.getMessage().setBody(originalBody);
- target.getMessage().setHeaders(originalHeaders);
- }
- // restore previous MEP
- target.setPattern(existingPattern);
- // emit event that the exchange was sent to the endpoint
- SendHelper.notifySent(target, destination, watch);
- } finally {
- callback.done(doneSync);
- }
- };
+ sent = new SentCallback(target, existingPattern, originalBody,
originalHeaders, watch, callback);
+ ac = sent;
}
try {
// replace message body with variable
@@ -264,13 +251,87 @@ public class SendProcessor extends BaseProcessorSupport
}
return sync;
} catch (Exception throwable) {
+ if (sent != null && !sent.compareAndSet(false, true)) {
+ // the producer called the callback and then threw: the
exchange is already continued (maybe on
+ // another thread), so it is not completed a second time and
the exception is not set on it
+ LOG.warn("The producer of {} threw an exception after it
completed the exchange. This exception is ignored.",
+ destination, throwable);
+ return sent.doneSync;
+ }
exchange.setException(throwable);
- callback.done(true);
+ if (sent != null) {
+ // the producer threw before it called the callback: complete
the send as if it had called it
+ sent.complete(true);
+ } else {
+ // the callback of the caller was given to the producer as is
(no object per send on this path), so
+ // whether the producer called it before it threw is only
known by the caller
+ callback.done(true);
+ }
}
return true;
}
+ /**
+ * The callback of a send in {@link #sendUsingProducer}, when the send
needs one: stores the result in the variable,
+ * restores the exchange pattern, emits the sent event and calls the
callback of the caller, once (also when the
+ * producer calls its callback and then throws; a further call of the
callback by the producer is only passed on).
+ * One object per send, as the lambda it replaces.
+ */
+ private final class SentCallback extends AtomicBoolean implements
AsyncCallback {
+ private final Exchange target;
+ private final ExchangePattern existingPattern;
+ private final Object originalBody;
+ private final Map<String, Object> originalHeaders;
+ private final StopWatch watch;
+ private final AsyncCallback callback;
+ private volatile boolean doneSync;
+
+ private SentCallback(Exchange target, ExchangePattern existingPattern,
Object originalBody,
+ Map<String, Object> originalHeaders, StopWatch
watch, AsyncCallback callback) {
+ this.target = target;
+ this.existingPattern = existingPattern;
+ this.originalBody = originalBody;
+ this.originalHeaders = originalHeaders;
+ this.watch = watch;
+ this.callback = callback;
+ }
+
+ @Override
+ public void done(boolean doneSync) {
+ if (compareAndSet(false, true)) {
+ complete(doneSync);
+ } else {
+ // the producer calls the callback more than once, or after it
threw: the send is not completed again,
+ // and the call is passed on to the callback of the caller as
on main, so the step around this one
+ // (which ignores it) and the dev-mode checks of the route
still see it
+ LOG.warn("The producer of {} completed the exchange after the
send was completed (more than once, or after"
+ + " it threw).",
+ destination);
+ callback.done(doneSync);
+ }
+ }
+
+ private void complete(boolean doneSync) {
+ this.doneSync = doneSync;
+ try {
+ // result should be stored in variable instead of message
body/headers
+ if (ExchangeHelper.shouldSetVariableResult(target,
variableReceive)) {
+
ExchangeHelper.setVariableFromMessageBodyAndHeaders(target, variableReceive,
+ target.getMessage());
+ target.getMessage().setBody(originalBody);
+ target.getMessage().setHeaders(originalHeaders);
+ }
+ // restore previous MEP
+ target.setPattern(existingPattern);
+ // emit event that the exchange was sent to the endpoint
+ SendHelper.notifySent(target, destination, watch);
+ } finally {
+ callback.done(doneSync);
+ }
+ }
+ }
+
public String getVariableSend() {
return variableSend;
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheProducerThrowsTest.java
b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheProducerThrowsTest.java
index 319bcbb45dde..1271d65bafa1 100644
---
a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheProducerThrowsTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerCacheProducerThrowsTest.java
@@ -32,6 +32,7 @@ import org.apache.camel.Producer;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.spi.SendHook;
import org.apache.camel.support.DefaultAsyncProducer;
import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.support.EventNotifierSupport;
@@ -53,6 +54,7 @@ public class DefaultProducerCacheProducerThrowsTest extends
ContextTestSupport {
private final AtomicInteger created = new AtomicInteger();
private final AtomicInteger sending = new AtomicInteger();
private final AtomicInteger sent = new AtomicInteger();
+ private final AtomicInteger syncCalls = new AtomicInteger();
@Override
protected CamelContext createCamelContext() throws Exception {
@@ -192,6 +194,114 @@ public class DefaultProducerCacheProducerThrowsTest
extends ContextTestSupport {
}
}
+ @Test
+ public void testSyncSendCallbackThenThrowKeepsTheResult() {
+ DefaultProducerCache cache = new DefaultProducerCache(this, context,
0);
+ cache.start();
+ try {
+ Exchange exchange = newExchange("after");
+
+ Exchange out = cache.send(endpoint(), exchange, null);
+
+ assertNull(out.getException(), "The producer completed the
exchange before it threw");
+ assertEquals("Done", out.getMessage().getBody());
+ } finally {
+ cache.stop();
+ }
+ }
+
+ @Test
+ public void testSyncSendThrowBeforeCallbackFails() {
+ DefaultProducerCache cache = new DefaultProducerCache(this, context,
0);
+ cache.start();
+ try {
+ Endpoint endpoint = endpoint();
+ for (int i = 0; i < 2; i++) {
+ Exchange out = cache.send(endpoint, newExchange("before"),
null);
+ assertInstanceOf(IllegalStateException.class,
out.getException());
+ }
+ // the producer is back in the pool after each send
+ assertEquals(1, created.get());
+ // and the unit of work of each send is done
+ assertEquals(0, context.getInflightRepository().size());
+ } finally {
+ cache.stop();
+ }
+ }
+
+ @Test
+ public void testTransactedSyncSendCallsTheSyncMethodOfTheProducer() {
+ DefaultProducerCache cache = new DefaultProducerCache(this, context,
0);
+ cache.start();
+ try {
+ Exchange exchange = newExchange("ok");
+ exchange.getExchangeExtension().setTransacted(true);
+
+ Exchange out = cache.send(endpoint(), exchange, null);
+
+ // a transacted exchange is sent with process(Exchange) of the
producer, not its async method
+ assertNull(out.getException());
+ assertEquals(1, syncCalls.get());
+ assertEquals("Done (sync)", out.getMessage().getBody());
+ assertEquals(0, context.getInflightRepository().size());
+ } finally {
+ cache.stop();
+ }
+ }
+
+ @Test
+ public void testAsyncSendEmitsOneSendingAndOneSentEvent() throws Exception
{
+ DefaultProducerCache cache = new DefaultProducerCache(this, context,
0);
+ cache.setEventNotifierEnabled(true);
+ cache.start();
+ try {
+ Exchange exchange = newExchange("ok");
+ cache.asyncSendExchange(endpoint(), null, null, null, exchange,
null).get(5, TimeUnit.SECONDS);
+
+ assertNull(exchange.getException());
+ assertEquals(1, sending.get(), "one sending event per send");
+ assertEquals(1, sent.get(), "one sent event per send");
+ } finally {
+ cache.stop();
+ }
+ }
+
+ @Test
+ public void testToDCallbackThenThrowWithSendHookRoutesOnce() throws
Exception {
+ AtomicInteger afterSend = new AtomicInteger();
+ SendHook hook = new SendHook() {
+ @Override
+ public long beforeSend(Exchange exchange, Endpoint endpoint) {
+ return PROCEED;
+ }
+
+ @Override
+ public void afterSend(Exchange exchange, Endpoint endpoint) {
+ if (endpoint.getEndpointUri().startsWith("throwing:")) {
+ afterSend.incrementAndGet();
+ }
+ }
+ };
+ context.getCamelContextExtension().addSendHook(hook);
+ try {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Done");
+
+ Exchange out = template.send("direct:start", e -> {
+ e.getMessage().setHeader("mode", "after");
+ e.getMessage().setHeader("target", "producer");
+ });
+
+ // the hook does not set the exception the producer threw after it
completed, and runs after the send once
+ mock.assertIsSatisfied();
+ assertEquals(1, mock.getReceivedCounter());
+ assertNull(out.getException());
+ assertEquals(1, afterSend.get());
+ } finally {
+ context.getCamelContextExtension().removeSendHook(hook);
+ }
+ }
+
@Test
public void testToDCallbackThenThrowRoutesOnce() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
@@ -245,9 +355,21 @@ public class DefaultProducerCacheProducerThrowsTest
extends ContextTestSupport {
public Producer createProducer() {
created.incrementAndGet();
return new DefaultAsyncProducer(this) {
+ @Override
+ public void process(Exchange exchange) {
+ // the sync method, which only a transacted exchange
reaches here
+ syncCalls.incrementAndGet();
+ exchange.getMessage().setBody("Done (sync)");
+ }
+
@Override
public boolean process(Exchange exchange, AsyncCallback
callback) {
Object mode = exchange.getMessage().getHeader("mode");
+ if ("ok".equals(mode)) {
+ exchange.getMessage().setBody("Done");
+ callback.done(true);
+ return true;
+ }
if ("error".equals(mode)) {
// such as a class of the producer that is missing
throw new NoClassDefFoundError("Simulated missing
class");
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/SendHookTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/SendHookTest.java
index 0392ac3561e3..7ee9f74f22c2 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/SendHookTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/SendHookTest.java
@@ -200,9 +200,8 @@ class SendHookTest extends ContextTestSupport {
getMockEndpoint("mock:ptAsync").expectedBodiesReceived("Hello");
template.asyncSendBody("mock:ptAsync", "Hello").get(10,
TimeUnit.SECONDS);
assertMockEndpointsSatisfied();
- // the hook runs once, though this path fires the sending and the sent
event twice (as it did before the hook)
- assertEquals(List.of("sending:mock://ptAsync",
"sending:mock://ptAsync", "before:mock://ptAsync",
- "after:mock://ptAsync", "sent:mock://ptAsync",
"sent:mock://ptAsync"), calls("mock://ptAsync"));
+ // the hook runs once, and the sending and the sent event are fired
once
+ assertHookAroundSend("mock://ptAsync");
}
@Test
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/SendProcessorProducerThrowsTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/SendProcessorProducerThrowsTest.java
new file mode 100644
index 000000000000..33e95c76904f
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/SendProcessorProducerThrowsTest.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.processor;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.SendHook;
+import org.apache.camel.support.DefaultAsyncProducer;
+import org.apache.camel.support.DefaultEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * An AsyncProducer that breaks the async contract by throwing from process,
before or after it called the callback,
+ * called by the optimised {@code to} (SendProcessor) when the send needs its
own callback (here because the exchange
+ * pattern of the send differs from the one of the exchange).
+ */
+class SendProcessorProducerThrowsTest extends ContextTestSupport {
+
+ private final AtomicInteger afterSend = new AtomicInteger();
+
+ @Test
+ void testCallbackThenThrowRoutesOnce() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Done");
+
+ Exchange out = template.send("direct:start", e ->
e.getMessage().setHeader("mode", "after"));
+
+ // the rest of the route runs once, and without the exception the
producer threw after it completed
+ mock.assertIsSatisfied();
+ assertEquals(1, mock.getReceivedCounter());
+ assertNull(out.getException());
+ assertEquals(ExchangePattern.InOnly, out.getPattern());
+ }
+
+ @Test
+ void testThrowBeforeCallbackFailsAndRestoresPattern() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(0);
+
+ Exchange out = template.send("direct:start", e ->
e.getMessage().setHeader("mode", "before"));
+
+ mock.assertIsSatisfied();
+ assertInstanceOf(IllegalStateException.class, out.getException());
+ // the send is completed as when the producer sets the exception: the
exchange pattern is restored
+ assertEquals(ExchangePattern.InOnly, out.getPattern());
+ }
+
+ @Test
+ void testCallbackThenThrowWithSendHookRoutesOnce() throws Exception {
+ SendHook hook = new SendHook() {
+ @Override
+ public long beforeSend(Exchange exchange, Endpoint endpoint) {
+ return PROCEED;
+ }
+
+ @Override
+ public void afterSend(Exchange exchange, Endpoint endpoint) {
+ if (endpoint.getEndpointUri().startsWith("throwing:")) {
+ afterSend.incrementAndGet();
+ }
+ }
+ };
+ context.getCamelContextExtension().addSendHook(hook);
+ try {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Done");
+
+ Exchange out = template.send("direct:start", e ->
e.getMessage().setHeader("mode", "after"));
+
+ // the hook does not set the exception the producer threw after it
completed, and runs after the send once
+ mock.assertIsSatisfied();
+ assertEquals(1, mock.getReceivedCounter());
+ assertNull(out.getException());
+ assertEquals(1, afterSend.get());
+ } finally {
+ context.getCamelContextExtension().removeSendHook(hook);
+ }
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ context.addEndpoint("throwing:producer", new
ThrowingEndpoint());
+ from("direct:start").to(ExchangePattern.InOut,
"throwing:producer").to("mock:result");
+ }
+ };
+ }
+
+ private final class ThrowingEndpoint extends DefaultEndpoint {
+
+ ThrowingEndpoint() {
+ super("throwing:producer", null);
+ setCamelContext(context);
+ }
+
+ @Override
+ public Producer createProducer() {
+ return new DefaultAsyncProducer(this) {
+ @Override
+ public boolean process(Exchange exchange, AsyncCallback
callback) {
+ if
("after".equals(exchange.getMessage().getHeader("mode"))) {
+ exchange.getMessage().setBody("Done");
+ callback.done(true);
+ }
+ throw new IllegalStateException("Simulated producer that
throws");
+ }
+ };
+ }
+
+ @Override
+ public Consumer createConsumer(Processor processor) {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/support/SendHelperTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/SendHelperTest.java
index 65bdaed0073a..19cbc1ff07b2 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/SendHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/SendHelperTest.java
@@ -45,6 +45,7 @@ import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -261,6 +262,50 @@ class SendHelperTest extends ContextTestSupport {
assertSame(failure, exchange.getException());
}
+ /**
+ * SEND_TWICE: the producer completes the first send asynchronously (so
the thread that completed it makes the
+ * second send, which is still running) and then throws. The send goes on
asynchronously and completes once, when
+ * the second send is done; the exception is not set on the exchange.
+ */
+ @Test
+ void testSendTwiceFirstSendHandedOffThenThrowGoesOnAsynchronously() {
+ Exchange exchange = newExchange(Mode.TWICE, Kind.SYNC);
+ Completion completion = new Completion();
+ AsyncCallback[] second = new AsyncCallback[1];
+ AtomicInteger calls = new AtomicInteger();
+
+ boolean sync = SendHelper.process(exchange, endpoint, new
AsyncProcessorSupport() {
+ @Override
+ public boolean process(Exchange exchange, AsyncCallback callback) {
+ exchange.getProperty(PRODUCER_COUNT,
AtomicInteger.class).incrementAndGet();
+ if (calls.incrementAndGet() == 2) {
+ // the second send goes on asynchronously
+ second[0] = callback;
+ return false;
+ }
+ // the first send completes as if on another thread (which
then makes the second send), and then throws
+ exchange.getMessage().setBody("Reply");
+ callback.done(false);
+ throw new IllegalStateException("Simulated producer that
throws");
+ }
+ }, completion);
+
+ // the send is not completed by the throw: the second send owns it
+ assertFalse(sync);
+ assertEquals(0, completion.count.get());
+ assertNull(exchange.getException());
+ assertEquals(2, count(exchange, PRODUCER_COUNT));
+
+ exchange.getMessage().setBody("Second reply");
+ second[0].done(false);
+
+ assertEquals(1, completion.count.get());
+ assertFalse(completion.doneSync);
+ assertNull(exchange.getException());
+ assertEquals("Second reply", exchange.getMessage().getBody());
+ assertEquals(1, count(exchange, AFTER_COUNT));
+ }
+
/**
* Many concurrent sends, each with a random hook outcome and a random
producer: every send completes exactly once,
* with the value process returned, and the after hook runs once for each
before hook that returned.
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/SendHelper.java
b/core/camel-support/src/main/java/org/apache/camel/support/SendHelper.java
index dde89b360e2f..58e18b841b34 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/SendHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/SendHelper.java
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
@@ -35,6 +36,8 @@ import org.apache.camel.spi.SendHook;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.TimeSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Helper for the places in Camel that send an {@link Exchange} to the
producer of an {@link Endpoint}: the
@@ -123,7 +126,9 @@ public final class SendHelper {
* <p>
* With a hook, see {@link SendHook} for what the hook can do. An
exception the producer throws, instead of calling
* the callback, is set on the exchange, the hook's {@link
SendHook#afterSend(Exchange, Endpoint)} is called, and
- * the exception is thrown to the caller, as without a hook.
+ * the exception is thrown to the caller, as without a hook. An exception
the producer throws after it called the
+ * callback is thrown to the caller as it is: the exchange, which is
already continued, is left alone, and the hook
+ * is not called after the send a second time.
*
* @param exchange the exchange
* @param endpoint the endpoint of the producer
@@ -181,6 +186,13 @@ public final class SendHelper {
*/
private static final class HookedSend implements AsyncCallback {
+ private static final Logger LOG =
LoggerFactory.getLogger(HookedSend.class);
+ private static final AtomicIntegerFieldUpdater<HookedSend> STATE
+ = AtomicIntegerFieldUpdater.newUpdater(HookedSend.class,
"state");
+ private static final int RUNNING = 0;
+ private static final int HANDED_OFF = 1;
+ private static final int COMPLETED = 2;
+
// the one hook, or null when there are several
private final SendHook hook;
// the hooks of a composite, in the order they are asked, or null when
there is one
@@ -197,6 +209,9 @@ public final class SendHelper {
private Exception existing;
// the copy of the message for the second send, while the first one
runs
private Message again;
+ // RUNNING, then HANDED_OFF when the first of two sends completed on
another thread (which makes the second
+ // send), then COMPLETED once the send is completed (the callback, or
a throw of the producer before it)
+ private volatile int state;
private HookedSend(SendHook hook, Exchange exchange, Endpoint
endpoint, AsyncProcessor producer,
AsyncCallback callback) {
@@ -260,18 +275,37 @@ public final class SendHelper {
}
}
+ // whether the producer of the first of two sends is running (and
not the second send)
+ boolean firstOfTwo = false;
try {
if (!twice) {
return producer.process(exchange, this);
}
again = copyForSecondSend();
+ firstOfTwo = true;
if (!producer.process(exchange, this::firstSendDone)) {
// the first send goes on asynchronously: the thread that
completes it makes the second send
return false;
}
+ firstOfTwo = false;
// the first send completed on this thread, which makes the
second send
return secondSend(true);
} catch (RuntimeException e) {
+ if (!STATE.compareAndSet(this, RUNNING, COMPLETED)) {
+ if (firstOfTwo) {
+ // the producer completed the first of two sends
(HANDED_OFF) and then threw: the thread that
+ // completed it makes the second send and completes
the send, maybe still running, so this
+ // thread leaves the exchange alone and tells the
caller the send goes on asynchronously
+ LOG.warn("The producer of {} threw an exception after
it completed the first of two sends. This"
+ + " exception is ignored.",
+ endpoint, e);
+ return false;
+ }
+ // the producer called the callback and then threw: the
send is not completed again, and the
+ // exception is not set on the exchange. The caller, which
knows that its callback was called,
+ // handles it
+ throw e;
+ }
// the caller handles a producer that throws as it does
without a hook
again = null;
exchange.setException(e);
@@ -305,12 +339,26 @@ public final class SendHelper {
if (doneSync) {
return;
}
+ if (!STATE.compareAndSet(this, RUNNING, HANDED_OFF)) {
+ // the producer threw after it called this callback, and the
throw completed the send first
+ LOG.warn("The producer of {} completed the first send after
the send was completed (more than once, or"
+ + " after it threw). This is ignored.",
+ endpoint);
+ return;
+ }
try {
secondSend(false);
} catch (RuntimeException e) {
+ if (!STATE.compareAndSet(this, HANDED_OFF, COMPLETED)) {
+ // the producer called the callback of the second send and
then threw
+ LOG.warn("The producer of {} threw an exception after it
completed the exchange. This exception is"
+ + " ignored.",
+ endpoint, e);
+ return;
+ }
// the producer threw instead of calling the callback: the
send fails
exchange.setException(e);
- done(false);
+ complete(false);
}
}
@@ -395,6 +443,20 @@ public final class SendHelper {
*/
@Override
public void done(boolean doneSync) {
+ int current = state;
+ while (current != COMPLETED) {
+ if (STATE.compareAndSet(this, current, COMPLETED)) {
+ complete(doneSync);
+ return;
+ }
+ current = state;
+ }
+ // the producer calls the callback more than once, or after it
threw: the hooks are not called again, and
+ // the call is passed on to the callback of the caller, as without
a hook
+ callback.done(doneSync);
+ }
+
+ private void complete(boolean doneSync) {
afterSend();
callback.done(doneSync);
}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
b/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
index 3b476bd99bc6..b5d4719a1c2f 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java
@@ -35,6 +35,7 @@ import org.apache.camel.spi.EndpointUtilizationStatistics;
import org.apache.camel.spi.InternalProcessorFactory;
import org.apache.camel.spi.ProducerCache;
import org.apache.camel.spi.SharedInternalProcessor;
+import org.apache.camel.support.AsyncCallbackToCompletableFutureAdapter;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.DefaultEndpointUtilizationStatistics;
import org.apache.camel.support.PluginHelper;
@@ -174,8 +175,10 @@ public class DefaultProducerCache extends ServiceSupport
implements ProducerCach
watch = SendHelper.notifySending(exchange, endpoint);
}
- // invoke the synchronous method (with the send hook around
the producer if any)
- sharedInternalProcessor.process(exchange,
SendHelper.withSendHook(camelContext, producer), resultProcessor);
+ // invoke the synchronous method (with the send hook around
the producer if any); the producer is
+ // called through a SyncSend, which completes the send once
also when the producer throws
+ sharedInternalProcessor.process(exchange,
+ new SyncSend(endpoint,
SendHelper.withSendHook(camelContext, producer)), resultProcessor);
} catch (Exception e) {
// ensure exceptions is caught and set on the exchange
@@ -349,21 +352,79 @@ public class DefaultProducerCache extends ServiceSupport
implements ProducerCach
exchange.setProperty(ExchangePropertyKey.TO_ENDPOINT,
endpoint.getEndpointUri());
// send the exchange using the processor; an exception thrown by the
producer is handled by doInAsyncProducer,
- // which knows whether the producer called the callback before it threw
- EventNotifierCallback notifier = null;
- if (eventNotifierEnabled &&
camelContext.getCamelContextExtension().isEventNotificationApplicable()) {
- notifier = new EventNotifierCallback(callback, exchange, endpoint);
- callback = notifier;
+ // which knows whether the producer called the callback before it
threw. doInAsyncProducer also emits the
+ // sending and sent events of the send
+ return sharedInternalProcessor.process(exchange, callback, producer,
resultProcessor);
+ }
+
+ /**
+ * The producer of a synchronous {@link #send}: calls the producer, and
calls the callback once, also when the
+ * producer throws after it called the callback (the exception is then
logged, and not set on the exchange, as in
+ * {@link #doInAsyncProducer}) or before it (the exception is then set on
the exchange, as when the producer sets
+ * it). One object per send. An {@link Error} is thrown on as before.
+ */
+ private static final class SyncSend extends AtomicBoolean implements
AsyncProcessor, AsyncCallback {
+ private final Endpoint endpoint;
+ private final AsyncProcessor producer;
+ private AsyncCallback callback;
+ private volatile boolean doneSync;
+
+ private SyncSend(Endpoint endpoint, AsyncProcessor producer) {
+ this.endpoint = endpoint;
+ this.producer = producer;
}
- try {
- // invoke the asynchronous method
- return sharedInternalProcessor.process(exchange, callback,
producer, resultProcessor);
- } catch (Throwable e) {
- if (notifier != null) {
- // pair the sending event with a sent event, unless the
callback did already
- notifier.notifySentOnce();
+
+ @Override
+ public boolean process(Exchange exchange, AsyncCallback callback) {
+ this.callback = callback;
+ try {
+ return producer.process(exchange, this);
+ } catch (RuntimeException e) {
+ if (!compareAndSet(false, true)) {
+ // the producer called the callback and then threw: the
send is already completed
+ LOG.warn("The producer of {} threw an exception after it
completed the exchange. This exception is"
+ + " ignored.",
+ endpoint, e);
+ return doneSync;
+ }
+ exchange.setException(e);
+ callback.done(true);
+ return true;
+ }
+ }
+
+ @Override
+ public void done(boolean doneSync) {
+ if (compareAndSet(false, true)) {
+ this.doneSync = doneSync;
+ callback.done(doneSync);
+ } else {
+ // not passed on (SendProcessor.SentCallback passes it on to
the route step around it, which ignores
+ // it): here the callback runs the after advices of the shared
processor, such as the end of the unit
+ // of work, and completes the sync send, which must happen once
+ LOG.warn("The producer of {} completed the exchange after the
send was completed (more than once, or after"
+ + " it threw). This is ignored.",
+ endpoint);
}
- throw e;
+ }
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ // a transacted exchange: the sync method of the producer, as
before (no callback that can be called twice);
+ // SharedCamelInternalProcessor.processTransacted sets an
exception it throws on the exchange
+ producer.process(exchange);
+ }
+
+ @Override
+ public CompletableFuture<Exchange> processAsync(Exchange exchange) {
+ AsyncCallbackToCompletableFutureAdapter<Exchange> future = new
AsyncCallbackToCompletableFutureAdapter<>(exchange);
+ process(exchange, future);
+ return future.getFuture();
+ }
+
+ @Override
+ public String toString() {
+ return producer.toString();
}
}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/cache/EventNotifierCallback.java
b/core/camel-support/src/main/java/org/apache/camel/support/cache/EventNotifierCallback.java
deleted file mode 100644
index 6a5b834f9a23..000000000000
---
a/core/camel-support/src/main/java/org/apache/camel/support/cache/EventNotifierCallback.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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.support.cache;
-
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.apache.camel.AsyncCallback;
-import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
-import org.apache.camel.support.SendHelper;
-import org.apache.camel.util.StopWatch;
-
-/**
- * Helper class to notify on exchange sending events in async engine
- */
-class EventNotifierCallback implements AsyncCallback {
- private final AsyncCallback originalCallback;
- private final StopWatch watch;
- private final Exchange exchange;
- private final Endpoint endpoint;
- private final AtomicBoolean sent = new AtomicBoolean();
-
- public EventNotifierCallback(AsyncCallback originalCallback, Exchange
exchange,
- Endpoint endpoint) {
- this.originalCallback = originalCallback;
- this.exchange = exchange;
- this.endpoint = endpoint;
- this.watch = SendHelper.notifySending(exchange, endpoint);
- }
-
- @Override
- public void done(boolean doneSync) {
- notifySentOnce();
- originalCallback.done(doneSync);
- }
-
- /**
- * Emits the sent event, once (the producer may throw after it called the
callback).
- */
- void notifySentOnce() {
- if (sent.compareAndSet(false, true)) {
- SendHelper.notifySent(exchange, endpoint, watch);
- }
- }
-}
diff --git a/design/chaos.adoc b/design/chaos.adoc
index f37ccf2da634..028e204f3fc3 100644
--- a/design/chaos.adoc
+++ b/design/chaos.adoc
@@ -276,9 +276,9 @@ camel get chaos myapp --report
|Level |Hook |Faults
|Sends to any endpoint
-|The send hook of camel-core (CAMEL-26331): one helper that the six places
that fire `ExchangeSendingEvent` share
-(`SendProcessor`, `DefaultProducerCache` twice, `EventNotifierCallback`,
`MulticastProcessor`,
-`EventNotifierProducer`), with a hook before and after the producer.
+|The send hook of camel-core (CAMEL-26331): one helper that the five places
that fire `ExchangeSendingEvent` share
+(`SendProcessor`, `DefaultProducerCache` twice, `MulticastProcessor`,
`EventNotifierProducer`), with a hook before and
+after the producer.
|`error` (the producer is not called), `delay`, `drop` (a send that reports
success), `corrupt` (the message sent).
|Messages that enter a route
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 6acc26ae0b92..bd8b466e92fc 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -8052,6 +8052,22 @@ consumer resume from an older offset. A log that rolled
over and was cut short b
log rewrote itself after the roll-over holds fewer entries and is replayed:
the last entry of a key in it can then be
an older offset, so the consumer can process again the messages after that
offset.
+=== camel-core - a producer that calls the callback and then throws
+
+When the producer of an endpoint calls the callback of a send and then throws
(an `AsyncProducer` that breaks the
+async contract), the exception is now logged at WARN and no longer set on the
exchange, which has already continued,
+and the send is completed once. This now also applies to `to` when the send
needs a callback of its own (when event
+notification applies, when `to` sets an exchange pattern that differs from the
one of the exchange, or with
+`variableReceive`), to the synchronous sends of the `ProducerTemplate`, and to
a send with a send hook (as installed by
+camel-chaos), as it already did for `toD` and the asynchronous sends. When
such a `to` throws before it calls the
+callback, the exchange pattern is now restored and the sent event is fired, as
when the producer sets the exception on
+the exchange. A plain `to` that does not need a callback of its own (no event
notifier, such as camel-micrometer or a
+tracer, and no change of exchange pattern or `variableReceive`) keeps the old
behaviour for such a producer.
+
+The asynchronous sends of the `ProducerTemplate` now fire one
`ExchangeSendingEvent` and one `ExchangeSentEvent` per
+send; before, each event was fired twice. Tracing (camel-tracing,
camel-telemetry), which builds its spans from these
+events, now sees one sending/sent pair per asynchronous send of the
`ProducerTemplate` instead of two.
+
=== camel-core - a hook before each step of a route
A new SPI `org.apache.camel.spi.StepHook`, added with
`ExtendedCamelContext.addStepHook`, can fail or delay each step of