2.14. OnCompletion
概述
OnCompletion DSL 名称用于定义在完成 工作单元时要执行的操作。工作单元是 Camel 概念,包含整个交换。请参阅 第 34.1 节 “Exchanges”。onCompletion 命令具有以下特性:
-
OnCompletion命令的范围可以是全局或每个路由。路由范围覆盖全局范围。 -
OnCompletion可以配置为在成功失败时触发。 -
onWhenpredicate 可用于在特定情况下触发Completion。 - 您可以定义是否有使用线程池,但默认设置不是线程池。
仅对Completion 的路由范围
在交换上指定了 onCompletion DSL 时,Camel 会从新线程关闭。这允许原始线程 在Completion 任务中的任何干扰的情况下继续。路由只支持 Completion。在以下示例中,触发了 Completion,即交换是否成功完成还是失败。这是默认的操作。
from("direct:start")
.onCompletion()
// This route is invoked when the original route is complete.
// This is similar to a completion callback.
.to("log:sync")
.to("mock:sync")
// Must use end to denote the end of the onCompletion route.
.end()
// here the original route contiues
.process(new MyProcessor())
.to("mock:result");对于 XML 格式,格式如下:
<route>
<from uri="direct:start"/>
<!-- This onCompletion block is executed when the exchange is done being routed. -->
<!-- This callback is always triggered even if the exchange fails. -->
<onCompletion>
<!-- This is similar to an after completion callback. -->
<to uri="log:sync"/>
<to uri="mock:sync"/>
</onCompletion>
<process ref="myProcessor"/>
<to uri="mock:result"/>
</route>
要在失败时触发 Completion,可以使用 onFailureOnly 参数。同样,若要在 success 上触发Completion,请使用 CompleteOnly 参数。
from("direct:start")
// Here onCompletion is qualified to invoke only when the exchange fails (exception or FAULT body).
.onCompletion().onFailureOnly()
.to("log:sync")
.to("mock:sync")
// Must use end to denote the end of the onCompletion route.
.end()
// here the original route continues
.process(new MyProcessor())
.to("mock:result");
对于 XML,inFailureOnly 和 onCompleteOnly 在 Completion 标签中以布尔值形式表示:
<route>
<from uri="direct:start"/>
<!-- this onCompletion block will only be executed when the exchange is done being routed -->
<!-- this callback is only triggered when the exchange failed, as we have onFailure=true -->
<onCompletion onFailureOnly="true">
<to uri="log:sync"/>
<to uri="mock:sync"/>
</onCompletion>
<process ref="myProcessor"/>
<to uri="mock:result"/>
</route>在完成过程中进行全局范围
为多个路由定义 Completion :
// define a global on completion that is invoked when the exchange is complete
onCompletion().to("log:global").to("mock:sync");
from("direct:start")
.process(new MyProcessor())
.to("mock:result");使用 onWhen
要在某些情况下触发 Completion,请使用 onWhen predicate。当消息正文包含文字 Hello:
/from("direct:start")
.onCompletion().onWhen(body().contains("Hello"))
// this route is only invoked when the original route is complete as a kind
// of completion callback. And also only if the onWhen predicate is true
.to("log:sync")
.to("mock:sync")
// must use end to denote the end of the onCompletion route
.end()
// here the original route contiues
.to("log:original")
.to("mock:result");使用带有或没有线程池的 onletion
自 Camel 2.14 起,默认情况下,Completion 不使用线程池。要强制使用线程池,可以设置 executorService 或将 parallelProcessing 设置为 true。例如,在 Java DSL 中,使用以下格式:
onCompletion().parallelProcessing()
.to("mock:before")
.delay(1000)
.setBody(simple("OnComplete:${body}"));对于 XML,格式为:
<onCompletion parallelProcessing="true">
<to uri="before"/>
<delay><constant>1000</constant></delay>
<setBody><simple>OnComplete:${body}<simple></setBody>
</onCompletion>
使用 executorServiceRef 选项引用特定的线程池:
<onCompletion executorServiceRef="myThreadPool"
<to uri="before"/>
<delay><constant>1000</constant></delay>
<setBody><simple>OnComplete:${body}</simple></setBody>
</onCompletion>>在消费者发送响应前运行Completion
在Completion 可以在两种模式下运行:
- AfterConsumer - 在消费者完成后运行的默认模式
-
BeforeConsumer - 在消费者向调用者写入响应之前运行。这允许
Completion修改 Exchange,如添加特殊标头,或者将 Exchange 配置为响应日志记录器。
例如,要在响应中添加 由标头创建的,请使用 modeBeforeConsumer (),如下所示:
.onCompletion().modeBeforeConsumer()
.setHeader("createdBy", constant("Someone"))
.end()
对于 XML,将 mode 属性设置为 BeforeConsumer :
<onCompletion mode="BeforeConsumer">
<setHeader headerName="createdBy">
<constant>Someone</constant>
</setHeader>
</onCompletion>