package com.turreta.springboot.retry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.retry.RecoveryCallback;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;

import java.util.Collections;
import java.util.List;

@SpringBootApplication
public class ComTurretaSpringbootRetryApplication
{

	public static void main(String[] args) throws Exception
	{
		ApplicationContext context = SpringApplication.run(ComTurretaSpringbootRetryApplication.class, args);

		AnyService anyService = context.getBean(AnyService.class);
		RetryTemplate retryTemplate = context.getBean(RetryTemplate.class);

		// ##### Example 1
		List<String> data = retryTemplate.execute(
				new RetryCallback<List<String>, Exception>()
				{
					@Override public List<String> doWithRetry(RetryContext retryContext) throws Exception
					{
						System.out.println("Trying...");
						return anyService.getAllCodes_ThrowException();
					}
				}, new RecoveryCallback<List<String>>()
				{

					@Override public List<String> recover(RetryContext retryContext) throws Exception
					{
						return Collections.EMPTY_LIST;
					}
				});

		// Empty list - no output
		data.forEach(System.out::print);

		// ##### Example 2
		List<String> dataTwo = retryTemplate.execute(
				// Retries
				new RetryCallback<List<String>, Exception>()
				{
					@Override public List<String> doWithRetry(RetryContext retryContext) throws Exception
					{
						System.out.println("Trying...");
						return anyService.getAllCodes_Ok();
					}
				}
				,

				// Recover
				new RecoveryCallback<List<String>>()
				{
					@Override public List<String> recover(RetryContext retryContext) throws Exception
					{
						return Collections.EMPTY_LIST;
					}
				}
		);

		// Successfully retrieved data
		dataTwo.forEach(System.out::println);

		// ##### Example 3
		retryTemplate.execute(

				// Retries
				new RetryCallback<List<String>, Exception>()
				{
					@Override public List<String> doWithRetry(RetryContext retryContext) throws Exception
					{
						System.out.println("Trying...");
						anyService.updateCode_ThrowException("AA", "OK");
						return null;
					}
				},

				// Recover
				new RecoveryCallback<List<String>>()
				{
					@Override public List<String> recover(RetryContext retryContext) throws Exception
					{
						anyService.updateCode_Recovery("AA", "OK-BUT-ERROR");
						return null;
					}
				}
		);

		// ##### Example 4
		retryTemplate.execute(

				// Retries
				new RetryCallback<List<String>, Exception>()
				{
					@Override public List<String> doWithRetry(RetryContext retryContext) throws Exception
					{
						System.out.println("Trying...");
						anyService.updateCode_Ok("AB", "OK");
						return null;
					}
				},

				new RecoveryCallback<List<String>>()
				{
					@Override public List<String> recover(RetryContext retryContext) throws Exception
					{
						anyService.updateCode_Recovery("AA", "OK-BUT-ERROR");
						return null;
					}
				}
		);
	}
}

@@ -2122,36 +2122,28 @@
 tryCallback%3C
-List%3CString%3E
+Void
 , Exception%3E
@@ -2169,36 +2169,28 @@
 ride public 
-List%3CString%3E
+Void
  doWithRetry
@@ -2406,36 +2406,28 @@
 eryCallback%3C
-List%3CString%3E
+Void
 %3E()%0A%09%09%09%09%7B%0A%09%09
@@ -2442,36 +2442,28 @@
 ride public 
-List%3CString%3E
+Void
  recover(Ret
@@ -2688,36 +2688,28 @@
 tryCallback%3C
-List%3CString%3E
+Void
 , Exception%3E
@@ -2735,36 +2735,28 @@
 ride public 
-List%3CString%3E
+Void
  doWithRetry
@@ -2945,36 +2945,28 @@
 eryCallback%3C
-List%3CString%3E
+Void
 %3E()%0A%09%09%09%09%7B%0A%09%09
@@ -2981,36 +2981,28 @@
 ride public 
-List%3CString%3E
+Void
  recover(Ret

Update ComTurretaSpringbootRetryApplication.java