From 0ff13d0cf0b85a7cacb4fb3e27b1fc2ebe54cffd Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Mon, 23 Feb 2015 16:19:00 -0500 Subject: [PATCH] Fixing TransactionFailoverExample The TransactionFailoverExample IDs are not really unique if the journal is not cleaned up This commit will make the IDs to use an UUID so the ID will always be unique even if you reuse the journal --- .../ReplicatedTransactionFailoverExample.java | 11 +++++++++-- .../jms/example/TransactionFailoverExample.java | 16 +++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/jms/replicated-transaction-failover/src/main/java/org/apache/activemq/jms/example/ReplicatedTransactionFailoverExample.java b/examples/jms/replicated-transaction-failover/src/main/java/org/apache/activemq/jms/example/ReplicatedTransactionFailoverExample.java index 80f8ed69c4..98f096bc66 100644 --- a/examples/jms/replicated-transaction-failover/src/main/java/org/apache/activemq/jms/example/ReplicatedTransactionFailoverExample.java +++ b/examples/jms/replicated-transaction-failover/src/main/java/org/apache/activemq/jms/example/ReplicatedTransactionFailoverExample.java @@ -37,6 +37,13 @@ import org.apache.activemq.common.example.ActiveMQExample; */ public class ReplicatedTransactionFailoverExample extends ActiveMQExample { + + // You need to guarantee uniqueIDs when using duplicate detection + // It needs to be unique even after a restart + // as these IDs are stored on the journal for control + // We recommend some sort of UUID, but for this example the Current Time as string would be enough + String uniqueID = Long.toString(System.currentTimeMillis()); + public static void main(final String[] args) { new ReplicatedTransactionFailoverExample().run(args); @@ -146,7 +153,7 @@ public class ReplicatedTransactionFailoverExample extends ActiveMQExample // We set the duplicate detection header - so the server will ignore the same message // if sent again after failover - message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), "uniqueid" + i); + message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), uniqueID + i); producer.send(message); @@ -169,7 +176,7 @@ public class ReplicatedTransactionFailoverExample extends ActiveMQExample // We set the duplicate detection header - so the server will ignore the same message // if sent again after failover - message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), "uniqueid" + i); + message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), uniqueID + i); producer.send(message); diff --git a/examples/jms/transaction-failover/src/main/java/org/apache/activemq/jms/example/TransactionFailoverExample.java b/examples/jms/transaction-failover/src/main/java/org/apache/activemq/jms/example/TransactionFailoverExample.java index d9cbd95fbc..9666c8f427 100644 --- a/examples/jms/transaction-failover/src/main/java/org/apache/activemq/jms/example/TransactionFailoverExample.java +++ b/examples/jms/transaction-failover/src/main/java/org/apache/activemq/jms/example/TransactionFailoverExample.java @@ -37,11 +37,19 @@ import org.apache.activemq.common.example.ActiveMQExample; */ public class TransactionFailoverExample extends ActiveMQExample { + public static void main(final String[] args) { new TransactionFailoverExample().run(args); } + // You need to guarantee uniqueIDs when using duplicate detection + // It needs to be unique even after a restart + // as these IDs are stored on the journal for control + // We recommend some sort of UUID, but for this example the Current Time as string would be enough + String uniqueID = Long.toString(System.currentTimeMillis()); + + @Override public boolean runExample() throws Exception { @@ -138,15 +146,13 @@ public class TransactionFailoverExample extends ActiveMQExample final int numMessages, final boolean killServer) throws Exception { + // We send half of messages for (int i = 0; i < numMessages / 2; i++) { TextMessage message = session.createTextMessage("This is text message " + i); - // We set the duplicate detection header - so the server will ignore the same message - // if sent again after failover - - message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), "uniqueid" + i); + message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), uniqueID + i); producer.send(message); @@ -169,7 +175,7 @@ public class TransactionFailoverExample extends ActiveMQExample // We set the duplicate detection header - so the server will ignore the same message // if sent again after failover - message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), "uniqueid" + i); + message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), uniqueID + i); producer.send(message);