muskaan62 opened a new issue, #2849:
URL: https://github.com/apache/activemq/issues/2849

   We are using a single shared ActiveMQ Connection with multiple independent 
JMS Sessions, where each Session is used by a separate thread.
   **For example:**
   Thread 1 uses Session 1 for producing messages.
   Thread 2 uses Session 2 for consuming messages.
   Both Sessions share the same ActiveMQ Connection.
   We observed that when an operation in one thread fails with 
java.io.InterruptedIOException, the other thread also fails, even though it 
uses a separate JMS Session.
   
   **Steps to Reproduce**
   1. Create a single ActiveMQ Connection.
   3. Create two separate JMS Sessions from the same Connection.
   5. Assign one Session to a producer thread and the other to a consumer 
thread.
   7. Start both threads.
   9. Trigger an interruption/failure while the producer is waiting for a 
broker response during producer.send().
   11. Observe the consumer thread.
   
   `public class ActiveMQThread {
       private static Thread producerThread;
       private static Thread consumerThread;
       private static final AtomicBoolean producerRunning =
               new AtomicBoolean(false);
   
       private static final AtomicBoolean consumerRunning =
               new AtomicBoolean(false);
   
       public static void main(String[] args) throws InterruptedException, 
JMSException {
           Connection connection = null;
           try {
               connection = createConnection();
           } catch (JMSException | NamingException e) {
               e.printStackTrace();
           }
   
           startProducer(connection);
           startConsumer(connection);
           stopProducer();
           stopConsumer();
   
           producerThread.join();
           consumerThread.join();
   
           stopProducer();
           stopConsumer();
   
           System.out.println("Connection closing.");
           connection.close();
       }
   
       public static Connection createConnection() throws JMSException, 
NamingException {
           Properties props = new Properties();
           props.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
           props.put(Context.PROVIDER_URL, "******"); // Replace with your 
WebLogic server URL
           props.put(Context.SECURITY_PRINCIPAL, "****"); // Replace with your 
username
           props.put(Context.SECURITY_CREDENTIALS, "*****"); // Replace with 
your password
   
           Context context = null;
           try {
               context = new InitialContext(props);
           } catch (Exception e) {
               System.out.println(e.getMessage());
           }
   
           // 2. Look up Connection Factory
           ConnectionFactory connectionFactory = (ConnectionFactory) 
context.lookup("ConnectionFactory");
   
           // 3. Create a Connection
           return connectionFactory.createConnection();
       }
   
       private static void startProducer(Connection connection) {
   
           producerThread = new Thread(() -> {
   
               try {
                   Session session = connection.createSession(
                           false,
                           Session.AUTO_ACKNOWLEDGE
                   );
   
                   try {
                       Destination queue =
                               session.createQueue("DEMO.QUEUE");
   
                       MessageProducer producer =
                               session.createProducer(queue);
   
                       try {
                           int counter = 1;
   
                           while (producerRunning.get()
                                   && !Thread.currentThread().isInterrupted()) {
   
                               TextMessage message =
                                       session.createTextMessage(
                                               "Message-" + counter++
                                       );
   
                               producer.send(message);
   
                               System.out.println(
                                       "[PRODUCER] Sent: "
                                               + message.getText()
                               );
   
                               Thread.sleep(1000);
                           }
                           System.out.println("Producer thread exiting loop.");
                       } finally {
                           System.out.println("Producer thread exiting.");
                           producer.close();
                       }
   
                   } finally {
                       System.out.println("Producer session closing.");
                       session.close();
                   }
   
               } catch (JMSException e) {
                   e.printStackTrace();
   
               } catch (InterruptedException e) {
                   Thread.currentThread().interrupt();
   
               } finally {
                   producerRunning.set(false);
               }
   
           }, "Producer-Thread");
   
           producerThread.start();
       }
   
       private static void startConsumer(Connection connection) {
   
           consumerThread = new Thread(() -> {
   
               try {
                   Session session = connection.createSession(
                           false,
                           Session.AUTO_ACKNOWLEDGE
                   );
   
                   try {
                       Destination queue =
                               session.createQueue("DEMO.QUEUE");
   
                       MessageConsumer consumer =
                               session.createConsumer(queue);
   
                       try {
                           while (consumerRunning.get()
                                   && !Thread.currentThread().isInterrupted()) {
   
                               javax.jms.Message message =
                                       consumer.receive(500);
   
                               if (message instanceof TextMessage) {
                                   System.out.println(
                                           "[CONSUMER] Received: "
                                                   + ((TextMessage) message)
                                                   .getText()
                                   );
                               }
                           }
                           System.out.println("Consumer thread exiting loop.");
                       } finally {
                           System.out.println("Consumer thread exiting.");
                           consumer.close();
                       }
   
                   } finally {
                       System.out.println("Consumer session closing.");
                       session.close();
                   }
   
               } catch (JMSException e) {
                   e.printStackTrace();
   
               } finally {
                   consumerRunning.set(false);
               }
   
           }, "Consumer-Thread");
   
           consumerThread.start();
       }
   
       private static void stopConsumer() {
           System.out.println("Consumer stop requested in stopConsumer().");
           consumerRunning.set(false);
   
           Thread thread = consumerThread;
   
           if (thread != null && thread.isAlive()) {
               thread.interrupt();
   
               try {
                   thread.join(3000);
               } catch (InterruptedException e) {
                   Thread.currentThread().interrupt();
               }
           }
   
           System.out.println("Consumer stop requested.");
       }
   
       private static void stopProducer() {
           System.out.println("Producer stop requested in stopProducer().");
           producerRunning.set(false);
   
           Thread thread = producerThread;
   
           if (thread != null && thread.isAlive()) {
               thread.interrupt();
   
               try {
                   thread.join(3000);
               } catch (InterruptedException e) {
                   Thread.currentThread().interrupt();
               }
           }
   
           System.out.println("Producer stop requested.");
       }
   }`
   
   > javax.jms.JMSException: java.io.InterruptedIOException
        at 
