You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by opstalj <ja...@alcatel-lucent.com> on 2012/02/20 08:31:20 UTC

Camel Apache: can I use a retryWhile to re-send a request?

I would like to achieve the following kind of orchestration with CAMEL:

   1. Client sends a HTTP POST request to CAMEL
   2. CAMEL sends HTTP POST request to external endpoint (server)
   3. External server replies with a 200 OK
   4. CAMEL sends HTTP GET request to external endpoint (server)
   5. External server replies

After step 5, I want to check the reply: if the reply is a 200 OK and state
= INPROGRESS (this state can be retrieved from the received XML body), I
want to re-transmit the HTTP GET to the external endpoint until the state is
different from INPROGRESS (the INPROGRESS state just means the server is
busy handling the request, the result can still end up with FAILED or
SUCCESS).

I was thinking to use the retryWhile statement, but I am not sure how to
build the routine within the route. Eg, for checking whether the reply is a
200 OK and state = INPROGRESS, I can easily introduce a Predicate. So the
retryWhile already becomes like:

  /.retryWhile(Is200OKandINPROGRESS)/

but where should I place it in the route so that the HTTP GET will be
re-transmitted ?

Eg: (only taking step 4 and 5 into account)

/from("...")
  // here format the message to be sent out
  .to("external_server")
      // what code should I write here ??
      // something like: 
      //     .onException(alwaysDo.class)
      //         .retryWhile(Is200OKandINPROGRESS)
      //         .delay(2000)
      //     .end ()
      // or maybe it should not be here ??/

I am also a bit confused how the "alwaysDo.class" should look like ?? Or ...
should I use something completely different to solve this orchestration ? (I
just want to re-transmit as long as I get a 200 OK with INPROGRESS state
...)

Thanks in advance for your help.

Note: same question has been posted on the stackoverflow site, namely here:


http://stackoverflow.com/questions/9328978/camel-apache-can-i-use-a-retrywhile-to-re-send-a-request



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Apache-can-I-use-a-retryWhile-to-re-send-a-request-tp5498382p5498382.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: Camel Apache: can I use a retryWhile to re-send a request?

Posted by opstalj <ja...@alcatel-lucent.com>.
Update: the loop statement could indeed be used to serve my needs :).
Thanks once more.

--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Apache-can-I-use-a-retryWhile-to-re-send-a-request-tp5498382p5501879.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: Camel Apache: can I use a retryWhile to re-send a request?

Posted by "Boller, Stefan" <st...@sap.com>.
Hi Jan,

the retryWhile predicate can only used for error handling. If I understood you correctly, no exception is fired during the call of your http endpoint. Therefore, retryWhile does not fit for your use case. However, you can use the loop pattern to achieve what you want. If your loop includes a when statement you can decide, when the loop shall be ended. Your code shall look like:
                from("direct:file1").setHeader("state", constant("INPROGRESS"))//
                     .loop(1000)//
                        .to(httpEndpoint).process(new Processor() {//simulates the replies of external server
                            @Override
                            public void process(Exchange arg0) throws Exception {
                                if (arg0.getProperty("CamelLoopIndex").equals(NUMBER_OF_TRIES)) //set the state to COMPLETED after NUMBER_OF_TRIES tries
                                    arg0.getIn().setHeader("state", "COMPLETED");

                            }
                        }).choice()//
                        .when(header("state").isNotEqualTo("INPROGRESS")).to(finalEndpoint).stop()//
                     .end(); //loop

I hope this helps.

Best regards, STefan

-----Original Message-----
From: opstalj [mailto:jan.van_opstal@alcatel-lucent.com] 
Sent: Montag, 20. Februar 2012 08:31
To: users@camel.apache.org
Subject: Camel Apache: can I use a retryWhile to re-send a request?

I would like to achieve the following kind of orchestration with CAMEL:

   1. Client sends a HTTP POST request to CAMEL
   2. CAMEL sends HTTP POST request to external endpoint (server)
   3. External server replies with a 200 OK
   4. CAMEL sends HTTP GET request to external endpoint (server)
   5. External server replies

After step 5, I want to check the reply: if the reply is a 200 OK and state
= INPROGRESS (this state can be retrieved from the received XML body), I
want to re-transmit the HTTP GET to the external endpoint until the state is
different from INPROGRESS (the INPROGRESS state just means the server is
busy handling the request, the result can still end up with FAILED or
SUCCESS).

I was thinking to use the retryWhile statement, but I am not sure how to
build the routine within the route. Eg, for checking whether the reply is a
200 OK and state = INPROGRESS, I can easily introduce a Predicate. So the
retryWhile already becomes like:

  /.retryWhile(Is200OKandINPROGRESS)/

but where should I place it in the route so that the HTTP GET will be
re-transmitted ?

Eg: (only taking step 4 and 5 into account)

/from("...")
  // here format the message to be sent out
  .to("external_server")
      // what code should I write here ??
      // something like: 
      //     .onException(alwaysDo.class)
      //         .retryWhile(Is200OKandINPROGRESS)
      //         .delay(2000)
      //     .end ()
      // or maybe it should not be here ??/

I am also a bit confused how the "alwaysDo.class" should look like ?? Or ...
should I use something completely different to solve this orchestration ? (I
just want to re-transmit as long as I get a 200 OK with INPROGRESS state
...)

Thanks in advance for your help.

Note: same question has been posted on the stackoverflow site, namely here:


http://stackoverflow.com/questions/9328978/camel-apache-can-i-use-a-retrywhile-to-re-send-a-request



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Apache-can-I-use-a-retryWhile-to-re-send-a-request-tp5498382p5498382.html
Sent from the Camel - Users mailing list archive at Nabble.com.