如何通过Spring Integration消息有效负载发送和接收数据模型对象?

时间:2018-04-16 20:16:59

标签: spring jms spring-integration spring-jms

我正在尝试将数据模型Java对象发送到Spring Integration Message Channel。我有一个jms出站通道适配器。

这是我的Java代码。但它不起作用。

@Autowired
MessageChannel outputChannel;

@Override 
public void sendToOutputQueue() 
{
    Record record = new Record();
    Record.setId("123456789");
    Record.setSerialNum("10000000");
    Record.setCode("ABC");


    Map<String, Object> map = new HashMap<String, Object>();
    map.put("record", record);

    outputChannel.send(MessageBuilder.withPayload(map).build());

}

这是我的Spring Integration配置

<int:channel id="outputChannel" />  

<int-jms:outbound-channel-adapter id="outboundChannelAdapter" channel="outputChannel" jms-template="outputQueueJmsTemplate" />

1 个答案:

答案 0 :(得分:0)

使用Map向{JMS}发送JmsTemplate时,它会被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的一个片段:

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消息,并使RecordSerializable能够通过JMS发送它。或者考虑使用其他一些MessageConverter,而不是默认的SimpleMessageConverter。例如,对我来说,MappingJackson2MessageConverter应该对你有好处。