Skip to content

Exception Handling

Alexander Kochurov edited this page Jul 24, 2015 · 1 revision

MxCache supports few strategies for handling exceptions in @Cached methods. Use @CacheExceptionPolicy annotation to choose a strategy for your method.

This annotation provide the following abilities.

rememberExceptions

Controls whether the exceptions will be stored in cache.

If set to true, the cache will remember that a certain key caused an exception and will throw the same exception on the next invocation of cached method with the same key.

Regardless of whether this flag is set or not, in case of exception the cache will recall original method for retries times. If any of these calls succeeds, the value will be saved to cache and returned.

The exception is stored for rememberExceptionTimeoutMillis milliseconds. After that the original method will be invoked again.

This setting may be overriden for specific exception type with specialCases.

retries

In case of exception the cache will recall original method for specified number of times. If any of these calls succeeds, the value will be saved to cache and returned.

The value of 0 (by default) means that if the first invocation of original method throws an exception, the cache will return it to the client without retrying.

Before each retry, the cache will wait for sleepBeforeRetryMillis ms.

This setting may be overriden for specific exception type with specialCases.

sleepBeforeRetryMillis

If retries is non-zero, the cache will wait for specified number of milliseconds before each retry. Ignored otherwise.

This setting may be overriden for specific exception type with specialCases.

rememberExceptionTimeoutMillis

If rememberExceptions is set to true, this property specifies the number of milliseconds for which the exception will be stored in cache. Ignored otherwise.

This setting may be overriden for specific exception type with specialCases.

Zero timeout means that the exception will be stored forever.

specialCases

You can override the properties for exception handling for certain exception types. All special cases are evaluated sequentially, in the same way as "catch" clauses.

For each special case you have to specify an exception class via exceptionClass and all the same parameters as listed above.

Example

@CacheExceptionPolicy(rememberExceptions = true, rememberExceptionTimeoutMillis = 5000, specialCases = @SpecialCase(exceptionClass = IOException.class, rememberExceptions = true, retries = 10, sleepBeforeRetryMillis = 500)
String loadSomething(URL url) throws IOException { ... }

If the method throws IOException, MxCache will call it again up to 10 times. Between retries there will be a pause of 0.5 seconds. If each of these fails too, MxCache will remember this exception for 5 seconds (inherited from @CacheExceptionPolicy.rememberExceptionTimeoutMillis).

If the method throws any other exception, it will be stored in cache for 5 seconds (during this period the cache will immediately rethrow the same exception without calling the original method).

Clone this wiki locally