spring async cleanup before publish
This commit is contained in:
parent
ef46c6f7d4
commit
393616f969
@ -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;
|
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
||||||
|
|
||||||
public class CustomAsyncExceptionHandler implements
|
public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
|
||||||
AsyncUncaughtExceptionHandler {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleUncaughtException(final Throwable throwable,
|
public void handleUncaughtException(final Throwable throwable, final Method method, final Object... obj) {
|
||||||
final Method method, final Object... obj) {
|
System.out.println("Exception message - " + throwable.getMessage());
|
||||||
System.out.println("Exception message - " + throwable.getMessage());
|
System.out.println("Method name - " + method.getName());
|
||||||
System.out.println("Method name - " + method.getName());
|
for (final Object param : obj) {
|
||||||
for (final Object param : obj) {
|
System.out.println("Param - " + param);
|
||||||
System.out.println("Param - " + param);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,18 +17,18 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|||||||
@ComponentScan("org.baeldung.async")
|
@ComponentScan("org.baeldung.async")
|
||||||
public class SpringAsyncConfig implements AsyncConfigurer {
|
public class SpringAsyncConfig implements AsyncConfigurer {
|
||||||
|
|
||||||
@Bean(name = "threadPoolTaskExecutor")
|
@Bean(name = "threadPoolTaskExecutor")
|
||||||
public Executor threadPoolTaskExecutor() {
|
public Executor threadPoolTaskExecutor() {
|
||||||
return new ThreadPoolTaskExecutor();
|
return new ThreadPoolTaskExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Executor getAsyncExecutor() {
|
public Executor getAsyncExecutor() {
|
||||||
return new SimpleAsyncTaskExecutor();
|
return new SimpleAsyncTaskExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||||
return new CustomAsyncExceptionHandler();
|
return new CustomAsyncExceptionHandler();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,8 +6,9 @@
|
|||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||||
|
|
||||||
<task:annotation-driven executor="myExecutor" />
|
<task:annotation-driven executor="myExecutor" />
|
||||||
<task:executor id="myExecutor" pool-size="5" />
|
<task:executor id="myExecutor" pool-size="5" />
|
||||||
|
|
||||||
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncAnnotationExample" />
|
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncAnnotationExample" />
|
||||||
|
|
||||||
</beans>
|
</beans>
|
@ -15,47 +15,45 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||||||
@ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class AsyncAnnotationExampleTest {
|
public class AsyncAnnotationExampleTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
AsyncAnnotationExample asyncAnnotationExample;
|
private AsyncComponent asyncAnnotationExample;
|
||||||
|
|
||||||
@Test
|
// tests
|
||||||
public void testAsyncAnnotationForMethodsWithVoidReturnType() {
|
|
||||||
System.out.println("Start - invoking an asynchronous method. "
|
|
||||||
+ Thread.currentThread().getName());
|
|
||||||
asyncAnnotationExample.asyncMethodWithVoidReturnType();
|
|
||||||
System.out.println("End - invoking an asynchronous method. ");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAsyncAnnotationForMethodsWithReturnType()
|
public void testAsyncAnnotationForMethodsWithVoidReturnType() {
|
||||||
throws InterruptedException, ExecutionException {
|
System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
|
||||||
System.out.println("Start - invoking an asynchronous method. "
|
asyncAnnotationExample.asyncMethodWithVoidReturnType();
|
||||||
+ Thread.currentThread().getName());
|
System.out.println("End - invoking an asynchronous method. ");
|
||||||
final Future<String> future = asyncAnnotationExample
|
}
|
||||||
.asyncMethodWithReturnType();
|
|
||||||
|
|
||||||
while (true) {
|
@Test
|
||||||
if (future.isDone()) {
|
public void testAsyncAnnotationForMethodsWithReturnType() throws InterruptedException, ExecutionException {
|
||||||
System.out.println("Result from asynchronous process - "
|
System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
|
||||||
+ future.get());
|
final Future<String> future = asyncAnnotationExample.asyncMethodWithReturnType();
|
||||||
break;
|
|
||||||
}
|
|
||||||
System.out.println("Continue doing something else. ");
|
|
||||||
Thread.sleep(1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
while (true) {
|
||||||
public void testAsyncAnnotationForMethodsWithConfiguredExecutor() {
|
if (future.isDone()) {
|
||||||
System.out.println("Start - invoking an asynchronous method. ");
|
System.out.println("Result from asynchronous process - " + future.get());
|
||||||
asyncAnnotationExample.asyncMethodWithConfiguredExecutor();
|
break;
|
||||||
System.out.println("End - invoking an asynchronous method. ");
|
}
|
||||||
}
|
System.out.println("Continue doing something else. ");
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAsyncAnnotationForMethodsWithConfiguredExecutor() {
|
||||||
|
System.out.println("Start - invoking an asynchronous method. ");
|
||||||
|
asyncAnnotationExample.asyncMethodWithConfiguredExecutor();
|
||||||
|
System.out.println("End - invoking an asynchronous method. ");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAsyncAnnotationForMethodsWithException() throws Exception {
|
||||||
|
System.out.println("Start - invoking an asynchronous method. ");
|
||||||
|
asyncAnnotationExample.asyncMethodWithExceptions();
|
||||||
|
System.out.println("End - invoking an asynchronous method. ");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAsyncAnnotationForMethodsWithException() throws Exception {
|
|
||||||
System.out.println("Start - invoking an asynchronous method. ");
|
|
||||||
asyncAnnotationExample.asyncMethodWithExceptions();
|
|
||||||
System.out.println("End - invoking an asynchronous method. ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,17 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
@ContextConfiguration("classpath:springAsync-config.xml")
|
@ContextConfiguration("classpath:springAsync-config.xml")
|
||||||
public class AsyncWithXMLTest {
|
public class AsyncWithXMLTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
AsyncAnnotationExample asyncAnnotationExample;
|
private AsyncComponent asyncAnnotationExample;
|
||||||
|
|
||||||
|
// tests
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAsyncAnnotationForMethodsWithVoidReturnType() throws InterruptedException {
|
||||||
|
System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
|
||||||
|
asyncAnnotationExample.asyncMethodWithVoidReturnType();
|
||||||
|
Thread.sleep(2000);
|
||||||
|
System.out.println("End - invoking an asynchronous method. ");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAsyncAnnotationForMethodsWithVoidReturnType()
|
|
||||||
throws InterruptedException {
|
|
||||||
System.out.println("Start - invoking an asynchronous method. "
|
|
||||||
+ Thread.currentThread().getName());
|
|
||||||
asyncAnnotationExample.asyncMethodWithVoidReturnType();
|
|
||||||
Thread.sleep(2000);
|
|
||||||
System.out.println("End - invoking an asynchronous method. ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user