将异步方法的Spring配置类设置为:
@Configuration
@EnableAsync(proxyTargetClass = true)
@EnableScheduling
public class AsyncConfiguration {
@Autowired
private ApplicationContext applicationContext;
@Bean
public ActivityMessageListener activityMessageListener() {
return new ActivityMessageListener();
}
@Bean
public TaskExecutor defaultExecutor()
{
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(10);
threadPoolTaskExecutor.setQueueCapacity(Integer.MAX_VALUE);
return threadPoolTaskExecutor;
}
我的所有@Async
方法都按预期工作,但如果我将AsyncConfigurer
实现到AsyncConfiguration
以捕获实现getAsyncUncaughtExceptionHandler()
方法的异常,那么我的bean不会被代理,所以方法@Async
不会在池执行程序中运行。
这是非工作配置:
@Configuration
@EnableAsync(proxyTargetClass = true)
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {
@Autowired
private ApplicationContext applicationContext;
@Bean
public ActivityMessageListener activityMessageListener() {
return new ActivityMessageListener();
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(10);
threadPoolTaskExecutor.setQueueCapacity(Integer.MAX_VALUE);
return threadPoolTaskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
可能会发生什么?
我们正在使用这样的@Async
:
public class ActivityMessageListener extends BaseMessageListener {
public static final String PARAM_USER_ID = "userId";
public static final String PARAM_COMPANY_ID = "companyId";
public static final String PARAM_CREATE_DATE = "createDate";
public static final String PARAM_CLASS_NAME = "className";
public static final String PARAM_CLASS_PK = "classPK";
public static final String PARAM_TYPE = "type";
public static final String PARAM_EXTRA_DATA = "extraData";
public static final String PARAM_RECEIVED_USER_ID = "receiverUserId";
@Override @Async(value = "defaultExecutor")
public Future<String> doReceive(Message message) throws Exception {
String name = Thread.currentThread().getName();
Map<String, Object> parameters = message.getValues();
Long userId = (Long)parameters.get(ActivityMessageListener.PARAM_USER_ID);
Long companyId = (Long)parameters.get(ActivityMessageListener.PARAM_COMPANY_ID);
Date createDate = (Date)parameters.get(ActivityMessageListener.PARAM_CREATE_DATE);
String className = (String)parameters.get(ActivityMessageListener.PARAM_CLASS_NAME);
Long classPK = (Long)parameters.get(ActivityMessageListener.PARAM_CLASS_PK);
Integer type = (Integer)parameters.get(ActivityMessageListener.PARAM_TYPE);
String extraData = (String)parameters.get(ActivityMessageListener.PARAM_EXTRA_DATA);
Long receiverUserId = (Long)parameters.get(ActivityMessageListener.PARAM_RECEIVED_USER_ID);
ActivityLocalServiceUtil.addActivity(userId, companyId, createDate, className, classPK, type, extraData, receiverUserId);
return new AsyncResult<String>(name);
}
}
答案 0 :(得分:5)
编辑:我已提交bug report (SPR-14630)。
我即将向Spring的issue tracker提交错误报告,但是当我准备一个小应用程序来复制错误时,我发现并修复了问题。
首先,在使用ThreadPoolTaskExecutor
时,您应该在返回之前调用其initialize()
方法:
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setMaxPoolSize(1);
executor.setCorePoolSize(1);
executor.setThreadNamePrefix("CUSTOM-");
// Initialize the executor
executor.initialize();
return executor;
}
同样由于某种原因,如果我在同一配置类中定义的@PostConstruct
方法中使用bean,它将不会异步运行。原因是@PostConstruct
方法在执行getAsyncExecutor()
和getAsyncUncaughtExceptionHandler()
之前执行:
AsyncBean.java
@Component
public class AsyncBean implements IAsyncBean {
@Override
@Async
public void whoAmI() {
final String message =
String.format("My name is %s and I am running in %s", getClass().getSimpleName(), Thread.currentThread());
System.out.println(message);
}
}
AsyncDemoApp.java
@SpringBootApplication
@EnableAsync
public class AsyncDemoApp implements AsyncConfigurer {
@Autowired
private IAsyncBean asyncBean;
public static void main(String[] args) {
SpringApplication.run(AsyncDemoApp.class, args);
}
@Override
public Executor getAsyncExecutor() {
System.out.println("AsyncDemoApp.getAsyncExecutor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("CUSTOM-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
System.out.println("AsyncDemoApp.getAsyncUncaughtExceptionHandler");
return (throwable, method, objects)
-> throwable.printStackTrace();
}
@PostConstruct
public void start() {
System.out.println("AsyncDemoApp.start");
asyncBean.whoAmI();
}
}
AsyncDemoApp.start
My name is AsyncBean and I am running in Thread[main,5,main]
AsyncDemoApp.getAsyncExecutor
AsyncDemoApp.getAsyncUncaughtExceptionHandler
但是,如果在应用程序上下文可以使用后使用bean,它应该按预期工作:
@SpringBootApplication
@EnableAsync
public class AsyncDemoApp implements AsyncConfigurer {
public static void main(String[] args) {
final ConfigurableApplicationContext context = SpringApplication.run(AsyncDemoApp.class, args);
final IAsyncBean asyncBean = context.getBean(IAsyncBean.class);
asyncBean.whoAmI();
}
@Override
public Executor getAsyncExecutor() {
System.out.println("AsyncDemoApp.getAsyncExecutor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("CUSTOM-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
System.out.println("AsyncDemoApp.getAsyncUncaughtExceptionHandler");
return (throwable, method, objects)
-> throwable.printStackTrace();
}
}
另一个奇怪的行为是,如果您在同一配置类中 autowire 异步bean,则在配置自定义异步执行程序之前会发生自动连接,因此bean不会异步运行并且在主线程。这可以通过向@PostConstruct
添加AsyncBean
并使用CommandLineRunner
来运行应用来验证(个人认为这是一个错误。至少可以说这种行为非常令人惊讶):
AsyncBean
与@PostConstruct
:@Component
public class AsyncBean implements IAsyncBean {
@Override
@Async
public void whoAmI() {
final String message =
String.format("My name is %s and I am running in %s", getClass().getSimpleName(), Thread.currentThread());
System.out.println(message);
}
@PostConstruct
public void postConstruct() {
System.out.println("AsyncBean is constructed");
}
}
AsyncDemoApp
实施CommandLineRunner
:@SpringBootApplication
@EnableAsync
public class AsyncDemoApp implements AsyncConfigurer, CommandLineRunner {
@Autowired
private IAsyncBean asyncBean;
public static void main(String[] args) {
SpringApplication.run(AsyncDemoApp.class, args);
}
@Override
public Executor getAsyncExecutor() {
System.out.println("AsyncDemoApp.getAsyncExecutor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("CUSTOM-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
System.out.println("AsyncDemoApp.getAsyncUncaughtExceptionHandler");
return (throwable, method, objects)
-> throwable.printStackTrace();
}
@Override
public void run(String... args) throws Exception {
System.out.println("AsyncDemoApp.run");
asyncBean.whoAmI();
}
}
AsyncBean is constructed
AsyncDemoApp.getAsyncExecutor
AsyncDemoApp.getAsyncUncaughtExceptionHandler
AsyncDemoApp.run
My name is AsyncBean and I am running in Thread[main,5,main]
还有一件事! :)如果使用ThreadPoolTaskExecutor
,根据您的要求,您可能希望将其守护程序属性设置为true,否则您的应用程序将继续保持运行(对于Web / Worker应用程序而言,这不是一个大问题)。以下是setDaemon(boolean)
的JavaDoc所说的内容:
设置此工厂是否应该创建守护程序线程 只要应用程序本身正在运行就执行。默认是 “假”:具体工厂通常支持明确取消。 因此,如果应用程序关闭,Runnables将默认完成 他们的执行。为急切关闭线程指定“true” 仍然在应用程序时主动执行Runnable 本身就会关闭。