繁体   English   中英

如何获取 Spring Boot @Async 方法被拒绝任务的详细信息?

[英]How to get details of Spring Boot @Async method's rejected task?

在我的应用程序中,我使用@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 的ThreadPoolTaskExucutor Exucutor ,下面是我的AsyncConfiguration class 的快照,我在其中声明了这个任务执行器。

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

如您所见,我为我的 Executor 配置了RejectedExecutionHandler 根据 Spring 文档,当队列已满时,将调用此方法。

 * Method that may be invoked by a {@link ThreadPoolExecutor} when * {@link ThreadPoolExecutor#execute execute} cannot accept a * task. This may occur when no more threads or queue slots are * available because their bounds would be exceeded, or upon * shutdown of the Executor.
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??
    } 
}

被拒绝的执行处理程序对我来说工作正常,现在在这个rejectedExecutorion的Executorion 方法中,我想访问异步任务被拒绝的MyJob (我的异步方法的参数)。 我想用状态更新那个特定的被拒绝的工作,以便我以后可以运行玉米并处理那些被拒绝的工作。 在这个rejectedExecution方法中,我只有RunnableThreadPoolExucutor ,我如何在这里提取/获取有关 MyJob 的信息?

我的应用程序的 Spring 启动版本是2.2.2.RELEASE

您可以考虑通过为MyJob Class 实现Runnable接口直接使用TaskExecutor而不是@Async注释,并在run()方法中执行所需的异步操作。

Runnable r可以在处理程序的rejectedExecution方法中被转换回MyJob Object ,因此您可以从那里检索您的工作信息。

 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
         }

    } 
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM