package <%=packageName%>.async;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.task.AsyncTaskExecutor;

public class ExceptionHandlingAsyncTaskExecutor implements AsyncTaskExecutor,
        InitializingBean, DisposableBean {

    private final Logger log = LoggerFactory.getLogger(ExceptionHandlingAsyncTaskExecutor.class);

    private final AsyncTaskExecutor executor;

    public ExceptionHandlingAsyncTaskExecutor(AsyncTaskExecutor executor) {
        this.executor = executor;
    }

    @Override
    public void execute(Runnable task) {
        executor.execute(task);
    }

    @Override
    public void execute(Runnable task, long startTimeout) {
        executor.execute(createWrappedRunnable(task), startTimeout);
    }

    private <T> Callable<T> createCallable(final Callable<T> task) {<% if (javaVersion == '8') { %>
        return () -> {
            try {
                return task.call();
            } catch (Exception e) {
                handle(e);
                throw e;
            }
        };<% } else { %>
        return new Callable<T>() {

            @Override
            public T call() throws Exception {
                try {
                    return task.call();
                } catch (Exception e) {
                    handle(e);
                    throw e;
                }
            }
        };<% } %>
    }

    private Runnable createWrappedRunnable(final Runnable task) {<% if (javaVersion == '8') { %>
		return () -> {
		    try {
		        task.run();
		    } catch (Exception e) {
		        handle(e);
		    }
		};<% } else { %>
        return new Runnable() {

            @Override
            public void run() {
                try {
                    task.run();
                } catch (Exception e) {
                    handle(e);
                }
            }
        };<% } %>
    }

    protected void handle(Exception e) {
        log.error("Caught async exception", e);
    }

    @Override
    public Future<?> submit(Runnable task) {
        return executor.submit(createWrappedRunnable(task));
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return executor.submit(createCallable(task));
    }

    @Override
    public void destroy() throws Exception {
        if (executor instanceof DisposableBean) {
            DisposableBean bean = (DisposableBean) executor;
            bean.destroy();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (executor instanceof InitializingBean) {
            InitializingBean bean = (InitializingBean) executor;
            bean.afterPropertiesSet();
        }
    }
}

@@ -1685,18 +1685,24 @@
 ') %7B %25%3E%0A
-%09%09
+        
 return (
@@ -1708,18 +1708,24 @@
 () -%3E %7B%0A
-%09%09
+        
     try 
@@ -1726,18 +1726,24 @@
   try %7B%0A
-%09%09
+        
         
@@ -1754,18 +1754,24 @@
 .run();%0A
-%09%09
+        
     %7D ca
@@ -1794,18 +1794,18 @@
 ) %7B%0A
-%09%09
+  
         
 hand
@@ -1792,32 +1792,38 @@
  e) %7B%0A          
+      
 handle(e);%0A%09%09   
@@ -1821,18 +1821,30 @@
 e);%0A
-%09%09    %7D%0A%09%09
+            %7D%0A        
 %7D;%3C%25

Fix indentation