user1305398
user1305398

Reputation: 3690

Apache Camel - stop execution and return with a failure message

I was on the look for an integration framework. And guess out I have found one - Apache Camel. This framework is cool. But for one requirement of mine, I am not able to place the components in the right order. The requirement has following stages -

Authentication
Authorization
PreProcessing etc.

So when a request comes I need to authenticate, authorize, preprocess etc. The problem is each of these step can fails and with that I do not want the execution flow to continue, rather should return back with some failure message. I was trying something of this sort -

from("jetty").choice().
when(Auth.isAuthenticated()).
       // proceed to authorization
       // proceed to preprocessing 
otherwise()
      // do something
endChoice();

But the problem over here is authorization stage might also fail, then the preprocessing step should not be executed and authorization stage itself should return a failure message. Is it possible?

Upvotes: 1

Views: 1268

Answers (1)

Souciance Eqdam Rashti
Souciance Eqdam Rashti

Reputation: 3193

Yes, this is standard Camel error handling. Please see the error and exception handling documentation. https://camel.apache.org/exception-clause.html

  1. Something goes wrong in the authorization stage. Fine throw an exception.
  2. You have an OnException which gets triggered based on that exception and then take some action. You can log, retry or send the message to another route which can put it on a dead letter queue on ActiveMQ.

Another thing I would probably do is split your functionality in separate routes. Put the authentication, authorization and preprocessing in separate routes. One big benefit of this is that they are not dependent on each other so other flows which need this functionality can simply call the routes and you can reuse them. Also if something does go wrong in authentication, then the message never gets to authentication route etc.

Upvotes: 2

Related Questions