You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by "jeanouii (via GitHub)" <gi...@apache.org> on 2026/03/23 10:31:42 UTC

Re: [PR] [AMQ-8320] Implement DeliveryDelay and Jakarta 3.1 compliance [activemq]

jeanouii commented on code in PR #1815:
URL: https://github.com/apache/activemq/pull/1815#discussion_r2973999545


##########
activemq-client/src/main/java/org/apache/activemq/ActiveMQProducer.java:
##########
@@ -246,12 +255,23 @@ public long getTimeToLive() {
 
     @Override
     public JMSProducer setDeliveryDelay(long deliveryDelay) {
-        throw new UnsupportedOperationException("setDeliveryDelay(long) is not supported");
+        try {
+            // Tell the internal core producer about the delay
+            this.activemqMessageProducer.setDeliveryDelay(deliveryDelay);
+
+            // Update the local field in this wrapper for consistency
+            this.deliveryDelay = deliveryDelay;
+
+        } catch (JMSException e) {
+            // JMS 2.0 requires converting checked exceptions to RuntimeExceptions
+            throw JMSExceptionSupport.convertToJMSRuntimeException(e);
+        }
+        return this;
     }
 
     @Override
     public long getDeliveryDelay() {
-        throw new UnsupportedOperationException("getDeliveryDelay() is not supported");
+        return this.deliveryDelay;

Review Comment:
   Shouldn't this be implemented in a similar way as getTimeToLive() or getPriority() to avoid the NPE because of the unboxing of the Long?



##########
activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageProducer.java:
##########
@@ -326,6 +327,13 @@ public void send(Destination destination, Message message, int deliveryMode, int
             }
         }
 
+        long delay = getDeliveryDelay();
+        if (delay > 0) {
+            message.setLongProperty("AMQ_SCHEDULED_DELAY", delay);
+            long deliveryTime = System.currentTimeMillis() + delay;
+            message.setLongProperty(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY, deliveryTime);

Review Comment:
   To be checked with ActiveMQSession which sets to now without delay, correct?



##########
activemq-client/src/main/java/org/apache/activemq/ActiveMQProducer.java:
##########
@@ -90,6 +92,13 @@ public JMSProducer send(Destination destination, Message message) {
                 }
             }
 
+            // [AMQ-8320] Producer setting for deliveryDelay will override user-specified ActiveMQ Scheduled Delay property
+            if(this.deliveryDelay != null) {
+                long deliveryTimeMillis = System.currentTimeMillis() + this.deliveryDelay;

Review Comment:
   I don't think that is really necessary here. 
   For JMS 2+, the path is ActiveMQProducer.send() → ActiveMQMessageProducer.send() → ActiveMQSession.send().
   
   So applying on ActiveMQMessageProducer should cover both JMS 1 and 2+
   For now, we set a value here and we override it with a different value in ActiveMQMessageProducer. 
   
   Not a big deal functionally, but having the logic duplicated will make maintenance harder in the future and may introduce discrepancies. 



##########
activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQDeliveryDelayTest.java:
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.activemq;
+
+import jakarta.jms.Connection;
+import jakarta.jms.MessageProducer;
+import jakarta.jms.Session;
+import org.apache.activemq.command.ActiveMQMessage;
+import org.junit.Test;
+
+import static org.apache.activemq.command.DataStructureTestSupport.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class ActiveMQDeliveryDelayTest {
+
+    private final String connectionUri = "vm://localhost?broker.persistent=false";
+
+    @Test
+    public void testStrictComplianceRejectsNegativeDelay() throws Exception {
+        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
+        // Turn ON strict compliance (Jakarta 3.1 requirement)
+        factory.setStrictCompliance(true);
+
+        try (Connection conn = factory.createConnection();
+             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
+
+            MessageProducer producer = sess.createProducer(sess.createQueue("TEST.STRICT"));
+
+            try {
+                producer.setDeliveryDelay(-1000L);
+                fail("Should have thrown a JMSException for negative delay in strict mode");
+            } catch (jakarta.jms.JMSException e) {
+                // Success: Exception was thrown as required by the spec
+            }
+        }
+    }
+
+    @Test
+    public void testLegacyBehaviorAllowsNegativeDelay() throws Exception {
+        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
+        // Turn OFF strict compliance (Legacy ActiveMQ behavior)
+        factory.setStrictCompliance(false);
+
+        try (Connection conn = factory.createConnection();
+             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
+
+            MessageProducer producer = sess.createProducer(sess.createQueue("TEST.LEGACY"));
+
+            // Should NOT throw an exception
+            producer.setDeliveryDelay(-1000L);
+            assertEquals(-1000L, producer.getDeliveryDelay());
+        }
+    }
+
+    @Test
+    public void testDeliveryDelayEffectiveOnMessage() throws Exception {
+        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
+        try (Connection conn = factory.createConnection();
+             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
+
+            MessageProducer producer = sess.createProducer(sess.createQueue("TEST.EFFECTIVE"));
+            long delay = 5000L;
+            producer.setDeliveryDelay(delay);
+
+            ActiveMQMessage msg = (ActiveMQMessage) sess.createTextMessage("Hello");
+            producer.send(msg);
+
+            // Verify Broker-side scheduling property
+            assertEquals("Broker delay property missing",

Review Comment:
   Because of the session comment above, if you also check the header instead of the custom property, it might fail?
   
   To be checked



##########
activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageProducerSupport.java:
##########
@@ -56,7 +58,12 @@ public ActiveMQMessageProducerSupport(ActiveMQSession session) {
      */
     @Override
     public void setDeliveryDelay(long deliveryDelay) throws JMSException {
-        throw new UnsupportedOperationException("setDeliveryDelay() is not supported");
+        checkClosed();
+        // This should now compile after the rebase!

Review Comment:
   If it does eventually compile, please remove this comment ;-)



##########
activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageProducer.java:
##########
@@ -326,6 +327,13 @@ public void send(Destination destination, Message message, int deliveryMode, int
             }
         }
 
+        long delay = getDeliveryDelay();
+        if (delay > 0) {
+            message.setLongProperty("AMQ_SCHEDULED_DELAY", delay);

Review Comment:
   Can you use the constante like bellow ?



##########
activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQDeliveryDelayTest.java:
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.activemq;
+
+import jakarta.jms.Connection;
+import jakarta.jms.MessageProducer;
+import jakarta.jms.Session;
+import org.apache.activemq.command.ActiveMQMessage;
+import org.junit.Test;
+
+import static org.apache.activemq.command.DataStructureTestSupport.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class ActiveMQDeliveryDelayTest {
+
+    private final String connectionUri = "vm://localhost?broker.persistent=false";
+
+    @Test
+    public void testStrictComplianceRejectsNegativeDelay() throws Exception {
+        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
+        // Turn ON strict compliance (Jakarta 3.1 requirement)
+        factory.setStrictCompliance(true);
+
+        try (Connection conn = factory.createConnection();
+             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
+
+            MessageProducer producer = sess.createProducer(sess.createQueue("TEST.STRICT"));
+
+            try {
+                producer.setDeliveryDelay(-1000L);
+                fail("Should have thrown a JMSException for negative delay in strict mode");
+            } catch (jakarta.jms.JMSException e) {
+                // Success: Exception was thrown as required by the spec
+            }
+        }
+    }
+
+    @Test
+    public void testLegacyBehaviorAllowsNegativeDelay() throws Exception {
+        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
+        // Turn OFF strict compliance (Legacy ActiveMQ behavior)
+        factory.setStrictCompliance(false);
+
+        try (Connection conn = factory.createConnection();
+             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
+
+            MessageProducer producer = sess.createProducer(sess.createQueue("TEST.LEGACY"));
+
+            // Should NOT throw an exception
+            producer.setDeliveryDelay(-1000L);
+            assertEquals(-1000L, producer.getDeliveryDelay());
+        }
+    }
+
+    @Test
+    public void testDeliveryDelayEffectiveOnMessage() throws Exception {
+        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
+        try (Connection conn = factory.createConnection();
+             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
+
+            MessageProducer producer = sess.createProducer(sess.createQueue("TEST.EFFECTIVE"));
+            long delay = 5000L;
+            producer.setDeliveryDelay(delay);
+
+            ActiveMQMessage msg = (ActiveMQMessage) sess.createTextMessage("Hello");
+            producer.send(msg);
+
+            // Verify Broker-side scheduling property
+            assertEquals("Broker delay property missing",
+                    delay, msg.getLongProperty("AMQ_SCHEDULED_DELAY"));
+
+            // Verify Consumer-side visibility property (matching #1157 logic)
+            assertTrue("JMSDeliveryTime property missing or incorrect",
+                    msg.getLongProperty(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY) >= System.currentTimeMillis() + delay - 100);

Review Comment:
   In the CI, this might randomly fail under load or when parallelism is used. 
   
   You could do something like this instead
   
   ```
   final  long before = System.currentTimeMillis();
     producer.send(msg);
   
   ...
   
     long deliveryTime = msg.getLongProperty(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY);
     assertTrue(deliveryTime >= before + delay);
     assertTrue(deliveryTime <= System.currentTimeMillis() + delay);
   ```



##########
activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQDeliveryDelayTest.java:
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.activemq;
+
+import jakarta.jms.Connection;
+import jakarta.jms.MessageProducer;
+import jakarta.jms.Session;
+import org.apache.activemq.command.ActiveMQMessage;
+import org.junit.Test;
+
+import static org.apache.activemq.command.DataStructureTestSupport.assertEquals;

Review Comment:
   Wrong one??



-- 
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: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact