简体   繁体   English

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

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

In my application I'm using an @Async method which is calling a rest service and based on the rest service result I'm updating the MyJob status in DB.在我的应用程序中,我使用@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
}

I'm using Spring's ThreadPoolTaskExucutor , Below is a snap from my AsyncConfiguration class where I declared this task executor.我正在使用 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;
}

As you can see I had configured a RejectedExecutionHandler for my Executor.如您所见,我为我的 Executor 配置了RejectedExecutionHandler According to Spring documentation when queue is full this method will be called.根据 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??
    } 
}

Rejected execution handler is working fine for me, now inside this rejectedExecutorion method, I want to access the MyJob (parameter of my async method) for which the async task is rejected.被拒绝的执行处理程序对我来说工作正常,现在在这个rejectedExecutorion的Executorion 方法中,我想访问异步任务被拒绝的MyJob (我的异步方法的参数)。 I want to update that particular rejected job with a status so that I can later run a corn and process those rejected jobs.我想用状态更新那个特定的被拒绝的工作,以便我以后可以运行玉米并处理那些被拒绝的工作。 Inside this rejectedExecution method I only have Runnable and ThreadPoolExucutor , how can I extract/get info about MyJob here?在这个rejectedExecution方法中,我只有RunnableThreadPoolExucutor ,我如何在这里提取/获取有关 MyJob 的信息?

My application's Spring boot version is 2.2.2.RELEASE我的应用程序的 Spring 启动版本是2.2.2.RELEASE

You could consider using the TaskExecutor directly instead of the @Async annotation by implementing the Runnable interface for MyJob Class and perform the required async operation inside the run() method.您可以考虑通过为MyJob Class 实现Runnable接口直接使用TaskExecutor而不是@Async注释,并在run()方法中执行所需的异步操作。

The Runnable r could be cast back to MyJob Object in the rejectedExecution method of the handler and hence you could retrieve information of your job from there. 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