Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artnaseef/d518c0431a34cd3d49e8d6e045a719e2 to your computer and use it in GitHub Desktop.
Save artnaseef/d518c0431a34cd3d49e8d6e045a719e2 to your computer and use it in GitHub Desktop.
Patch to ActiveMQTextMessage.java intended to eliminate race condition on access to the text field.
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 bb89378..f805ecb 100644
--- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java
+++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQTextMessage.java
@@ -46,6 +46,7 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_TEXT_MESSAGE;
protected String text;
+ private final Object textSync = new Object();
@Override
public Message copy() {
@@ -55,8 +56,10 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
}
private void copy(ActiveMQTextMessage copy) {
- super.copy(copy);
- copy.text = text;
+ synchronized (textSync) {
+ super.copy(copy);
+ copy.text = text;
+ }
}
@Override
@@ -72,20 +75,24 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
@Override
public void setText(String text) throws MessageNotWriteableException {
checkReadOnlyBody();
- this.text = text;
- setContent(null);
+ synchronized (textSync) {
+ this.text = text;
+ setContent(null);
+ }
}
@Override
public String getText() throws JMSException {
- ByteSequence content = getContent();
+ synchronized (textSync) {
+ ByteSequence content = getContent();
- if (text == null && content != null) {
- text = decodeContent(content);
- setContent(null);
- setCompressed(false);
+ if (text == null && content != null) {
+ text = decodeContent(content);
+ setContent(null);
+ setCompressed(false);
+ }
+ return text;
}
- return text;
}
private String decodeContent(ByteSequence bodyAsBytes) throws JMSException {
@@ -123,8 +130,10 @@ public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage
@Override
public void storeContentAndClear() {
- storeContent();
- text=null;
+ synchronized (textSync) {
+ storeContent();
+ text=null;
+ }
}
@Override
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment