You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by vss123 <vs...@gmail.com> on 2013/03/27 15:11:42 UTC

Retry based on HTTP response codes

Hi,

We use camel 2.10.3 with Scala DSL.

We have route that does a http post. We want to retry based on the response
code.
For e.g. no retries for 4xx and 2 retries for 5xx

We tried using handle[HttpOperationFailedException] but not sure how to get
the response code within that.
Can you help us with whats the best way to handle this case?



--
View this message in context: http://camel.465427.n5.nabble.com/Retry-based-on-HTTP-response-codes-tp5729939.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Retry based on HTTP response codes

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

See this FAQ
http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html

You can also some how to do this on this page
http://camel.apache.org/exception-clause.html

If you have a copy of Camel in Action book, then section 5.5 covers
such a use case to only handle certain conditions from a http response
code.

On Thu, Mar 28, 2013 at 10:02 AM, vss123 <vs...@gmail.com> wrote:
> Oh sorry.. I did not explain clearly.We wanted to throw different exceptions
> for 4xx and 5xx errors and handle them differently.  The problem is that we
> do not know how to get the exception object. We tried this
>     handle[HttpOperationFailedException]{      process(new Processor {
> def process(exchange: Exchange) {          val httpStatusCode =
> exchange.getIn.getHeader(Exchange.HTTP_RESPONSE_CODE)
> if(httpStatusCode.toString.startsWith("5")) throw new
> ServerHttpErrorException
> if(httpStatusCode.toString.startsWith("4")) throw new
> ClientHttpErrorException            throw new Exception        }      })
> }.handled(true)
> The httpStatusCode was /null/. We guess probably because it was thrown and
> not set on the exchange. We tried /getException/.But that, too is /null/
> Then we tried this,
> class ReThrower {  def doIt(httpException: HttpOperationFailedException){
> if(httpException.getStatusCode.toString.startsWith("5")) throw new
> ServerHttpErrorException
> if(httpException.getStatusCode.toString.startsWith("4")) throw new
> ClientHttpErrorException    throw httpException
> }}handle[HttpOperationFailedException]().handled(true).onWhen(bean(classOf[ReThrower],
> "doIt"))
> We got /java.util.NoSuchElementException: head of empty list/ when running.
> Could you give us ideas on how to go about this?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Retry-based-on-HTTP-response-codes-tp5729939p5730016.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Retry based on HTTP response codes

Posted by vss123 <vs...@gmail.com>.
Oh sorry.. I did not explain clearly.We wanted to throw different exceptions
for 4xx and 5xx errors and handle them differently.  The problem is that we
do not know how to get the exception object. We tried this 
    handle[HttpOperationFailedException]{      process(new Processor {       
def process(exchange: Exchange) {          val httpStatusCode =
exchange.getIn.getHeader(Exchange.HTTP_RESPONSE_CODE)         
if(httpStatusCode.toString.startsWith("5")) throw new
ServerHttpErrorException         
if(httpStatusCode.toString.startsWith("4")) throw new
ClientHttpErrorException            throw new Exception        }      })   
}.handled(true)
The httpStatusCode was /null/. We guess probably because it was thrown and
not set on the exchange. We tried /getException/.But that, too is /null/
Then we tried this,
class ReThrower {  def doIt(httpException: HttpOperationFailedException){   
if(httpException.getStatusCode.toString.startsWith("5")) throw new
ServerHttpErrorException   
if(httpException.getStatusCode.toString.startsWith("4")) throw new
ClientHttpErrorException    throw httpException 
}}handle[HttpOperationFailedException]().handled(true).onWhen(bean(classOf[ReThrower],
"doIt"))
We got /java.util.NoSuchElementException: head of empty list/ when running.
Could you give us ideas on how to go about this?



--
View this message in context: http://camel.465427.n5.nabble.com/Retry-based-on-HTTP-response-codes-tp5729939p5730016.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Retry based on HTTP response codes

Posted by Claus Ibsen <cl...@gmail.com>.
The HttpOperationFailedException class has methods to get the response code.


On Wed, Mar 27, 2013 at 3:11 PM, vss123 <vs...@gmail.com> wrote:
> Hi,
>
> We use camel 2.10.3 with Scala DSL.
>
> We have route that does a http post. We want to retry based on the response
> code.
> For e.g. no retries for 4xx and 2 retries for 5xx
>
> We tried using handle[HttpOperationFailedException] but not sure how to get
> the response code within that.
> Can you help us with whats the best way to handle this case?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Retry-based-on-HTTP-response-codes-tp5729939.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen