有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring任务异步且延迟

我需要做一件事,我不知道这是最好的做法

在我向一个特定的服务发送一个请求后,这个请求返回OK并对我的请求进行排队。我有一个回调服务,用来通知它何时结束

问题是,整个过程可能需要很长一段时间而不通知任何事情,然后我需要考虑超时。

该应用程序是SpringBoot应用程序,我正在考虑在一个有睡眠时间的服务方法上使用注释@enablesync和@Async

@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("TIMCLL-");
        executor.initialize();
        return executor;

    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        // TODO Auto-generated method stub
        return null;
    }

}

。 .

@Async
public void verifyStatusTimPayment() throws InterruptedException {

    Thread.sleep(5000);
    logger.info( "Executed after 5s " +  new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(new Date())); 

}

验证需要在请求15分钟后完成,并且每个请求只需进行一次

我怎么能不做线呢。睡觉


共 (3) 个答案

  1. # 1 楼答案

    我想我们可以在最近的春天使用@Scheduled。它将每15分钟运行一次 like方法注释如下

    @Scheduled(cron = "0 0/15 * * * *")
    public void verifyStatusTimPayment() throws InterruptedException {
        logger.info( "Executed after 5s " +  new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(new Date())); 
    
    }
    

    我知道我迟到了,但可能会帮助正在经历困境的人

  2. # 2 楼答案

    您可以使用Redis支持的延迟调度程序,这将保证您不会丢失任务。这可以使用Rqueue非常容易地完成

    在Rqueue中,您可以将15分钟后运行的任务排队,如下所示:

    public class Verification {
        private String id;
    }
    
    @Component
    class VerificationListener {
      @RqueueListener(
      value = "verification-queue")
      public void onMessage(Verification verification) {
        // do verification 
      }
    }
    
    
    @Service
    class DelayedTaskService {
        @Autowired private RqueueMessageSender rqueueMessageSender
        public void enqeueVerification(Verification verification) {
             rqueueMessageSender.enqueuIn("verification-queue", verification, Duration.ofMinutes(15);
        }
    }
    

    另外,我是Rqueue库的开发者

  3. # 3 楼答案

    您可以将@EnableScheduling注释添加到配置中:

    @Configuration
    @EnableAsync
    @EnableScheduling
    public class AsyncConfiguration implements AsyncConfigurer {
    
        @Override
        public Executor getAsyncExecutor() {
    
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(2);
            executor.setMaxPoolSize(10);
            executor.setQueueCapacity(500);
            executor.setThreadNamePrefix("TIMCLL-");
            executor.initialize();
            return executor;
    
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            // TODO Auto-generated method stub
            return null;
        }
    
    }
    

    如果要执行一次计划并延迟,可以调用taskScheduler:

    @Autowired 
    private TaskScheduler taskScheduler;
    

    并执行任务:

    taskScheduler.schedule(
        () -> {//your task}, 
        //Your Delay task
    );