5.4 异步请求-响应方式
异步请求-响应方式即请求方调用服务后不需要立即获得返回结果,component将请求发送给其他外围系统处理(可能有多个),全部处理完毕后通过指定的异步应答Router返回给请求方。
图 Asynchronous Request-Response
异步请求-响应方式通过在OutBound Endpoint中增加reply-to以及增加async-reply节点实现,响应配置如下:
<flow name="echo"> <inbound-endpoint address="http://localhost:7007/services/Echo" exchange-pattern="request-response" /> <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" /> <component> <singleton-object class="demo.mule.umo.StdIo" /> </component> <vm:outbound-endpoint path="vm" exchange-pattern="request-response" /> </flow> <flow name="vm"> <vm:inbound-endpoint path="vm" exchange-pattern="request-response" /> <component> <singleton-object class="demo.mule.umo.Vm" /> </component> <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" /> </flow>
异步请求-响应方式适用于请求需要被多个远程服务并行处理,结果需要汇总处理后返回的场景。
注:上述代码未运行通过,queue1和queue2获得了请求消息并正常处理,但返回至async-reply时抛出异常,暂未定位到问题。
后将collection-async-reply-router改为single-async-reply-router未报异常,代码示例如下:
<service name="async req-rep">
<inbound>
<stdio:inbound-endpoint ref="stdioInEndpoint" />
</inbound>
<component class="demo.mule.umo.Echo" />
<outbound>
<multicasting-router>
<vm:outbound-endpoint path="async.queue1" exchange-pattern="one-way" />
<vm:outbound-endpoint path="async.queue2" exchange-pattern="one-way" />
<reply-to address="vm://reply" />
</multicasting-router>
</outbound>
<async-reply timeout="5000" failOnTimeout="true">
<vm:inbound-endpoint path="reply" exchange-pattern="one-way" />
<single-async-reply-router />
</async-reply>
</service>
等有空看看collection-async-reply-router吧,或者自定义router。