Code samples for spring @Async annotation.
This commit is contained in:
parent
b5cc62b50e
commit
abbfc01105
@ -0,0 +1,43 @@
|
|||||||
|
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,21 @@
|
|||||||
|
package org.baeldung.async;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import org.springframework.aop.interceptor.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package org.baeldung.async.config;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import org.baeldung.async.CustomAsyncExceptionHandler;
|
||||||
|
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||||
|
import org.springframework.scheduling.annotation.AsyncConfigurer;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAsync()
|
||||||
|
@ComponentScan("org.baeldung.async")
|
||||||
|
public class SpringAsyncConfig implements AsyncConfigurer {
|
||||||
|
|
||||||
|
@Bean(name = "threadPoolTaskExecutor")
|
||||||
|
public Executor threadPoolTaskExecutor() {
|
||||||
|
return new ThreadPoolTaskExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Executor getAsyncExecutor() {
|
||||||
|
return new SimpleAsyncTaskExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||||
|
return new CustomAsyncExceptionHandler();
|
||||||
|
}
|
||||||
|
}
|
13
spring-all/src/main/resources/springAsync-config.xml
Normal file
13
spring-all/src/main/resources/springAsync-config.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:task="http://www.springframework.org/schema/task"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-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">
|
||||||
|
|
||||||
|
<task:annotation-driven executor="myExecutor" />
|
||||||
|
<task:executor id="myExecutor" pool-size="5" />
|
||||||
|
|
||||||
|
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncAnnotationExample" />
|
||||||
|
</beans>
|
@ -0,0 +1,61 @@
|
|||||||
|
package org.baeldung.async;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import org.baeldung.async.config.SpringAsyncConfig;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { SpringAsyncConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class AsyncAnnotationExampleTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AsyncAnnotationExample asyncAnnotationExample;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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
|
||||||
|
public void testAsyncAnnotationForMethodsWithReturnType()
|
||||||
|
throws InterruptedException, ExecutionException {
|
||||||
|
System.out.println("Start - invoking an asynchronous method. "
|
||||||
|
+ Thread.currentThread().getName());
|
||||||
|
final Future<String> future = asyncAnnotationExample
|
||||||
|
.asyncMethodWithReturnType();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (future.isDone()) {
|
||||||
|
System.out.println("Result from asynchronous process - "
|
||||||
|
+ future.get());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
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. ");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package org.baeldung.async;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("classpath:springAsync-config.xml")
|
||||||
|
public class AsyncWithXMLTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AsyncAnnotationExample asyncAnnotationExample;
|
||||||
|
|
||||||
|
@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