jbonofre commented on code in PR #1815:
URL: https://github.com/apache/activemq/pull/1815#discussion_r2974264029
##########
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:
Agree, to be consistent with the other parts of this class, we should use a
constant here.
##########
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:
I think so 😄
##########
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:
We have something weird here. As `AMQ_SCHEDULED_DELAY` and `JMSDeliveryTime`
properties are set in both `ActiveMQProducer.send()` and
`ActiveMQMessageProducer.send()`.
As `ActiveMQProducer.send()` delegates to `ActiveMQMessageProducer.send()`,
the properties get set twice with potentially different
`System.currentTimeMillis()` values.
Imho, the logic should be in `ActiveMQMessageProducer.send()` only.
##########
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:
That's correct and a blocker to me.
As this class declares `private Long deliveryDelay = null` (boxed), here you
do auto-unboxes to `long`.
When `deliveryDelay` is `null` (the default), this will throw
`NullPointerException`.
As @jeanouii said, you have to use a similar approach as in
`getTimeToLive()` or `getPriority()` methods.
##########
activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java:
##########
@@ -636,6 +636,9 @@ public void execute() throws Exception {
}
});
}
+ if (m.propertyExists(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY)) {
+
m.setJMSDeliveryTime(m.getLongProperty(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY));
Review Comment:
Here, `JMSDeliveryTime` is restored from a message property.
This needs verification to ensure the consumer-side restoration isn't
overwritten.
##########
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!
+ if (deliveryDelay < 0 && session.connection.isStrictCompliance()) {
Review Comment:
The check is correct to validate the parameter.
However, the validation only rejects negative values when `strictCompliance`
is `true`.
When `strictCompliance` is `false`, negative delays are silently accepted
but will produce nonsensical `AMQ_SCHEDULED_DELAY` values (negative scheduling).
I suggest:
1. Always rejecting negative values (JMS spec says delay must be >= 0)
or
2. Clamping to 0
##########
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:
Yes, it smells flakiness here 😄
--
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