Posts

Showing posts from January, 2023

Retry in SprinBoot Application

In a Spring Boot application, you can use the @Retryable annotation to automatically retry a failed operation. The @Retryable annotation can be applied to a method and can be used to specify the conditions under which the method should be retried. For example: @Retryable (value = { MyException. class }, maxAttempts = 3 , backoff = @Backoff (delay = 1000 )) public void myMethod () throws MyException { // method body } In this example, the myMethod() method will be retried if it throws an exception of type MyException. The method will be retried up to a maximum of 3 times, with a delay of 1000 milliseconds between each retry. You can also specify the @Recover annotation on a method to provide a recovery action for a failed operation. The @Recover method should have the same signature as the retryable method, except that it should have an additional parameter of type Throwable to receive the exception that caused the failure. For example: @Recover public void recover (MyE...

How to cache data in Springboot application

To cache data in a Spring Boot application, you can use the @Cacheable annotation on a method. When the method is called, the data returned by the method will be stored in a cache. Subsequent calls to the method will return the cached data, rather than executing the method again, until the cache is evicted or expired. In the following example,  @EnableCaching  annotation enables the cache mechanism. @SpringBootApplication @EnableCaching public class SpringBootCachingApplication { public static void main (String[] args) { SpringApplication. run (SpringBootCachingApplication. class , args); } } Here is an example of how to use the @Cacheable annotation: @Cacheable (value = "users" , key = "#userId" ) public User getUserById (Long userId) { // This method will be called only on the first invocation, // and the result will be cached. return userRepository. findById (userId); } In this example, the getUserById() method will...