-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[AMQ-8320] Implement DeliveryDelay and Jakarta 3.1 compliance #1815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import jakarta.jms.JMSException; | ||
| import jakarta.jms.Message; | ||
|
|
||
| import org.apache.activemq.command.ActiveMQMessage; | ||
| import org.apache.activemq.command.ActiveMQDestination; | ||
| import org.apache.activemq.command.ProducerAck; | ||
| import org.apache.activemq.command.ProducerId; | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the constante like bellow ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, to be consistent with the other parts of this class, we should use a constant here. |
||
| long deliveryTime = System.currentTimeMillis() + delay; | ||
| message.setLongProperty(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY, deliveryTime); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be checked with ActiveMQSession which sets to now without delay, correct?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have something weird here. As As Imho, the logic should be in |
||
| } | ||
|
|
||
| this.session.send(this, dest, message, deliveryMode, priority, timeToLive, disableMessageID, disableMessageTimestamp, producerWindow, sendTimeout, onComplete); | ||
|
|
||
| stats.onMessage(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,8 @@ public abstract class ActiveMQMessageProducerSupport implements MessageProducer, | |
| protected long defaultTimeToLive; | ||
| protected int sendTimeout=0; | ||
|
|
||
| private long deliveryDelay = 0; | ||
|
|
||
| public ActiveMQMessageProducerSupport(ActiveMQSession session) { | ||
| this.session = session; | ||
| disableMessageTimestamp = session.connection.isDisableTimeStampsByDefault(); | ||
|
|
@@ -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! | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it does eventually compile, please remove this comment ;-) |
||
| if (deliveryDelay < 0 && session.connection.isStrictCompliance()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check is correct to validate the parameter. I suggest:
|
||
| throw new jakarta.jms.JMSException("Delivery delay cannot be negative."); | ||
| } | ||
| this.deliveryDelay = deliveryDelay; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -68,7 +75,8 @@ public void setDeliveryDelay(long deliveryDelay) throws JMSException { | |
| */ | ||
| @Override | ||
| public long getDeliveryDelay() throws JMSException { | ||
| throw new UnsupportedOperationException("getDeliveryDelay() is not supported"); | ||
| checkClosed(); | ||
| return this.deliveryDelay; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| import jakarta.jms.ObjectMessage; | ||
| import jakarta.jms.TextMessage; | ||
|
|
||
| import org.apache.activemq.command.ActiveMQMessage; | ||
| import org.apache.activemq.util.JMSExceptionSupport; | ||
| import org.apache.activemq.util.TypeConversionSupport; | ||
|
|
||
|
|
@@ -46,6 +47,7 @@ public class ActiveMQProducer implements JMSProducer { | |
| // QoS override of defaults on a per-JMSProducer instance basis | ||
| private String correlationId = null; | ||
| private byte[] correlationIdBytes = null; | ||
| private Long deliveryDelay = null; | ||
| private Integer deliveryMode = null; | ||
| private Boolean disableMessageID = false; | ||
| private Boolean disableMessageTimestamp = false; | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that is really necessary here. So applying on ActiveMQMessageProducer should cover both JMS 1 and 2+ Not a big deal functionally, but having the logic duplicated will make maintenance harder in the future and may introduce discrepancies. |
||
| message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, this.deliveryDelay); | ||
| message.setLongProperty(ActiveMQMessage.JMS_DELIVERY_TIME_PROPERTY, deliveryTimeMillis); | ||
| } | ||
|
|
||
| activemqMessageProducer.send(destination, message, getDeliveryMode(), getPriority(), getTimeToLive(), getDisableMessageID(), getDisableMessageTimestamp(), null); | ||
| } catch (JMSException e) { | ||
| throw JMSExceptionSupport.convertToJMSRuntimeException(e); | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be implemented in a similar way as getTimeToLive() or getPriority() to avoid the NPE because of the unboxing of the Long?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct and a blocker to me. As this class declares As @jeanouii said, you have to use a similar approach as in |
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong one??
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so 😄 |
||
| 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", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the session comment above, if you also check the header instead of the custom property, it might fail? To be checked |
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the CI, this might randomly fail under load or when parallelism is used. You could do something like this instead
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it smells flakiness here 😄 |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here,
JMSDeliveryTimeis restored from a message property.This needs verification to ensure the consumer-side restoration isn't overwritten.