diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java index 3f1baf1dce..4c5190db47 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/TypedProperties.java @@ -23,10 +23,12 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Predicate; +import java.util.function.Supplier; import io.netty.buffer.ByteBuf; import org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException; @@ -169,10 +171,12 @@ public class TypedProperties { throw new ActiveMQPropertyConversionException("Invalid conversion: " + key); } - public Byte getByteProperty(final SimpleString key) throws ActiveMQPropertyConversionException { + public Byte getByteProperty(final SimpleString key, + final Supplier defaultValue) throws ActiveMQPropertyConversionException { + Objects.requireNonNull(defaultValue); Object value = doGetProperty(key); if (value == null) { - return Byte.valueOf(null); + return defaultValue.get(); } else if (value instanceof Byte) { return (Byte) value; } else if (value instanceof SimpleString) { @@ -181,6 +185,10 @@ public class TypedProperties { throw new ActiveMQPropertyConversionException("Invalid conversion: " + key); } + public Byte getByteProperty(final SimpleString key) throws ActiveMQPropertyConversionException { + return getByteProperty(key, () -> Byte.valueOf(null)); + } + public Character getCharProperty(final SimpleString key) throws ActiveMQPropertyConversionException { Object value = doGetProperty(key); if (value == null) { diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesConversionTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesConversionTest.java index 539172412f..a2917ff562 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesConversionTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TypedPropertiesConversionTest.java @@ -115,6 +115,18 @@ public class TypedPropertiesConversionTest { } } + @Test + public void testNoByteProperty() { + Assert.assertEquals(0, props.size()); + Assert.assertNull(props.getByteProperty(key, () -> null)); + props.putByteProperty(key.concat('0'), RandomUtil.randomByte()); + Assert.assertEquals(1, props.size()); + Assert.assertNull(props.getByteProperty(key, () -> null)); + props.putNullValue(key); + Assert.assertTrue(props.containsProperty(key)); + Assert.assertNull(props.getByteProperty(key, () -> null)); + } + @Test public void testIntProperty() throws Exception { Integer val = RandomUtil.randomInt(); diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java index 7f25f04fc2..1129f558a0 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java @@ -154,10 +154,11 @@ public class CoreMessage extends RefCountMessage implements ICoreMessage { @Override public RoutingType getRoutingType() { - if (containsProperty(Message.HDR_ROUTING_TYPE)) { - return RoutingType.getType(getByteProperty(Message.HDR_ROUTING_TYPE)); + final Byte maybeByte = getProperties().getByteProperty(Message.HDR_ROUTING_TYPE, () -> null); + if (maybeByte == null) { + return null; } - return null; + return RoutingType.getType(maybeByte); } @Override