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 bc9fb7d869d9 CAMEL-27339: camel-jms, camel-sjms - No reply manager is 
created after the producer stopped (#30630)
bc9fb7d869d9 is described below

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

    CAMEL-27339: camel-jms, camel-sjms - No reply manager is created after the 
producer stopped (#30630)
    
    A request that passed the running check of process() before the producer
    stopped reached initReplyManager after the stop, found started=false and
    created and started a new reply manager (JMS connection, listener,
    timeout checker thread and onTimeout pool) that nothing stopped.
    
    initReplyManager checks isRunAllowed() again under the producer's lock
    (the lock BaseService.stop holds) and fails the request with
    RejectedExecutionException, as process() does for a stopped producer.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../apache/camel/component/jms/JmsProducer.java    |   5 +
 .../jms/JmsProducerStopReplyManagerTest.java       | 185 ++++++++++++++++++++
 .../apache/camel/component/sjms/SjmsProducer.java  |   5 +
 .../sjms/SjmsProducerStopReplyManagerTest.java     | 190 +++++++++++++++++++++
 4 files changed, 385 insertions(+)

diff --git 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
index 5ded35072985..a48b1217b415 100644
--- 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
+++ 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
@@ -83,6 +83,11 @@ public class JmsProducer extends DefaultAsyncProducer {
                 if (started.get()) {
                     return;
                 }
+                if (!isRunAllowed()) {
+                    // this request passed the check in process() before the 
producer was stopped: do not create a reply
+                    // manager that nothing would stop (the lock is the one of 
the producer's start and stop)
+                    throw new RejectedExecutionException("Producer is not 
running: " + this);
+                }
 
                 // must use the classloader from the application context when 
creating reply manager,
                 // as it should inherit the classloader from app context and 
not the current which may be
diff --git 
a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerStopReplyManagerTest.java
 
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerStopReplyManagerTest.java
new file mode 100644
index 000000000000..8d80b5ce575d
--- /dev/null
+++ 
b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsProducerStopReplyManagerTest.java
@@ -0,0 +1,185 @@
+/*
+ * 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.component.jms;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import jakarta.jms.Connection;
+import jakarta.jms.ConnectionFactory;
+import jakarta.jms.JMSContext;
+import jakarta.jms.JMSException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.component.jms.reply.QueueReplyManager;
+import org.apache.camel.component.jms.reply.ReplyManager;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.DefaultMessage;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * A request that passed the running check of the producer before the producer 
was stopped must not create a reply
+ * manager after the stop: nothing would stop it (its listener, temporary 
queue and threads). The reply managers are not
+ * started here, as these paths do not need a broker.
+ */
+class JmsProducerStopReplyManagerTest {
+
+    private final AtomicInteger created = new AtomicInteger();
+    private CamelContext context;
+    private JmsProducer producer;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        context = new DefaultCamelContext();
+        context.addComponent("jms", JmsComponent.jmsComponent(new 
NoBrokerConnectionFactory()));
+        context.start();
+        JmsEndpoint endpoint = 
context.getEndpoint("jms:queue:test?requestTimeout=1000", JmsEndpoint.class);
+        producer = new JmsProducer(endpoint) {
+            @Override
+            protected ReplyManager createReplyManager() {
+                created.incrementAndGet();
+                return new QueueReplyManager(context);
+            }
+
+            @Override
+            protected ReplyManager createReplyManager(String replyTo) {
+                created.incrementAndGet();
+                return new QueueReplyManager(context);
+            }
+        };
+        producer.start();
+    }
+
+    @AfterEach
+    void tearDown() {
+        producer.stop();
+        context.stop();
+    }
+
+    @Test
+    void testNoReplyManagerAfterStop() throws Exception {
+        Exchange exchange = new DefaultExchange(context, 
ExchangePattern.InOut);
+        // the request waits after the running check of the producer, where it 
reads the destination name header
+        BlockingMessage in = new BlockingMessage(context, 
JmsConstants.JMS_DESTINATION_NAME);
+        in.setBody("Hello");
+        exchange.setIn(in);
+        CountDownLatch done = new CountDownLatch(1);
+        Thread sender = new Thread(() -> producer.process(exchange, doneSync 
-> done.countDown()));
+        sender.start();
+        assertThat(in.arrived.await(10, TimeUnit.SECONDS)).isTrue();
+
+        producer.stop();
+        in.release.countDown();
+        assertThat(done.await(10, TimeUnit.SECONDS)).isTrue();
+        sender.join(10000);
+
+        assertThat(created.get()).as("reply managers created after the 
producer stopped").isZero();
+        
assertThat(exchange.getException()).isInstanceOf(RejectedExecutionException.class);
+    }
+
+    @Test
+    void testReplyManagerCreatedOnceWhileRunningAndAfterRestart() throws 
Exception {
+        // no broker: the request fails when it is sent, after the reply 
manager was created
+        assertThat(send().getException()).isNotNull();
+        assertThat(send().getException()).isNotNull();
+        assertThat(created.get()).isEqualTo(1);
+
+        producer.stop();
+        producer.start();
+        assertThat(send().getException()).isNotNull();
+        assertThat(created.get()).isEqualTo(2);
+    }
+
+    private Exchange send() throws InterruptedException {
+        Exchange exchange = new DefaultExchange(context, 
ExchangePattern.InOut);
+        exchange.getIn().setBody("Hello");
+        CountDownLatch done = new CountDownLatch(1);
+        producer.process(exchange, doneSync -> done.countDown());
+        assertThat(done.await(10, TimeUnit.SECONDS)).isTrue();
+        return exchange;
+    }
+
+    private static final class BlockingMessage extends DefaultMessage {
+        private final String blockOn;
+        private final AtomicBoolean armed = new AtomicBoolean(true);
+        private final CountDownLatch arrived = new CountDownLatch(1);
+        private final CountDownLatch release = new CountDownLatch(1);
+
+        BlockingMessage(CamelContext context, String blockOn) {
+            super(context);
+            this.blockOn = blockOn;
+        }
+
+        @Override
+        public <T> T getHeader(String name, Class<T> type) {
+            if (blockOn.equals(name) && armed.compareAndSet(true, false)) {
+                arrived.countDown();
+                try {
+                    release.await(10, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                }
+            }
+            return super.getHeader(name, type);
+        }
+    }
+
+    /**
+     * A connection factory without a broker: every connection fails.
+     */
+    private static final class NoBrokerConnectionFactory implements 
ConnectionFactory {
+        @Override
+        public Connection createConnection() throws JMSException {
+            throw new JMSException("No broker");
+        }
+
+        @Override
+        public Connection createConnection(String userName, String password) 
throws JMSException {
+            throw new JMSException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext() {
+            throw new UnsupportedOperationException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext(String userName, String password) {
+            throw new UnsupportedOperationException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext(String userName, String password, int 
sessionMode) {
+            throw new UnsupportedOperationException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext(int sessionMode) {
+            throw new UnsupportedOperationException("No broker");
+        }
+    }
+}
diff --git 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsProducer.java
 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsProducer.java
index 21d328757a24..de9f68474171 100644
--- 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsProducer.java
+++ 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsProducer.java
@@ -100,6 +100,11 @@ public class SjmsProducer extends DefaultAsyncProducer {
                 if (started.get()) {
                     return;
                 }
+                if (!isRunAllowed()) {
+                    // this request passed the check in process() before the 
producer was stopped: do not create a reply
+                    // manager that nothing would stop (the lock is the one of 
the producer's start and stop)
+                    throw new RejectedExecutionException("Producer is not 
running: " + this);
+                }
 
                 // must use the classloader from the application context when 
creating reply manager,
                 // as it should inherit the classloader from app context and 
not the current which may be
diff --git 
a/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsProducerStopReplyManagerTest.java
 
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsProducerStopReplyManagerTest.java
new file mode 100644
index 000000000000..a15691b4c325
--- /dev/null
+++ 
b/components/camel-sjms/src/test/java/org/apache/camel/component/sjms/SjmsProducerStopReplyManagerTest.java
@@ -0,0 +1,190 @@
+/*
+ * 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.component.sjms;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import jakarta.jms.Connection;
+import jakarta.jms.ConnectionFactory;
+import jakarta.jms.JMSContext;
+import jakarta.jms.JMSException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.component.sjms.reply.QueueReplyManager;
+import org.apache.camel.component.sjms.reply.ReplyManager;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.DefaultMessage;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+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.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A request that passed the running check of the producer before the producer 
was stopped must not create a reply
+ * manager after the stop: nothing would stop it (its listener, temporary 
queue and threads). The reply managers are not
+ * started here, as these paths do not need a broker.
+ */
+class SjmsProducerStopReplyManagerTest {
+
+    private final AtomicInteger created = new AtomicInteger();
+    private CamelContext context;
+    private SjmsProducer producer;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        context = new DefaultCamelContext();
+        SjmsComponent component = new SjmsComponent();
+        component.setConnectionFactory(new NoBrokerConnectionFactory());
+        context.addComponent("sjms", component);
+        context.start();
+        SjmsEndpoint endpoint = 
context.getEndpoint("sjms:queue:test?requestTimeout=1000", SjmsEndpoint.class);
+        producer = new SjmsProducer(endpoint) {
+            @Override
+            protected ReplyManager createReplyManager() {
+                created.incrementAndGet();
+                return new QueueReplyManager(context);
+            }
+
+            @Override
+            protected ReplyManager createReplyManager(String replyTo) {
+                created.incrementAndGet();
+                return new QueueReplyManager(context);
+            }
+        };
+        producer.start();
+    }
+
+    @AfterEach
+    void tearDown() {
+        producer.stop();
+        context.stop();
+    }
+
+    @Test
+    void testNoReplyManagerAfterStop() throws Exception {
+        Exchange exchange = new DefaultExchange(context, 
ExchangePattern.InOut);
+        // the request waits after the running check of the producer, where it 
reads the destination name header
+        BlockingMessage in = new BlockingMessage(context, 
SjmsConstants.JMS_DESTINATION_NAME);
+        in.setBody("Hello");
+        exchange.setIn(in);
+        CountDownLatch done = new CountDownLatch(1);
+        Thread sender = new Thread(() -> producer.process(exchange, doneSync 
-> done.countDown()));
+        sender.start();
+        assertTrue(in.arrived.await(10, TimeUnit.SECONDS));
+
+        producer.stop();
+        in.release.countDown();
+        assertTrue(done.await(10, TimeUnit.SECONDS));
+        sender.join(10000);
+
+        assertEquals(0, created.get(), "reply managers created after the 
producer stopped");
+        assertInstanceOf(RejectedExecutionException.class, 
exchange.getException());
+    }
+
+    @Test
+    void testReplyManagerCreatedOnceWhileRunningAndAfterRestart() throws 
Exception {
+        // no broker: the request fails when it is sent, after the reply 
manager was created
+        assertNotNull(send().getException());
+        assertNotNull(send().getException());
+        assertEquals(1, created.get());
+
+        producer.stop();
+        producer.start();
+        assertNotNull(send().getException());
+        assertEquals(2, created.get());
+    }
+
+    private Exchange send() throws InterruptedException {
+        Exchange exchange = new DefaultExchange(context, 
ExchangePattern.InOut);
+        exchange.getIn().setBody("Hello");
+        CountDownLatch done = new CountDownLatch(1);
+        producer.process(exchange, doneSync -> done.countDown());
+        assertTrue(done.await(10, TimeUnit.SECONDS));
+        return exchange;
+    }
+
+    private static final class BlockingMessage extends DefaultMessage {
+        private final String blockOn;
+        private final AtomicBoolean armed = new AtomicBoolean(true);
+        private final CountDownLatch arrived = new CountDownLatch(1);
+        private final CountDownLatch release = new CountDownLatch(1);
+
+        BlockingMessage(CamelContext context, String blockOn) {
+            super(context);
+            this.blockOn = blockOn;
+        }
+
+        @Override
+        public <T> T getHeader(String name, Class<T> type) {
+            if (blockOn.equals(name) && armed.compareAndSet(true, false)) {
+                arrived.countDown();
+                try {
+                    release.await(10, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                }
+            }
+            return super.getHeader(name, type);
+        }
+    }
+
+    /**
+     * A connection factory without a broker: every connection fails.
+     */
+    private static final class NoBrokerConnectionFactory implements 
ConnectionFactory {
+        @Override
+        public Connection createConnection() throws JMSException {
+            throw new JMSException("No broker");
+        }
+
+        @Override
+        public Connection createConnection(String userName, String password) 
throws JMSException {
+            throw new JMSException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext() {
+            throw new UnsupportedOperationException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext(String userName, String password) {
+            throw new UnsupportedOperationException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext(String userName, String password, int 
sessionMode) {
+            throw new UnsupportedOperationException("No broker");
+        }
+
+        @Override
+        public JMSContext createContext(int sessionMode) {
+            throw new UnsupportedOperationException("No broker");
+        }
+    }
+}

Reply via email to