如何使用 Spring Integration 2.0.5 根据内容路由消息?
我开始使用 si-xml:xpath-router 但遇到了障碍。我是否使用了正确的路由器但实施错误?如果我使用的路由器不正确,我应该使用哪一个,即默认路由器、有效负载类型,或者可能是一个简单的 SpEL 表达式?
用例:
我需要根据负载内容路由消息。该请求包含一个元素,并且我需要执行的“操作”包含在其属性中,请参阅下面的属性“命令”。
入站请求示例(来自 Web 服务)。
<Request>
<Records>
<Record>
<data key="name" value="Jack Bauer" />
<data key="command" value="sendSMS" />
</Record>
</Records>
</Request>
伪代码是:
- marshall message。
- 基于值的路由,通过 xpath-router
但我收到错误:
不支持的有效负载类型 [javax.xml.transform.dom.DOMResult]
为了解决这个问题,我尝试过:
使用 ResultToDocumentTransformer 将属性“result-transformer”添加到转换器 bean。 错误= 无法解析通道名称 ''
使用 StringResult 将属性“结果类型”添加到转换器。 错误 = 不支持的负载类型 [org.springframework.xml.transform.StringResult]
添加上述两项。 错误 = 无法解析
使用 true 添加属性“evaluate-as-string”。 错误 = 不支持的负载类型。
原始配置文件如下:
<gateway id="gateway" default-request-channel="requestChannel"
service-interface="foo.SomeClass" />
<beans:bean id="marshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<beans:property name="classesToBeBound">
<beans:list>
<beans:value>com.foo.Request</beans:value>
<beans:value>com.foo.Record</beans:value>
<beans:value>com.foo.Data</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<chain input-channel="requestChannel">
<poller max-messages-per-poll="10" task-executor="executor">
<interval-trigger interval="5000" />
</poller>
<si-xml:marshalling-transformer marshaller="marshaller"/>
<si-xml:xpath-router id="instockRouter" resolution-required="true">
<si-xml:xpath-expression expression="/Request/Records/Record/data[@key='command']"/>
<si-xml:mapping value="sendSMS" channel="SMSChannel"/>
</si-xml:xpath-router>
</chain>
<task:executor id="executor" pool-size="8"/>
I started out using the si-xml:xpath-router but I ran into a roadblock. Am I using correct router but wrong implementation? If I'm using the incorrect router, which one should I be using, i.e. default router, payload-type, or maybe a simple SpEL expression?
Use Case:
I need to route a message based on payload content. The request contains an element and the 'action' I need to perform is contained in one if its attributes, see attribute "command" below.
Example inbound request (comes from a web-service).
<Request>
<Records>
<Record>
<data key="name" value="Jack Bauer" />
<data key="command" value="sendSMS" />
</Record>
</Records>
</Request>
The psuedocode was:
- marshall message.
- route based on value, via xpath-router
but I'm getting the error:
unsupported payload type [javax.xml.transform.dom.DOMResult]
In order to resolve this, I have tried:
adding the attribute "result-transformer" to the transformer bean using ResultToDocumentTransformer. error= failed to resolve channel name ''
adding attribute "result-type" to the transformer using StringResult. error = unsupported payload type [org.springframework.xml.transform.StringResult]
adding both of the above. error = failed to resolve channel name ''
adding the attribute "evaluate-as-string" using true. error = unsupported payload type.
Original Configuration file below:
<gateway id="gateway" default-request-channel="requestChannel"
service-interface="foo.SomeClass" />
<beans:bean id="marshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<beans:property name="classesToBeBound">
<beans:list>
<beans:value>com.foo.Request</beans:value>
<beans:value>com.foo.Record</beans:value>
<beans:value>com.foo.Data</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<chain input-channel="requestChannel">
<poller max-messages-per-poll="10" task-executor="executor">
<interval-trigger interval="5000" />
</poller>
<si-xml:marshalling-transformer marshaller="marshaller"/>
<si-xml:xpath-router id="instockRouter" resolution-required="true">
<si-xml:xpath-expression expression="/Request/Records/Record/data[@key='command']"/>
<si-xml:mapping value="sendSMS" channel="SMSChannel"/>
</si-xml:xpath-router>
</chain>
<task:executor id="executor" pool-size="8"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编组后可以使用 Spring 的
"=object-to-string-transformer />"
。You can use Spring's
"=object-to-string-transformer />"
after marshalling.从我所看到的:
javax.xml.transform.dom.DOMResult
DOMResult
类型时,您尝试使用 xpath-router。据我所知,如果消息有效负载是内部包含纯 XML 的 String 类型,XPath 路由器可以正常工作。因此,我建议在使用 xpath 路由器之前不要编组消息,而是首先使用 xpath 路由器。
如果您要编组该对象,它将是 DOMResult 类型,并且您将必须处理 DOMResult (悲伤但真实:))
...无论如何,我认为 DOMResult 是不是您想要的消息有效负载 - 也许您在编组和解组之间犯了错误?
From what I can see:
org.springframework.oxm.jaxb.Jaxb2Marshaller
intojavax.xml.transform.dom.DOMResult
DOMResult
type you try to use xpath-router.As far as I know, XPath router works fine if message payload is String type containing plain XML inside. So I would recommend to do not marshall your message before using xpath router, but to use xpath router first.
If you will marshall the object, it will be
DOMResult
type and you will have to deal withDOMResult
(sad but true :))...anyway I think that DOMResult is not what you want to have as a message payload - maybe you did a mistake between marshalling and unmarshalling?