Skip to content

Commit aac59b2

Browse files
committed
Return 0 JMS timestamp if property is null
The property is not supposed to be null in JMS, but it can in AMQP 0.9.1, so JMS messages consumed from AMQP resources can have a null value. This commit mitigates the issue by returning a timestamp set to 0 in this case, just like the JMS 1.1 specification suggests it when MessageProducer#setDisableMessageTimestamp(true) is called. Fixes #126 (cherry picked from commit 0fcf4c1) Conflicts: src/test/java/com/rabbitmq/integration/tests/SimpleAmqpQueueMessageIT.java
1 parent 3bd45d5 commit aac59b2

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

src/main/java/com/rabbitmq/jms/client/RMQMessage.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ public void setJMSMessageID(String id) throws JMSException {
242242
*/
243243
@Override
244244
public long getJMSTimestamp() throws JMSException {
245-
return this.getLongProperty(JMS_MESSAGE_TIMESTAMP);
245+
Object timestamp = this.getObjectProperty(JMS_MESSAGE_TIMESTAMP);
246+
if (timestamp == null) {
247+
return 0L;
248+
} else {
249+
return convertToLong(timestamp);
250+
}
246251
}
247252

248253
/**
@@ -496,7 +501,10 @@ else if (o instanceof String) {
496501
*/
497502
@Override
498503
public long getLongProperty(String name) throws JMSException {
499-
Object o = this.getObjectProperty(name);
504+
return convertToLong(this.getObjectProperty(name));
505+
}
506+
507+
private static long convertToLong(Object o) throws JMSException {
500508
if (o == null)
501509
throw new NumberFormatException("Null is not a valid long");
502510
else if (o instanceof String) {

src/test/java/com/rabbitmq/integration/tests/SimpleAmqpQueueMessageIT.java

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javax.jms.*;
1111
import java.util.*;
1212

13+
import static org.assertj.core.api.Assertions.assertThat;
1314
import static org.junit.jupiter.api.Assertions.*;
1415

1516
/**
@@ -19,22 +20,22 @@ public class SimpleAmqpQueueMessageIT extends AbstractAmqpITQueue {
1920

2021
private static final String USER_JMS_TYPE_SETTING = "this is my type";
2122
private static final String USER_STRING_PROPERTY_NAME = "UserHdr";
22-
private static final String QUEUE_NAME = "test.queue."+SimpleAmqpQueueMessageIT.class.getCanonicalName();
23-
private static final String QUEUE_NAME_NON_EXCLUSIVE = "test-non-ex.queue."+SimpleAmqpQueueMessageIT.class.getCanonicalName();
23+
private static final String QUEUE_NAME = "test.queue." + SimpleAmqpQueueMessageIT.class.getCanonicalName();
24+
private static final String QUEUE_NAME_NON_EXCLUSIVE = "test-non-ex.queue." + SimpleAmqpQueueMessageIT.class.getCanonicalName();
2425
private static final String MESSAGE = "Hello " + SimpleAmqpQueueMessageIT.class.getName();
2526
private static final long TEST_RECEIVE_TIMEOUT = 1000; // one second
26-
private static final byte[] BYTE_ARRAY = { (byte) -2, (byte) -3, 4, 5, 34, (byte) -1 };
27+
private static final byte[] BYTE_ARRAY = {(byte) -2, (byte) -3, 4, 5, 34, (byte) -1};
2728
private static final String STRING_PROP_VALUE = "the string property value";
2829

2930
@Test
3031
public void testSendToAmqpAndReceiveTextMessage() throws Exception {
3132

3233
channel.queueDeclare(QUEUE_NAME,
33-
false, // durable
34-
true, // exclusive
35-
true, // autoDelete
36-
null // options
37-
);
34+
false, // durable
35+
true, // exclusive
36+
true, // autoDelete
37+
null // options
38+
);
3839

3940
queueConn.start();
4041
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
@@ -56,29 +57,30 @@ public void testSendToAmqpAndReceiveTextMessage() throws Exception {
5657
byte[] body = response.getBody();
5758
assertNotNull(body, "body of response is null");
5859

59-
{ Map<String, Object> hdrs = response.getProps().getHeaders();
60+
{
61+
Map<String, Object> hdrs = response.getProps().getHeaders();
6062

6163
assertEquals(new HashSet<String>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME))
6264
, hdrs.keySet(), "Some keys missing"
6365
);
6466

65-
assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
66-
assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong"); // toString is a bit wiffy
67-
assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
67+
assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
68+
assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong"); // toString is a bit wiffy
69+
assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
6870
}
6971

70-
assertEquals(MESSAGE, new String(body, "UTF-8"), "Message received not identical to message sent");
72+
assertEquals(MESSAGE, new String(body, "UTF-8"), "Message received not identical to message sent");
7173
}
7274

7375
@Test
7476
public void testSendToAmqpAndReceiveBytesMessage() throws Exception {
7577

7678
channel.queueDeclare(QUEUE_NAME,
77-
false, // durable
78-
true, // exclusive
79-
true, // autoDelete
80-
null // options
81-
);
79+
false, // durable
80+
true, // exclusive
81+
true, // autoDelete
82+
null // options
83+
);
8284

8385
queueConn.start();
8486
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
@@ -102,31 +104,32 @@ public void testSendToAmqpAndReceiveBytesMessage() throws Exception {
102104
byte[] body = response.getBody();
103105
assertNotNull(body, "body of response is null");
104106

105-
{ Map<String, Object> hdrs = response.getProps().getHeaders();
107+
{
108+
Map<String, Object> hdrs = response.getProps().getHeaders();
106109

107110
assertEquals(new HashSet<String>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME))
108111
, hdrs.keySet(), "Some keys missing"
109112
);
110113

111-
assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
112-
assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong"); // toString is a bit wiffy
113-
assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
114+
assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
115+
assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong"); // toString is a bit wiffy
116+
assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
114117
}
115118

116-
assertArrayEquals(BYTE_ARRAY, body, "Message received not identical to message sent");
119+
assertArrayEquals(BYTE_ARRAY, body, "Message received not identical to message sent");
117120
}
118121

119122
@Test
120123
public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
121124

122125
channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE,
123-
false, // durable
124-
false, // non-exclusive
125-
true, // autoDelete
126-
null // options
127-
);
126+
false, // durable
127+
false, // non-exclusive
128+
true, // autoDelete
129+
null // options
130+
);
128131

129-
Map<String, Object> hdrs = new HashMap<String,Object>();
132+
Map<String, Object> hdrs = new HashMap<String, Object>();
130133
hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
131134
hdrs.put("JMSType", USER_JMS_TYPE_SETTING);
132135
hdrs.put("JMSPriority", 21);
@@ -147,7 +150,7 @@ public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
147150

148151
assertNotNull(message, "No message received");
149152

150-
byte[] bytes = new byte[BYTE_ARRAY.length+2];
153+
byte[] bytes = new byte[BYTE_ARRAY.length + 2];
151154
int bytesIn = message.readBytes(bytes);
152155
assertEquals(BYTE_ARRAY.length, bytesIn, "Message payload not correct size");
153156
byte[] bytesTrunc = new byte[bytesIn];
@@ -169,19 +172,20 @@ public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
169172

170173
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
171174
assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
175+
assertThat(message.getJMSTimestamp()).isZero();
172176
}
173177

174178
@Test
175179
public void testSendFromAmqpAndReceiveTextMessage() throws Exception {
176180

177181
channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE,
178-
false, // durable
179-
false, // non-exclusive
180-
true, // autoDelete
181-
null // options
182-
);
182+
false, // durable
183+
false, // non-exclusive
184+
true, // autoDelete
185+
null // options
186+
);
183187

184-
Map<String, Object> hdrs = new HashMap<String,Object>();
188+
Map<String, Object> hdrs = new HashMap<String, Object>();
185189
hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
186190
hdrs.put("JMSType", "TextMessage"); // To signal a JMS TextMessage
187191
hdrs.put("JMSPriority", 21);
@@ -219,16 +223,17 @@ public void testSendFromAmqpAndReceiveTextMessage() throws Exception {
219223

220224
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
221225
assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
226+
assertThat(message.getJMSTimestamp()).isZero();
222227
}
223228

224229
@Test
225230
public void testSendFromJmsAndReceiveJmsTextMessage() throws Exception {
226231
String queueName = UUID.randomUUID().toString();
227232
channel.queueDeclare(queueName,
228-
false, // durable
229-
false, // non-exclusive
230-
true, // autoDelete
231-
null // options
233+
false, // durable
234+
false, // non-exclusive
235+
true, // autoDelete
236+
null // options
232237
);
233238

234239
queueConn.start();
@@ -255,6 +260,7 @@ public void testSendFromJmsAndReceiveJmsTextMessage() throws Exception {
255260
assertNotNull(message, "No message received");
256261
assertEquals(MESSAGE, message.getText(), "Payload doesn't match");
257262
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
263+
assertThat(message.getJMSTimestamp()).isGreaterThan(0);
258264
}
259265

260266
}

0 commit comments

Comments
 (0)