Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Spring集成消息有效负载发送和接收数据模型对象?_Spring_Jms_Spring Integration_Spring Jms - Fatal编程技术网

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

如何通过Spring集成消息有效负载发送和接收数据模型对象?,spring,jms,spring-integration,spring-jms,Spring,Jms,Spring Integration,Spring Jms,我试图将数据模型Java对象发送到Spring集成消息通道。我有一个jms出站通道适配器 这是我的Java代码。但它不起作用 @Autowired MessageChannel outputChannel; @Override public void sendToOutputQueue() { Record record = new Record(); Record.setId("123456789"); Record.setSerialNum("10000000"

我试图将数据模型Java对象发送到Spring集成消息通道。我有一个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());

}
@Autowired
消息通道输出通道;
@凌驾
public void sendToOutputQueue()
{
记录=新记录();
记录.setId(“123456789”);
记录。设置序列(“10000000”);
记录。设置代码(“ABC”);
Map Map=newhashmap();
地图放置(“记录”,记录);
outputChannel.send(MessageBuilder.withPayload(map.build());
}
这是我的Spring集成配置

<int:channel id="outputChannel" />  

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

当您使用
JmsTemplate
Map
发送到JMS中时,它将被包装到
MapMessage
(默认情况下):

下面是ActiveMQ
封送支持的一个片段:

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);
    }
}
publicstaticvoidmarshallprimitive(DataOutputStream out,对象值)抛出IOException{
如果(值==null){
空(出);
}else if(value.getClass()==Boolean.class){
MarshallBoolean(out,((布尔)值).booleanValue());
}else if(value.getClass()==Byte.class){
MarshallByte(out,((字节)值).byteValue());
}else if(value.getClass()==Character.class){
marshalChar(out,((字符)值).charValue());
}else if(value.getClass()=Short.class){
marshallshort(out,((Short)value).shortValue());
}else if(value.getClass()==Integer.class){
marshallint(out,((整型)值).intValue());
}else if(value.getClass()=Long.class){
MarshallLong(out,((Long)值).longValue());
}else if(value.getClass()==Float.class){
marshallfloat(out,((Float)value).floatValue());
}else if(value.getClass()==Double.class){
marshalDouble(out,((Double)值).doubleValue());
}else if(value.getClass()==字节[].class){
MarshallByteArray(输出,(字节[])值);
}else if(value.getClass()==String.class){
marshalString(输出,(字符串)值);
}else if(value.getClass()==UTF8Buffer.class){
marshalString(out,value.toString());
}else if(映射的值实例){
out.writeByte(映射类型);
MarshallPrimitiveMap((映射)值,输出);
}else if(列表的值实例){
out.writeByte(列表类型);
MarshallPrimitiveList((列表)值,out);
}否则{
抛出新IOException(“对象不是基元:“+值”);
}
}
因此,您的
记录将不被接受


您需要考虑不要使用JavaScript的<代码> MAP>代码>,并将您的<代码>记录作为<代码>可序列化的<代码>,以便通过JMS发送。或者考虑使用其他的<代码> MessageConverter < /代码>,而不是<代码> SimpleMessageConverter <代码>,这是默认的。例如,对我来说,

映射Jackson2MessageConverter
应该对你有好处。

你认为
不起作用的症状是什么?也许你在日志中有一些错误?也许您的
记录
不可序列化
?我需要使我的记录可序列化吗?由于HashMap在默认情况下是可序列化的,所以我认为不需要使记录可序列化。???这不是Java中的工作方式。映射只是引用的容器。当然,键和值必须可序列化才能传输到JMS代理中。现在它可以成功地发送到MQ。请不要为我重复评论。这听起来像垃圾邮件。我使记录对象可序列化,然后直接将其传递到消息负载中,而不将其转换为映射。它是这样工作的。现在我的问题转移到了接受方。如何将其作为对象从消息中检索。是否需要编写转换器将其反序列化为记录对象?不确定您关心的是什么,但消费者也配置了相同的
SimpleMessageConverter
。因此,任何可序列化的
都应该按原样处理;然后像下面这样接收它:Record=message.getPayload();但我得到的异常是:java.lang.ClassCastException:com.ibm.jms.JMSObjectMessage与com.noname.RecordoutputChannel.send(MessageBuilder.withPayload(record.build())不兼容;你需要展示你是如何接收的,但在你的问题中,请。评论中没有可读性
/** 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.
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);
    }
}