当您使用JmsTemplate 将Map 发送到JMS 时,它会被包装到MapMessage(默认情况下):
else if (object instanceof Map) {
return createMessageForMap((Map<? ,?>) object, session);
}
您必须确保地图的所有键都是String 类型,值是primitivies。这是根据MapMessage JavaDocs:
/** A {@code MapMessage} object is used to send a set of name-value pairs.
* The names are {@code String} objects, and the values are primitive
* data types in the Java programming language. The names must have a value that
* is not null, and not an empty string. The entries can be accessed
* sequentially or randomly by name. The order of the entries is undefined.
* {@code MapMessage} inherits from the {@code Message} interface
* and adds a message body that contains a Map.
这是来自 ActiveMQ MarshallingSupport 的 sn-p:
public static void marshalPrimitive(DataOutputStream out, Object value) throws IOException {
if (value == null) {
marshalNull(out);
} else if (value.getClass() == Boolean.class) {
marshalBoolean(out, ((Boolean)value).booleanValue());
} else if (value.getClass() == Byte.class) {
marshalByte(out, ((Byte)value).byteValue());
} else if (value.getClass() == Character.class) {
marshalChar(out, ((Character)value).charValue());
} else if (value.getClass() == Short.class) {
marshalShort(out, ((Short)value).shortValue());
} else if (value.getClass() == Integer.class) {
marshalInt(out, ((Integer)value).intValue());
} else if (value.getClass() == Long.class) {
marshalLong(out, ((Long)value).longValue());
} else if (value.getClass() == Float.class) {
marshalFloat(out, ((Float)value).floatValue());
} else if (value.getClass() == Double.class) {
marshalDouble(out, ((Double)value).doubleValue());
} else if (value.getClass() == byte[].class) {
marshalByteArray(out, (byte[])value);
} else if (value.getClass() == String.class) {
marshalString(out, (String)value);
} else if (value.getClass() == UTF8Buffer.class) {
marshalString(out, value.toString());
} else if (value instanceof Map) {
out.writeByte(MAP_TYPE);
marshalPrimitiveMap((Map<String, Object>)value, out);
} else if (value instanceof List) {
out.writeByte(LIST_TYPE);
marshalPrimitiveList((List<Object>)value, out);
} else {
throw new IOException("Object is not a primitive: " + value);
}
}
因此,您的Record 将不会被接受。
您需要考虑不要将Map 用于JMS 消息并将您的Record 设置为Serializable 以便能够通过JMS 发送它。或者考虑使用其他一些MessageConverter,而不是默认的SimpleMessageConverter。例如,对我来说,MappingJackson2MessageConverter 应该对你有好处。