我的 Camel 路线设置如下:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file://C:/incoming?noop=true"/>
<log message="File Recieved"/>
<unmarshal>
<jaxb contextPath="org.ben.camel.spareparts.model" ignoreJAXBElement="true"/>
</unmarshal>
<log message="Store: ${body.getStore}"/>
<choice>
<when>
<simple>${body.getStore} == 2</simple>
<log message="New Item from Store 2"/>
<bean ref="processor" beanType="org.ben.camel.spareparts.model.StockReportProcessor"/>
<split>
<simple>${body}</simple>
<log message="item split!"/>
<marshal>
<jaxb contextPath="org.ben.camel.spareparts.model" ignoreJAXBElement="false"/>
</marshal>
<to uri="activemq:queue:store2"/>
</split>
</when>
<when>
<simple>${body.getStore} == 1</simple>
<log message="New Item from Store 1"/>
<bean ref="processor" beanType="org.ben.camel.spareparts.model.StockReportProcessor"/>
</when>
</choice>
</route>
</camelContext>
它获取一个 XML 对象,按 XML 元素拆分它,并将这些元素添加到队列中。我将其解码到 POJO 以仅提取元素列表,然后将其放入拆分组件中。
我的问题是,如果不是 XML,我似乎无法将 POJO 放入 ActiveMQ 中。即使我在提交之前再次尝试将其编码到 POJO 中,它仍然会以 XML 形式到达 MQ。我想知道,如果我在放入 ActiveMQ 的对象上有 JAXB 注释,它是否会自动转换为 XML?如果是这样,我该如何阻止它?
最佳答案
第一个编码只需将 jaxb 对象转换为 xml 输入流。
<marshal>
<jaxb contextPath="org.ben.camel.spareparts.model" ignoreJAXBElement="false"/>
</marshal>
其次,camel可以通过查找body实例来判断消息类型。
protected JmsMessageType getJMSMessageTypeForBody(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context) {
JmsMessageType type = null;
// let body determine the type
if (body instanceof Node || body instanceof String) {
type = Text;
} else if (body instanceof byte[] || body instanceof WrappedFile || body instanceof File || body instanceof Reader
|| body instanceof InputStream || body instanceof ByteBuffer || body instanceof StreamCache) {
type = Bytes;
} else if (body instanceof Map) {
type = Map;
} else if (body instanceof Serializable) {
type = Object;
} else if (exchange.getContext().getTypeConverter().tryConvertTo(File.class, body) != null
|| exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, body) != null) {
type = Bytes;
}
return type;
}
如果你想将对象消息发送到ActiveMQ,你需要确保消息体是Serialized的实例,否则camel会在JAXB后备转换器的帮助下将其转换为输入流,即使你将xml编码为Java 对象。
关于java - Camel、JAXB 到 ActiveMQ 自动创建 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25657241/