Java 如何获取Spring Boot@Async方法的详细信息';被拒绝的任务是什么?
在我的应用程序中,我使用一个Java 如何获取Spring Boot@Async方法的详细信息';被拒绝的任务是什么?,java,spring,spring-boot,asynchronous,Java,Spring,Spring Boot,Asynchronous,在我的应用程序中,我使用一个@Async方法调用rest服务,并根据rest服务结果更新数据库中的MyJob状态 @Async("thatOneTaskExecutor") public void myAsyncTask(MyJob job) { // get job details from the job and call rest service // update the job with the result from rest service an
@Async
方法调用rest服务,并根据rest服务结果更新数据库中的MyJob状态
@Async("thatOneTaskExecutor")
public void myAsyncTask(MyJob job) {
// get job details from the job and call rest service
// update the job with the result from rest service and save updated MyJob to DB
}
我正在使用Spring的ThreadPoolTaskExcutor
,下面是我声明此任务执行器的AsyncConfiguration
类的快照
private ThreadPoolTaskExecutor createExecutor(String name, int core, int max, int queue) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(core);
executor.setMaxPoolSize(max);
executor.setQueueCapacity(queue);
executor.setThreadNamePrefix(name);
executor.setTaskDecorator(new MdcAwareTaskDecorator());
executor.initialize();
return executor;
}
@Bean(name = "thatOneTaskExecutor")
public Executor taskExecutor() {
String prefix = "thatOneTask-";
String corePoolSize = 12;
String maxPoolSize = 20;
String queueSize = 1000;
ThreadPoolTaskExecutor executor = createExecutor(prefix, corePoolSize, maxPoolSize, queueSize);
executor.setRejectedExecutionHandler(new RejectedExecutionHandlerImpl());
return executor;
}
如您所见,我已经为我的执行器配置了一个RejectedExecutionHandler
。
根据Spring文档,当队列已满时,将调用此方法
被拒绝的执行处理程序对我来说运行良好,现在在这个rejectedExecutorion
方法中,我想访问被拒绝异步任务的MyJob(我的异步方法的参数)。我想用一个状态更新那个被拒绝的作业,以便以后可以运行corn并处理那些被拒绝的作业。在这个rejectedExecution
方法中,我只有Runnable
和ThreadPoolExcutor
,如何在这里提取/获取有关我的作业的信息
< >我的应用程序的Spring启动版本是<强> 2.2.2版本> p>可以通过使用<代码> @ asyc><代码>注释,而不是<代码> > ASYNC/<代码>注释,通过执行<代码> Runnaby<代码> >接口> <代码> MyJob /代码>类,并执行<代码> Run()中所需的异步操作。方法 在处理程序的
rejectedExecution
方法中,Runnable r
可以回溯到MyJob
对象,因此您可以从那里检索作业信息
public class Myjob implements Runnable{
.......
@Override
public void run(){
//get job details from the job and call rest service
//update the job with the result from rest service and save updated MyJob to DB
}
}
有一件事,我们可以使用类似observer的东西吗?设置一个观察者并在
拒绝执行
中观察它,但该观察者将如何获得有关我的作业
的信息?您可以看到,这里的问题是我希望访问@async方法的参数,该方法被拒绝。在这种情况下,ThreadPoolTaskExecutor
似乎受到位约束:它没有向回调传递足够的上下文(在您的情况下,MyJob
)来处理拒绝(rejectedExecution
在您的情况下)。如果API没有帮助,那么除了寻找一些替代方法之外,您没有什么可以做的。但是,不确定它是否会起作用,您可以尝试围绕任务调用设置一个。将Runnable
的实际调用包装在try-catch中,捕获执行器引发的异常(应该是拒绝执行exception
),检查(可能通过调试)异常是否为您提供了必要的上下文,并在那里实现错误处理。
public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
log.error("Task Rejected because of max queue size");
// How to get info about that particular job, for which Task executor rejected this task??
}
}
public class Myjob implements Runnable{
.......
@Override
public void run(){
//get job details from the job and call rest service
//update the job with the result from rest service and save updated MyJob to DB
}
}
@Autowired
TaskExecutor taskExecutor;
public void myAsyncTask(MyJob job) {
taskExecutor.execute(job)
}
public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
log.error("Task Rejected because of max queue size");
if(r.getClass()==MyJob.class)
{
MyJob failedJob=(MyJob)r; //Job info
}
}
}