org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:91)
        at 
org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1487)
        at 
org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1502)
        at 
org.apache.activemq.ActiveMQSession.syncSendPacket(ActiveMQSession.java:2159)
        at 
org.apache.activemq.ActiveMQMessageConsumer.<init>(ActiveMQMessageConsumer.java:294)
        at 
org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1326)
        at 
org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1269)
        at 
org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1181)
        at 
org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1153)
        at 
activemq.ActiveMQThread.lambda$startConsumer$1(ActiveMQThread.java:141)
        at java.base/java.lang.Thread.run(Thread.java:829)
   Caused by: java.io.InterruptedIOException
        at 
org.apache.activemq.transport.FutureResponse.dealWithInterrupt(FutureResponse.java:63)
        at 
org.apache.activemq.transport.FutureResponse.getResult(FutureResponse.java:51)
        at 
org.apache.activemq.transport.ResponseCorrelator.request(ResponseCorrelator.java:87)
        at 
org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1458)
        ... 9 more
   Caused by: java.lang.InterruptedException
        at 
   
java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2056)
        at 
java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2090)
        at 
java.base/java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:417)
        at 
org.apache.activemq.transport.FutureResponse.getResult(FutureResponse.java:48)
        ... 11 more
   
   
   Questions
   Is closing the entire shared transport the intended behaviour when a 
synchronous request from one Session is interrupted?
   Is there any mechanism to isolate the failure to the Session that initiated 
the interrupted request?
   Is this behaviour version-specific or affected by the Java 
runtime/environment?
   Is using separate Connections the only recommended approach when independent 
failure isolation between producer and consumer threads is required?
   
   **Expected Behaviour**
   Since the producer and consumer use separate JMS Sessions, we would like to 
understand whether a failure in one Session should affect operations performed 
by another Session.
   Ideally, a failure isolated to one Session's operation should not 
automatically cause other independent Sessions to become unusable.
   
   **Environment**
   ActiveMQ client version: 5.19.0
   Java version: 11.x
   
   


-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to