검색

2.14. OnCompletion

download PDF

2.14.1. 개요

OnCompletion DSL 이름은 작업 단위 가 완료될 때 수행할 작업을 정의하는 데 사용됩니다. 작업 단위는 전체 교환을 포함하는 Camel 개념입니다. 34.1절. “교환”을 참조하십시오. onCompletion 명령에는 다음과 같은 기능이 있습니다.

  • OnCompletion 명령의 범위는 글로벌 또는 경로당일 수 있습니다. 경로 범위는 글로벌 범위를 덮어씁니다.
  • OnCompletion 은 실패 시 성공 시 트리거되도록 구성할 수 있습니다.
  • onWhen 서술자는 특정 상황에서만 onCompletion 을 트리거하는 데 사용할 수 있습니다.
  • 기본값은 스레드 풀이 아니지만 스레드 풀을 사용할지 여부를 정의할 수 있습니다.

2.14.2. OnCompletion을 위한 경로만 범위

교환에 onCompletion DSL이 지정되면 Camel이 새 스레드를 구동합니다. 이를 통해 onCompletion 작업의 간섭 없이 원래 스레드를 계속할 수 있습니다. 경로는 하나의 onCompletion 만 지원합니다. 다음 예에서 onCompletion 은 교환이 성공 또는 실패로 완료되었는지 여부를 트리거합니다. 기본 동작입니다.

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>

실패 시 onCompletion 을 트리거하려면 onFailureOnly 매개변수를 사용할 수 있습니다. 마찬가지로 성공 시 onCompletion 을 트리거하려면 onCompleteOnly 매개변수를 사용합니다.

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의 경우 onFailureOnlyonCompleteOnlyonCompletion 태그에서 부울로 표시됩니다.

<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>

2.14.3. 온Completion을 위한 글로벌 범위

두 개 이상의 경로에 대한 onCompletion 을 정의하려면 다음을 수행합니다.

// 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");

2.14.4. OnIf 사용

특정 상황에서 onCompletion 을 트리거하려면 on When 서술자를 사용합니다. 다음 예제는 메시지 본문에 Hello:이라는 단어가 포함된 경우 onCompletion 을 트리거합니다.

/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");

2.14.5. 스레드 풀과 함께 또는 없는 온Completion 사용

Camel 2.14부터는 기본적으로 스레드 풀을 사용하지 않습니다. 스레드 풀을 강제로 사용하려면 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>>

2.14.6. 소비자 전송 응답 전에 온Completion 실행

OnCompletion 은 다음 두 가지 모드로 실행할 수 있습니다.

  • AfterConsumer - 소비자가 완료된 후 실행되는 기본 모드
  • beforeConsumer - 소비자가 호출자에게 다시 응답을 쓰기 전에 실행됩니다. 이를 통해 OnCompletion 을 통해 특수 헤더 추가와 같은 교환을 수정하거나 Exchange를 응답 로거로 기록할 수 있습니다.

예를 들어 헤더로 생성된 를 응답에 추가하려면 다음과 같이 modeBeforeConsumer() 를 사용합니다.

.onCompletion().modeBeforeConsumer()
     .setHeader("createdBy", constant("Someone"))
 .end()

XML의 경우 mode 속성을 BeforeConsumer 로 설정합니다.

<onCompletion mode="BeforeConsumer">
   <setHeader headerName="createdBy">
     <constant>Someone</constant>
   </setHeader>
 </onCompletion>
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.