0

If we are using a resource in servlet init method and during initialization it failed to reach that resource but we know that after sometime resource will be available. What shall we do in this case?

  1. throw unavailable exception
  2. don't do anything once a request comes for this servlet it might start working
  3. do necessary stuff like try/catch and provide fallback logic

Any other suggestion?

2 Answers 2

1

It's a strange situation to accept requests before everything is available, but regardless, this is an example of a fault exception. You should throw an exception and have it bubble up to the "fault barrier," which is where you handle the exception. For example, you might log the exception and let the user know to check back later.

This is in contrast to contingency exceptions, which are recoverable and should be handled in such a way as to allow the user to move forward.

By the way, I didn't come up with this approach. Barry Ruzek did.

Sign up to request clarification or add additional context in comments.

Comments

1

I think, it depend on whether the resource is required for user's request. If it is required, you must initialize the resource before process user's request. If it is not required, you can skip the resource. You can also log the request in your database or log file. When the resource is available, you can invoke operation according to the log and modify status of the data which you stored.

4 Comments

then how do you handle in-case resource failed to initialize?
it depend on whether the resource is required for user's request. If it is required, you can throws exception. If it is not required, you can catch the exception and output it in log file. You can also set an flag in your code and set it false when resource failed to initialized. The latter request will judge the flag. If it is false, it will skip the code.
if i understand correctly, you mean throwing unavailable exception is enough. no need to provide fallback logic in-case resource is required and not available?
I mean that if you bussiness logic must depend on the resource, you can throw exception when the resource is required and not available. Otherwise you can skip the resource when the it not required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.