Repository: activemq Updated Branches: refs/heads/activemq-5.13.x 93bc7030e -> 7f5c09f2d
https://issues.apache.org/jira/browse/AMQ-6221 Synchronizing ActiveMQText message on state changes for the content and text fields so that they are always changed together. This will prevent race conditions where data can be lost when using concurrent store and dispatch. (cherry picked from commit e0c549996479c2a1ccf70029ad4462cb987650f6) Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/6c2a825e Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/6c2a825e Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/6c2a825e Branch: refs/heads/activemq-5.13.x Commit: 6c2a825ebb516e9b37cdb913800de0306ed7e670 Parents: 93bc703 Author: Christopher L. Shannon (cshannon) <[email protected]> Authored: Tue Mar 22 13:41:20 2016 +0000 Committer: Christopher L. Shannon (cshannon) <[email protected]> Committed: Tue Mar 22 18:51:12 2016 +0000 ---------------------------------------------------------------------- .../activemq/command/ActiveMQTextMessage.java | 99 ++++++++++++++------ 1 file changed, 71 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/6c2a825e/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java ---------------------------------------------------------------------- diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java index 4618341..c9345b1 100755 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java @@ -45,7 +45,7 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_TEXT_MESSAGE; - protected String text; + protected volatile String text; @Override public Message copy() { @@ -55,14 +55,10 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage } private void copy(ActiveMQTextMessage copy) { - //AMQ-6218 - Save text before calling super.copy() to prevent a race condition when - //concurrent store and dispatch is enabled in KahaDB - //The issue is sometimes beforeMarshall() gets called in between the time content and - //text are copied to the new object leading to both fields being null when text should - //not be null - String text = this.text; - super.copy(copy); - copy.text = text; + synchronized(this) { + super.copy(copy); + copy.text = text; + } } @Override @@ -77,21 +73,30 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage @Override public void setText(String text) throws MessageNotWriteableException { - checkReadOnlyBody(); - this.text = text; - setContent(null); + synchronized(this) { + checkReadOnlyBody(); + this.text = text; + setContent(null); + } } @Override public String getText() throws JMSException { - ByteSequence content = getContent(); - String text = this.text; + ByteSequence content; + String text; + + synchronized(this) { + content = getContent(); + text = this.text; + } if (text == null && content != null) { text = decodeContent(content); - this.text = text; - setContent(null); - setCompressed(false); + synchronized(this) { + this.text = text; + setContent(null); + setCompressed(false); + } } return text; } @@ -131,16 +136,43 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage @Override public void storeContentAndClear() { - storeContent(); - text=null; + ByteSequence content; + String text; + synchronized(this) { + content = getContent(); + text = this.text; + } + if (content == null && text != null) { + content = marshallContent(text); + } + synchronized(this) { + setContent(content); + text=null; + } } @Override public void storeContent() { + ByteSequence content; + String text; + synchronized(this) { + content = getContent(); + text = this.text; + } + + if (content == null && text != null) { + content = marshallContent(text); + } + + synchronized(this) { + setContent(content); + } + } + + private ByteSequence marshallContent(String text) { + ByteSequence content = null; try { - ByteSequence content = getContent(); - String text = this.text; - if (content == null && text != null) { + if (text != null) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); @@ -151,19 +183,23 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage DataOutputStream dataOut = new DataOutputStream(os); MarshallingSupport.writeUTF8(dataOut, text); dataOut.close(); - setContent(bytesOut.toByteSequence()); + content = bytesOut.toByteSequence(); } } catch (IOException e) { throw new RuntimeException(e); } + return content; } + // see https://issues.apache.org/activemq/browse/AMQ-2103 // and https://issues.apache.org/activemq/browse/AMQ-2966 @Override public void clearMarshalledState() throws JMSException { - super.clearMarshalledState(); - this.text = null; + synchronized(this) { + super.clearMarshalledState(); + this.text = null; + } } /** @@ -179,13 +215,20 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage */ @Override public void clearBody() throws JMSException { - super.clearBody(); - this.text = null; + synchronized(this) { + super.clearBody(); + this.text = null; + } } @Override public int getSize() { - String text = this.text; + ByteSequence content; + String text; + synchronized(this) { + content = getContent(); + text = this.text; + } if (size == 0 && content == null && text != null) { size = getMinimumMessageSize(); if (marshalledProperties != null) {
