-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add preserveDeliveryMode to deadLetterStrategy #1354
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -17,10 +17,13 @@ | |
package org.apache.activemq.broker.region.policy; | ||
|
||
import org.apache.activemq.ActiveMQMessageAudit; | ||
import org.apache.activemq.command.ActiveMQMessage; | ||
import org.apache.activemq.command.Message; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A strategy for choosing which destination is used for dead letter queue | ||
* messages. | ||
|
@@ -30,6 +33,7 @@ public abstract class AbstractDeadLetterStrategy implements DeadLetterStrategy { | |
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeadLetterStrategy.class); | ||
private boolean processNonPersistent = false; | ||
private boolean processExpired = true; | ||
private boolean preserveDeliveryMode = false; | ||
private boolean enableAudit = true; | ||
private long expiration; | ||
|
||
|
@@ -91,6 +95,14 @@ public void setProcessNonPersistent(boolean processNonPersistent) { | |
this.processNonPersistent = processNonPersistent; | ||
} | ||
|
||
@Override | ||
public boolean isPreserveDeliveryMode() { return this.preserveDeliveryMode; } | ||
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. It should have the same format as other getter for consistency. So return at new new line 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. Strange, my IDE(ItelliJ) made the other getters in |
||
|
||
@Override | ||
public void setPreserveDeliveryMode(boolean preserveDeliveryMode) { | ||
this.preserveDeliveryMode = preserveDeliveryMode; | ||
} | ||
|
||
public boolean isEnableAudit() { | ||
return enableAudit; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/** | ||
* 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.broker.policy; | ||
|
||
import jakarta.jms.*; | ||
import org.apache.activemq.ActiveMQConnection; | ||
import org.apache.activemq.broker.BrokerService; | ||
import org.apache.activemq.broker.region.policy.*; | ||
import org.apache.activemq.command.ActiveMQQueue; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DeadLetterPersistenceTest extends DeadLetterTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(DiscardingDeadLetterPolicyTest.class); | ||
private static final String CLIENT_ID = "clientID"; | ||
private static final String NON_PERSISTENT_DEST = "nonPersistentDest"; | ||
private static final String PRESERVE_DELIVERY_DEST = "preserveDeliveryDest"; | ||
|
||
@Override | ||
protected BrokerService createBroker() throws Exception { | ||
BrokerService broker = super.createBroker(); | ||
|
||
PolicyEntry policy = new PolicyEntry(); | ||
IndividualDeadLetterStrategy strategy = new IndividualDeadLetterStrategy(); | ||
strategy.setProcessNonPersistent(true); | ||
strategy.setDestinationPerDurableSubscriber(true); | ||
policy.setDeadLetterStrategy(strategy); | ||
|
||
PolicyMap pMap = new PolicyMap(); | ||
pMap.setDefaultEntry(policy); | ||
|
||
SharedDeadLetterStrategy processNonPersistent = new SharedDeadLetterStrategy(); | ||
processNonPersistent.setDeadLetterQueue(new ActiveMQQueue("DLQ." + NON_PERSISTENT_DEST)); | ||
processNonPersistent.setProcessNonPersistent(true); | ||
PolicyEntry processNonPersistentDlqPolicy = new PolicyEntry(); | ||
processNonPersistentDlqPolicy.setDeadLetterStrategy(processNonPersistent); | ||
|
||
pMap.put(new ActiveMQQueue(NON_PERSISTENT_DEST), processNonPersistentDlqPolicy); | ||
|
||
SharedDeadLetterStrategy processPreserveDelivery = new SharedDeadLetterStrategy(); | ||
processPreserveDelivery.setDeadLetterQueue(new ActiveMQQueue("DLQ." + PRESERVE_DELIVERY_DEST)); | ||
processPreserveDelivery.setProcessNonPersistent(true); | ||
processPreserveDelivery.setPreserveDeliveryMode(true); | ||
PolicyEntry processPreserveDeliveryDlqPolicy = new PolicyEntry(); | ||
processPreserveDeliveryDlqPolicy.setDeadLetterStrategy(processPreserveDelivery); | ||
|
||
pMap.put(new ActiveMQQueue(PRESERVE_DELIVERY_DEST), processPreserveDeliveryDlqPolicy); | ||
|
||
broker.setDestinationPolicy(pMap); | ||
|
||
return broker; | ||
} | ||
|
||
@Override | ||
protected String createClientId() { | ||
return CLIENT_ID; | ||
} | ||
|
||
@Override | ||
protected Destination createDlqDestination() { | ||
String prefix = topic ? "ActiveMQ.DLQ.Topic." : "ActiveMQ.DLQ.Queue."; | ||
String destinationName = prefix + getClass().getName() + "." + getName(); | ||
if (durableSubscriber) { | ||
String subName = // connectionId:SubName | ||
CLIENT_ID + ":" + getDestination().toString(); | ||
destinationName += "." + subName ; | ||
} | ||
return new ActiveMQQueue(destinationName); | ||
} | ||
|
||
@Override | ||
protected void doTest() throws Exception { | ||
validateMessagePersistentSetToTrueWhenProducerIsPersistent(); | ||
validateMessagePersistentSetToTrueWhenProducerIsNonPeristent(); | ||
validateMessagePersitentNotSetWhenPreserveDeliveryModeIsTrue(); | ||
} | ||
|
||
public void validateMessagePersistentSetToTrueWhenProducerIsPersistent() throws Exception { | ||
messageCount = 1; | ||
connection.start(); | ||
|
||
ActiveMQConnection amqConnection = (ActiveMQConnection) connection; | ||
rollbackCount = amqConnection.getRedeliveryPolicy().getMaximumRedeliveries() + 1; | ||
|
||
makeConsumer(); | ||
makeDlqConsumer(); | ||
sendMessages(); | ||
|
||
for (int i = 0; i < messageCount; i++) { | ||
consumeAndRollback(i); | ||
} | ||
|
||
for (int i = 0; i < messageCount; i++) { | ||
Message msg = dlqConsumer.receive(1000); | ||
assertNotNull("Should be a DLQ message for loop: " + i, msg); | ||
org.apache.activemq.command.Message commandMsg = (org.apache.activemq.command.Message ) msg; | ||
assertTrue(commandMsg.isPersistent()); | ||
} | ||
|
||
session.commit(); | ||
} | ||
|
||
public void validateMessagePersistentSetToTrueWhenProducerIsNonPeristent() throws Exception { | ||
messageCount = 1; | ||
destination = new ActiveMQQueue(NON_PERSISTENT_DEST); | ||
durableSubscriber = false; | ||
deliveryMode = DeliveryMode.NON_PERSISTENT; | ||
connection.start(); | ||
|
||
ActiveMQConnection amqConnection = (ActiveMQConnection) connection; | ||
rollbackCount = amqConnection.getRedeliveryPolicy().getMaximumRedeliveries() + 1; | ||
|
||
makeConsumer(); | ||
makeDlqConsumer(); | ||
sendMessages(); | ||
|
||
for (int i = 0; i < messageCount; i++) { | ||
consumeAndRollback(i); | ||
} | ||
|
||
dlqDestination = new ActiveMQQueue("DLQ." + NON_PERSISTENT_DEST); | ||
dlqConsumer = session.createConsumer(dlqDestination); | ||
|
||
for (int i = 0; i < messageCount; i++) { | ||
Message msg = dlqConsumer.receive(1000); | ||
assertNotNull("Should be a DLQ message for loop: " + i, msg); | ||
assertEquals("NON_PERSISTENT", msg.getStringProperty("originalDeliveryMode")); | ||
org.apache.activemq.command.Message commandMsg = (org.apache.activemq.command.Message ) msg; | ||
assertTrue(commandMsg.isPersistent()); | ||
} | ||
|
||
session.commit(); | ||
} | ||
|
||
public void validateMessagePersitentNotSetWhenPreserveDeliveryModeIsTrue() throws Exception { | ||
messageCount = 1; | ||
destination = new ActiveMQQueue(PRESERVE_DELIVERY_DEST); | ||
durableSubscriber = false; | ||
deliveryMode = DeliveryMode.NON_PERSISTENT; | ||
connection.start(); | ||
|
||
ActiveMQConnection amqConnection = (ActiveMQConnection) connection; | ||
rollbackCount = amqConnection.getRedeliveryPolicy().getMaximumRedeliveries() + 1; | ||
|
||
makeConsumer(); | ||
makeDlqConsumer(); | ||
sendMessages(); | ||
|
||
for (int i = 0; i < messageCount; i++) { | ||
consumeAndRollback(i); | ||
} | ||
|
||
dlqDestination = new ActiveMQQueue("DLQ." + PRESERVE_DELIVERY_DEST); | ||
dlqConsumer = session.createConsumer(dlqDestination); | ||
|
||
for (int i = 0; i < messageCount; i++) { | ||
Message msg = dlqConsumer.receive(1000); | ||
assertNotNull("Should be a DLQ message for loop: " + i, msg); | ||
org.apache.activemq.command.Message commandMsg = (org.apache.activemq.command.Message ) msg; | ||
assertFalse(commandMsg.isPersistent()); | ||
} | ||
|
||
session.commit(); | ||
} | ||
} |
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.
I don't think we need/should create a new method here. This method is only used one time so I don't see the benefit of refactoring this unless you are planning to call
prepareMessageForDeadLetterQueue()
in more than one spot or make it protected/package scope to override it or be accessible in tests so I would just leave things inline.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.
I thought refactoring out made the
prepareMessageForDeadLetterQueue
method made thesendToDeadLetterQueue
method easier to read and understand.If folks don't agree I can revert the change?
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.
Well, while the method is in fact descriptive about what it is doing, it actually makes it harder to understand what you changed because by copying it all to a new method it's not clear what exactly the difference is from the old code. You have to look at it carefully as the entire method shows up as a new change which is the biggest reason why I made the comment because i as staring at it for a while to even figure out what was different.
That being said it's not the biggest deal obviously, the compiler may end up inlining the method anyways.
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.
Makes sense, I've removed the
prepareMessageForDeadLetterQueue
method.