spring async cleanup before publish

This commit is contained in:
eugenp 2014-12-07 18:26:58 +02:00
parent ef46c6f7d4
commit 393616f969
7 changed files with 114 additions and 120 deletions

View File

@ -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. ");
}
}

View File

@ -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. ");
}
}

View File

@ -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);
} }
} }
} }

View File

@ -10,4 +10,5 @@
<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>

View File

@ -16,28 +16,25 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
public class AsyncAnnotationExampleTest { public class AsyncAnnotationExampleTest {
@Autowired @Autowired
AsyncAnnotationExample asyncAnnotationExample; private AsyncComponent asyncAnnotationExample;
// tests
@Test @Test
public void testAsyncAnnotationForMethodsWithVoidReturnType() { public void testAsyncAnnotationForMethodsWithVoidReturnType() {
System.out.println("Start - invoking an asynchronous method. " System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
+ Thread.currentThread().getName());
asyncAnnotationExample.asyncMethodWithVoidReturnType(); asyncAnnotationExample.asyncMethodWithVoidReturnType();
System.out.println("End - invoking an asynchronous method. "); System.out.println("End - invoking an asynchronous method. ");
} }
@Test @Test
public void testAsyncAnnotationForMethodsWithReturnType() public void testAsyncAnnotationForMethodsWithReturnType() throws InterruptedException, ExecutionException {
throws InterruptedException, ExecutionException { System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
System.out.println("Start - invoking an asynchronous method. " final Future<String> future = asyncAnnotationExample.asyncMethodWithReturnType();
+ Thread.currentThread().getName());
final Future<String> future = asyncAnnotationExample
.asyncMethodWithReturnType();
while (true) { while (true) {
if (future.isDone()) { if (future.isDone()) {
System.out.println("Result from asynchronous process - " System.out.println("Result from asynchronous process - " + future.get());
+ future.get());
break; break;
} }
System.out.println("Continue doing something else. "); System.out.println("Continue doing something else. ");
@ -58,4 +55,5 @@ public class AsyncAnnotationExampleTest {
asyncAnnotationExample.asyncMethodWithExceptions(); asyncAnnotationExample.asyncMethodWithExceptions();
System.out.println("End - invoking an asynchronous method. "); System.out.println("End - invoking an asynchronous method. ");
} }
} }

View File

@ -11,15 +11,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class AsyncWithXMLTest { public class AsyncWithXMLTest {
@Autowired @Autowired
AsyncAnnotationExample asyncAnnotationExample; private AsyncComponent asyncAnnotationExample;
// tests
@Test @Test
public void testAsyncAnnotationForMethodsWithVoidReturnType() public void testAsyncAnnotationForMethodsWithVoidReturnType() throws InterruptedException {
throws InterruptedException { System.out.println("Start - invoking an asynchronous method. " + Thread.currentThread().getName());
System.out.println("Start - invoking an asynchronous method. "
+ Thread.currentThread().getName());
asyncAnnotationExample.asyncMethodWithVoidReturnType(); asyncAnnotationExample.asyncMethodWithVoidReturnType();
Thread.sleep(2000); Thread.sleep(2000);
System.out.println("End - invoking an asynchronous method. "); System.out.println("End - invoking an asynchronous method. ");
} }
} }