How to handle Errors and Exceptions in Apache Camel ?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to apache camel tutorial in this video you will discover how flexible deep and comprehensive camel's error handling is and how to tailor it to deal with most situations we will cover all the error handlers camel provides out of the box and when they are best used so you can pick the ones suited to your application all right so there are two types of errors okay as you know in any application one is irrecoverable errors and second is recoverable errors so irrecoverable errors continue to be errors on subsequent attempts what does it mean is let's say i have a non-existing table does not matter how many times i retry it's not going to work until somebody goes and and creates that table right so these type of errors are irrecoverable errors right because we know it doesn't matter how many time you you retry they they are not going to work on the other hand the recoverable errors are you know the intermediate issues which on retry may be first attempt second attempt or third attempt they might work for example let's say you know i have an existing table over here as you can see first time when i when i try to call that table due to some network issues you know i i could not okay if i retry maybe like no no the the network is now available so i will be able to talk to that table and then i can recover from that type of errors um so if recoverable errors um is is represented as a message with a fault flag that can be set or accessed from exchange in in camel right for example to set a known customer as a fault message you would do something like this right you you'll have say exchange give me the message and on the message you will set fault equal to true okay and then you will set some sort of body message so you can let your consumer know that okay you know this thing cannot be recoverable because customer does not exist okay you can't just go ahead and create customer for example on the other hand recoverable errors in camel they are represented as a plane throwable or exception that can be set or accessed accessed from from your normal exchange okay and how do you do that you typically like know whenever there is exception or you like to throw an exception you you set it like that you can say void set exception this is throwable and when you want to get it back from the exchange you can say get exception and then you get that exception so you can you can you know run your business logic on top of it okay so question comes why are the two types of error represented differently right so the answer is first the camel api was designed around the java business integration what we call jbi specification which includes a fault message concept right so you can you can tell your consumer that you know what there is a fault with your request i cannot recover so that's a that's a very standard way of notifying your your clients and that's exactly what tamil is trying to implement over here secondly camel has error handling built in in its its core right so whenever an exception is thrown back to camel it catches it sets the the thrown exception on the exchange as a recoverable error as um as illustrated over here as you can see right so let's say i have a processor which throwing some sort of exception i can set on my exchange that exception has occurred so i can let my my ups you know upstream consumer or root no and and then upstream root can decide what to do with that that thing okay using this pattern uh it allows camera to catch and handle all exceptions that are thrown yeah camel's error handling can then determine how to deal with this this error okay um it can retry or propagate the error back to the the caller or do something else right just log it and and forget about it right depends on on on the on the error type and your business requirement and users of camel can set irrecoverable errors as fault messages if they want and camel can react accordingly and stop uh rooting the message all right so camel's error handling applies only within the life cycle of an exchange okay that's very important concept so what i try to illustrate over here is um the error handler boundaries are basically represented over here okay let's just try to understand this this diagram a little bit more let's say i have a root okay and where my endpoint is my upstream uh data provider is sending a file okay and this is where my root starts right so i'm consuming then i have some sort of translator then file producer and then i'm sending my updated file from a producer over here camel's error handling boundaries will only start okay when i manage to consume the file in this this example right okay if the file cannot even reach to to my root nothing i can do about it makes makes general sense as well right so it has to be in my context in order for me to to handle it okay there are few types of error handlers in in camel let's have a quick look so one is called default error handler as name suggests this is the default error handler that's automatically enabled you don't have to do anything and what it really means in in practical terms whenever an exception is thrown on your route it will be propagated all the way to your your consumer is think of it as a normal java exception which we deal on a day-to-day basis correct there is another important one called dead letter channel okay this error handles um it basically implements you may have heard a design pattern or eip pattern called dead letter what it simply means is whenever an exception is thrown you catch that exception and then you send it to a destination which is typically called dead letter so you can have another process which can monitor that dead letter and then then run some business logic on on it so camel very nicely has you know implemented that pattern and i'll i'll show you an example of it okay in in the in the workshop which we're gonna do um transaction error handler this is a transactional aware error handler i might not be covering in in this video but i'll try to do it in in the future videos okay no error handler this handler is used to disable error handling altogether what it means is let's say i have a root and camel gives you flexibility to kind of you know put exception handling error handling at several places you can can put this no error handler and then it will basically disable everything okay and it simply becomes the default which means the error will be kind of you know exception will be going back to your your your caller again uh last one is logging error handler this error handler has been now deprecated okay it's not recommended by camel in favor of uh number two dead letter uh channel okay so so camel is recommending don't use this one rather you use dead letter channels all right so this is the end of the presentation now let's just go back to our code so what i have tried to do over here just just to demonstrate there are three ways you can handle um exceptions thrown by by your root one is by what we call error handler method over here okay so inside your root you can put error handler and then you can define you know one of the the the handlers which i was showing you earlier and there is the example of dead letter channel so dead letter channel what it's simply trying to say is whenever on your route exception is thrown please handle like this this is what we are telling camel on on the given route if there is exception please handle it like that and how we want it to handle it we are saying send it to that letter channel and the end point for that dead letter channel is a bit of a tongue twister isn't it so dead letter channel can be uh another route okay a very simple rule which could be you know pointing to some external jms some database or as simple as like no another direct route which i have got over here it could be log something as well but just for demonstration purposes what i'm saying over here is whenever there is an exception on my route please send it to this this route which is created specifically for exception handling let's try to run it okay so what we have got over here is i have a root very simple root okay and i'm using timer over here so i don't have to you know send anything to to this this consumer over here it will be automatically pinged every uh one second inside that i have tried to put a little logic just just to demonstrate the power of exception handling so what i'm saying is when the you know there is a counter which i'm managing so what i'm asking it to do is counter you increment and get and whenever like you know the the counter has you know a mod of two equal to zero uh ie like no off on on every third call you call a method called bad which intentionally going to throw an exception okay so let's have a quick look so i have this bean called hello bean if i call good it's just simply gonna say call good call for counter number days if it's bad it's gonna say uh you know throw some runtime exception make sense so let's try to run run this program over here and i'm wondering where my uh counter is let's just quickly come over here hello bean where is this reading from okay so we like to use this static variable rather than defining my own okay let's do a static import okay and let's try to run this program just just to try it out so i'm going to because i have other ones which i like to demonstrate to you later on but we are only going to run this one first okay and i'm going to disable everything else to to begin with so if i start my application now this thing should be called okay and what this exception handler root is doing let's let's try to go to this um common error handler root what it's going to do is it's simply going to write this warning saying i am in the exception handler i'm going to sleep for five seconds okay we don't need to because um it was for something else and then simply going to print a body you can have a complex logic over here but i don't so what i'm simply going to do is i'm just going to write that body onto onto my log if i start my application okay straight away we don't need to submit anything every one second a ticker will be you know pinging over here and on third call it should start throwing exception okay as you can see um started this bad call bad call bad call okay one more thing you can do you can basically say like you know how many times you like to retry if there is exception sometime you know exception can be recoverable as we were speaking earlier so camel gives you this flexibility of like you know saying how many times you would like to retry it okay so over here i simply said two so as you can see it's printing like you know first time obviously the bad call happened and then it it tried to execute it two times okay and then it just carried on okay and then it raised the exception so this is how you use error handlers now now let's just move on and try to look at the the other types so what i'm going to do is i'm going to disable this thing now and close this file the second type the second way of handling errors in camel is what we call on exception root this is this is one of my favorite ways of doing it okay why it's my favorite because with error handler you are kind of specifying on a global level they're saying okay whenever exception happens please handle it over here i can pick and choose how i like to deal with certain type of exceptions you know if there is a checked exception i can have a database exception i can have some sort of business exception or i can have some other rules which are failing right so i can actually put as many on exceptions i want over here just just for demonstration purpose i can have my my own exception class if i had one okay i i can say look you know in that case you try retry four time but if general exception occurs please do not retry do not retry that okay so that is the reason i like this way very much because it gives me the flexibility very much like a try catch that i can i can configure please handle this check checked exception like this but general exception maybe just just dump something on on the console that's it finish it again what you can do over here when you're using on exception method or or on exception way you can redirect to another route or you can simply set dot log over here along with that as i was saying to you earlier you have several ways like you know maximum read re-deliveries delay whenever you want to retry it like maybe you would like to do a little pause 500 millisecond one second so maybe you can you can give the opportunity for for database to recover etc makes sense another important thing you have to notice here is this thing called handled what does that actually mean so what we are telling camel is that whenever exception occurred i have handled it so it doesn't need to propagate all the way to my my consumer right so we are kind of catching over here and taking action and finishing it off otherwise what will happen is that exception will remain on the exchange and upper roots might see the same thing but once you do that it will be flush it will be cleared you you done you have handled the error okay so this is the second way of of handling the error now let's go and look at the the third way which is good and you might find it quite familiar okay but it has its limitation but i thought since we are talking about the various ways i'll show that to you the third and final way of handling errors is something called do try do catch do finally does it sound familiar exactly it's like try catch finally uh we do in in normal java so so camel guys have have done this this for us they're saying look if you want you can put do try execute a method if it fails then you catch it and then you end if no exception occurs it will happily continue to execute the root make sense so what you're doing is the difference if you have to compare between three the error handler the number one which we discussed it's basically global level error handling the second one on exception is scoped plus you are able to kind of you know capture uh checked exception last but not least the third one is very local or specific you can you can surround it um you know you can surround a method call or or another root call with do and try so your your scope is very very limited it's beautiful powerful stuff okay so this is how you do um error handling in in camel i hope you like the video the code will be available on on sago dot uk website plus my github repo so download it clone the clone the code and and and play it locally and let me know how how you find it okay thanks for your time please leave your feedback and if you have not subscribed please subscribe and thank you for watching
Info
Channel: Saggu
Views: 144
Rating: undefined out of 5
Keywords: Java, Camel, EIP, Tutorial, Code, Error Handling, Exception Handling
Id: vSigAONeXKI
Channel Id: undefined
Length: 17min 59sec (1079 seconds)
Published: Mon Nov 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.