Skip to content

Commit 0fcf4c1

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
1 parent 828a930 commit 0fcf4c1

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Copyright (c) 2013-2020 VMware, Inc. or its affiliates. All rights reserved. */
22
package com.rabbitmq.integration.tests;
33

4+
import static org.assertj.core.api.Assertions.assertThat;
45
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
56
import static org.junit.jupiter.api.Assertions.assertEquals;
67
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -22,6 +23,7 @@
2223
import javax.jms.Session;
2324
import javax.jms.TextMessage;
2425

26+
import org.assertj.core.api.Assertions;
2527
import org.junit.jupiter.api.Test;
2628

2729
import com.rabbitmq.client.AMQP;
@@ -187,6 +189,7 @@ public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
187189

188190
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
189191
assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
192+
assertThat(message.getJMSTimestamp()).isZero();
190193
}
191194

192195
@Test
@@ -238,6 +241,7 @@ public void testSendFromAmqpAndReceiveTextMessage() throws Exception {
238241

239242
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
240243
assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
244+
assertThat(message.getJMSTimestamp()).isZero();
241245
}
242246

243247
@Test
@@ -274,6 +278,7 @@ public void testSendFromJmsAndReceiveJmsTextMessage() throws Exception {
274278
assertNotNull(message, "No message received");
275279
assertEquals(MESSAGE, message.getText(), "Payload doesn't match");
276280
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
281+
assertThat(message.getJMSTimestamp()).isGreaterThan(0);
277282
}
278283

279284
}

0 commit comments

Comments
 (0)