spring async cleanup before publish
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
package org.baeldung.async;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AsyncAnnotationExample {
|
||||
|
||||
@Async
|
||||
public void asyncMethodWithVoidReturnType() {
|
||||
System.out.println("Execute method asynchronously. "
|
||||
+ Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
@Async
|
||||
public Future<String> asyncMethodWithReturnType() {
|
||||
System.out.println("Execute method asynchronously "
|
||||
+ Thread.currentThread().getName());
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
return new AsyncResult<String>("hello world !!!!");
|
||||
} catch (final InterruptedException e) {
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Async("threadPoolTaskExecutor")
|
||||
public void asyncMethodWithConfiguredExecutor() {
|
||||
System.out
|
||||
.println("Execute method asynchronously with configured executor"
|
||||
+ Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
@Async
|
||||
public void asyncMethodWithExceptions() throws Exception {
|
||||
throw new Exception("Throw message from asynchronous method. ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.baeldung.async;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AsyncComponent {
|
||||
|
||||
@Async
|
||||
public void asyncMethodWithVoidReturnType() {
|
||||
System.out.println("Execute method asynchronously. " + Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
@Async
|
||||
public Future<String> asyncMethodWithReturnType() {
|
||||
System.out.println("Execute method asynchronously " + Thread.currentThread().getName());
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
return new AsyncResult<String>("hello world !!!!");
|
||||
} catch (final InterruptedException e) {
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Async("threadPoolTaskExecutor")
|
||||
public void asyncMethodWithConfiguredExecutor() {
|
||||
System.out.println("Execute method asynchronously with configured executor" + Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
@Async
|
||||
public void asyncMethodWithExceptions() throws Exception {
|
||||
throw new Exception("Throw message from asynchronous method. ");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,18 +4,15 @@ import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
||||
|
||||
public class CustomAsyncExceptionHandler implements
|
||||
AsyncUncaughtExceptionHandler {
|
||||
public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
|
||||
|
||||
@Override
|
||||
public void handleUncaughtException(final Throwable throwable,
|
||||
final Method method, final Object... obj) {
|
||||
System.out.println("Exception message - " + throwable.getMessage());
|
||||
System.out.println("Method name - " + method.getName());
|
||||
for (final Object param : obj) {
|
||||
System.out.println("Param - " + param);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void handleUncaughtException(final Throwable throwable, final Method method, final Object... obj) {
|
||||
System.out.println("Exception message - " + throwable.getMessage());
|
||||
System.out.println("Method name - " + method.getName());
|
||||
for (final Object param : obj) {
|
||||
System.out.println("Param - " + param);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,18 +17,18 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
@ComponentScan("org.baeldung.async")
|
||||
public class SpringAsyncConfig implements AsyncConfigurer {
|
||||
|
||||
@Bean(name = "threadPoolTaskExecutor")
|
||||
public Executor threadPoolTaskExecutor() {
|
||||
return new ThreadPoolTaskExecutor();
|
||||
}
|
||||
@Bean(name = "threadPoolTaskExecutor")
|
||||
public Executor threadPoolTaskExecutor() {
|
||||
return new ThreadPoolTaskExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
return new SimpleAsyncTaskExecutor();
|
||||
}
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
return new SimpleAsyncTaskExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||
return new CustomAsyncExceptionHandler();
|
||||
}
|
||||
@Override
|
||||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||
return new CustomAsyncExceptionHandler();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user