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 b6119e99b144 CAMEL-27330: camel-mllp - maxConcurrentConsumers resets 
the connections above the limit again (#30626)
b6119e99b144 is described below

commit b6119e99b144984ed74012b25d341ceaf34ff94b
Author: allthingssecurity <[email protected]>
AuthorDate: Mon Nov 23 14:01:08 2026 +0530

    CAMEL-27330: camel-mllp - maxConcurrentConsumers resets the connections 
above the limit again (#30626)
    
    Since CAMEL-22957 (4.18) the consumer executor is created from a thread pool
    profile without a rejected policy, so it got the default CallerRuns: a
    connection above maxConcurrentConsumers ran on the validation thread instead
    of being reset as documented. The profile now sets the Abort policy, and a
    rejected runnable is removed from consumerRunnables, as it never runs to
    remove itself. MllpMaxConcurrentConsumersTest connected its second client to
    a port where nothing listens; it now connects to the consumer.
    
    Found with a TLA+ model.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../component/mllp/MllpTcpServerConsumer.java      |  6 ++
 .../mllp/MllpMaxConcurrentConsumersTest.java       | 64 ++++++++++++++++++----
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  9 +++
 3 files changed, 68 insertions(+), 11 deletions(-)

diff --git 
a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
 
b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
index 15a602e8b9f6..53458f17a910 100644
--- 
a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
+++ 
b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
@@ -55,6 +55,7 @@ import 
org.apache.camel.processor.mllp.Hl7AcknowledgementGenerationException;
 import org.apache.camel.spi.ThreadPoolProfile;
 import org.apache.camel.support.DefaultConsumer;
 import org.apache.camel.support.ExchangeHelper;
+import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -164,6 +165,9 @@ public class MllpTcpServerConsumer extends DefaultConsumer {
         consumerProfile.setMaxQueueSize(0);
         consumerProfile.setKeepAliveTime((long) 
getConfiguration().getAcceptTimeout());
         consumerProfile.setTimeUnit(TimeUnit.MILLISECONDS);
+        // reject a connection above maxConcurrentConsumers, so startConsumer 
resets it; without a policy the default
+        // profile's CallerRuns would serve it on the validation thread, and 
the limit would not apply
+        consumerProfile.setRejectedPolicy(ThreadPoolRejectedPolicy.Abort);
         consumerExecutor = 
getEndpoint().getCamelContext().getExecutorServiceManager()
                 .newThreadPool(this, "MllpConsumer", consumerProfile);
 
@@ -255,6 +259,8 @@ public class MllpTcpServerConsumer extends DefaultConsumer {
             getEndpoint().updateLastConnectionEstablishedTicks();
         } catch (RejectedExecutionException rejectedExecutionEx) {
             log.warn("startConsumer({}) - cannot start consumer - max 
consumers already active", clientSocket);
+            // the runnable never runs, so it does not remove itself
+            consumerRunnables.remove(client);
             mllpBuffer.resetSocket(clientSocket);
         }
     }
diff --git 
a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpMaxConcurrentConsumersTest.java
 
b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpMaxConcurrentConsumersTest.java
index d2d97173d3a9..0c4dc5f61485 100644
--- 
a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpMaxConcurrentConsumersTest.java
+++ 
b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpMaxConcurrentConsumersTest.java
@@ -31,18 +31,21 @@ import org.apache.camel.test.junit6.CamelTestSupport;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Tests related to maxConcurrentConsumers configuration
  */
 public class MllpMaxConcurrentConsumersTest extends CamelTestSupport {
 
-    @RegisterExtension
-    AvailablePortFinder.Port mllpClientPort1 = AvailablePortFinder.find();
+    static final String TEST_MESSAGE
+            = 
"MSH|^~\\&|ADT|EPIC|JCAPS|CC|20160902123950|RISTECH|ADT^A08|00001|D|2.3|||||||" 
+ '\r' + '\n';
+    static final String ROUTE_ID = "mllp-max-concurrent-consumers-route";
 
     @RegisterExtension
-    AvailablePortFinder.Port mllpClientPort2 = AvailablePortFinder.find();
+    AvailablePortFinder.Port mllpClientPort1 = AvailablePortFinder.find();
 
     @RegisterExtension
     public MllpClientResource mllpClient = new MllpClientResource();
@@ -63,8 +66,9 @@ public class MllpMaxConcurrentConsumersTest extends 
CamelTestSupport {
         mllpClient.setMllpHost("localhost");
         mllpClient.setMllpPort(mllpClientPort1.getPort());
 
+        // both clients connect to the same consumer
         mllpClient2.setMllpHost("localhost");
-        mllpClient2.setMllpPort(mllpClientPort2.getPort());
+        mllpClient2.setMllpPort(mllpClientPort1.getPort());
     }
 
     @SuppressWarnings("deprecation")
@@ -98,15 +102,53 @@ public class MllpMaxConcurrentConsumersTest extends 
CamelTestSupport {
         result.expectedMessageCount(1);
 
         mllpClient.connect();
+        mllpClient.sendMessageAndWaitForAcknowledgement(TEST_MESSAGE, 10000);
 
-        String testMessage = 
"MSH|^~\\&|ADT|EPIC|JCAPS|CC|20160902123950|RISTECH|ADT^A08|00001|D|2.3|||||||" 
+ '\r' + '\n';
-        mllpClient.sendMessageAndWaitForAcknowledgement(testMessage, 10000);
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
+
+        // the second connection is accepted by the operating system, and then 
reset by the consumer: no acknowledgement
+        mllpClient2.connect();
+        assertEquals("", sendAndReceiveAcknowledgement(mllpClient2), "the 
second connection should be reset");
 
+        // the rejected connection is not kept by the consumer
+        MllpTcpServerConsumer consumer = (MllpTcpServerConsumer) 
context.getRoute(ROUTE_ID).getConsumer();
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
consumer.getConsumerRunnables().size() == 1);
         MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testConnectionAfterTheOtherOneClosedIsServed() throws 
Exception {
+        addTestRoute(1);
+        result.expectedMessageCount(2);
+
+        mllpClient.connect();
+        mllpClient.sendMessageAndWaitForAcknowledgement(TEST_MESSAGE, 10000);
+        mllpClient.close();
+
+        // once the first connection has ended, the consumer serves a new one
+        MllpTcpServerConsumer consumer = (MllpTcpServerConsumer) 
context.getRoute(ROUTE_ID).getConsumer();
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
consumer.getConsumerRunnables().isEmpty());
+        await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
+            mllpClient2.connect();
+            try {
+                
assertTrue(sendAndReceiveAcknowledgement(mllpClient2).contains("MSA|AA"));
+            } finally {
+                mllpClient2.close();
+            }
+        });
+
+        MockEndpoint.assertIsSatisfied(context, 10, TimeUnit.SECONDS);
+    }
 
-        // second connection should fail
-        assertThrows(MllpJUnitResourceException.class,
-                () -> mllpClient2.connect());
+    /**
+     * The acknowledgement, or an empty string when the consumer closed or 
reset the connection.
+     */
+    static String sendAndReceiveAcknowledgement(MllpClientResource client) 
throws Exception {
+        try {
+            return client.sendMessageAndWaitForAcknowledgement(TEST_MESSAGE, 
10000);
+        } catch (MllpJUnitResourceException e) {
+            return "";
+        }
     }
 
     void addTestRoute(int maxConcurrentConsumers) throws Exception {
@@ -114,7 +156,7 @@ public class MllpMaxConcurrentConsumersTest extends 
CamelTestSupport {
 
             @Override
             public void configure() {
-                String routeId = "mllp-max-concurrent-consumers-route";
+                String routeId = ROUTE_ID;
 
                 
fromF("mllp://%s:%d?maxConcurrentConsumers=%d&autoAck=true&connectTimeout=100&receiveTimeout=1000",
                         mllpClient.getMllpHost(), mllpClient.getMllpPort(), 
maxConcurrentConsumers)
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 31fb0f9601e0..e9ee33664122 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
@@ -855,6 +855,15 @@ Apache MINA SSHD leaves it off by default, so a small 
request, such as one that
 listing, could wait for the server to acknowledge the previous reply. The 
`org.apache.sshd.config.tcp-nodelay` system
 property no longer applies to the connections of `mina-sftp`.
 
+=== camel-mllp - maxConcurrentConsumers limits the connections again
+
+The MLLP consumer resets a new connection when `maxConcurrentConsumers` 
connections are already being served, as the
+option documents. Since Camel 4.18 the consumer thread pool ran such a 
connection on the thread that validated it
+instead, so the option had no effect. A client that opened more connections 
than `maxConcurrentConsumers` (default 5)
+now gets the extra ones reset. To serve more connections at the same time, set 
`maxConcurrentConsumers` to the number
+of connections the clients open. With virtual threads enabled and 
`maxConcurrentConsumers` above 1, the consumer pool
+has no limit, as before.
+
 === camel-atmosphere-websocket - events carry the query parameters of their 
own connection
 
 With the `events` init-param of `CamelWebSocketServlet` set to `true`, the 
open, close and error event exchanges now carry

Reply via email to