davsclaus commented on code in PR #27698:
URL: https://github.com/apache/camel/pull/27698#discussion_r4245538475
##########
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PooledExchangeFactory.java:
##########
@@ -96,13 +96,32 @@ public Exchange create(Endpoint fromEndpoint, boolean
autoRelease) {
return exchange;
}
+ /**
+ * Releases the exchange back into the pool. An exchange created with auto
release is put into the pool by its
+ * on-done task; for such an exchange this method returns true once the
exchange is done, also when the pool was
Review Comment:
Nit: returning `true` when the pool was full and the exchange was discarded
differs from the `ExchangeFactory.release` contract ("true if released into the
pool"). Nobody reads the return value today, so it is harmless, but maybe
return what `doRelease` returned when the on-done task has just run, or mention
it in the interface javadoc.
##########
core/camel-core/src/test/java/org/apache/camel/impl/engine/PooledExchangeFactoryReleaseNotRoutedTest.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.impl.engine;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.ExchangeFactoryManager;
+import org.apache.camel.spi.PooledObjectFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * A consumer that gives back an exchange it never routed (a batch cut down to
maxMessagesPerPoll, a consumer that is
+ * stopping, a failure while the message is prepared) must put it into the
pool once, so that the next two exchanges are
+ * two different objects.
+ */
+class PooledExchangeFactoryReleaseNotRoutedTest extends ContextTestSupport {
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext camelContext = super.createCamelContext();
+ ExtendedCamelContext ecc = camelContext.getCamelContextExtension();
+ ecc.setExchangeFactory(new PooledExchangeFactory());
+ ecc.getExchangeFactory().setStatisticsEnabled(true);
+ return camelContext;
+ }
+
+ @Test
+ void autoReleaseExchangeThatWasNotRoutedIsPooledOnce() {
+ Consumer consumer = context.getRoute("foo").getConsumer();
+
+ Exchange exchange = consumer.createExchange(true);
+ exchange.getMessage().setBody("not routed");
+ consumer.releaseExchange(exchange, true);
+
+ assertPooledOnce(consumer, exchange);
+ }
+
+ @Test
+ void
autoReleaseExchangeThatWasNotRoutedIsPooledOnceWhenReleasedAsNotAutoRelease() {
+ Consumer consumer = context.getRoute("foo").getConsumer();
+
+ // several consumers create their exchanges with auto release and give
back the ones they do not route with false
+ Exchange exchange = consumer.createExchange(true);
+ exchange.getMessage().setBody("not routed");
+ consumer.releaseExchange(exchange, false);
+
+ assertPooledOnce(consumer, exchange);
+ }
+
+ @Test
+ void autoReleaseExchangeThatWasRoutedIsPooledOnceWhenReleasedAgain()
throws Exception {
Review Comment:
Nit: this test can read as "releasing twice is safe". It is only safe when
the exchange was not taken from the pool again in between; otherwise `done()`
resets another message's exchange. A short comment that a double release is
still a consumer bug would help future readers.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]