Java Spring任务异步且延迟

Java Spring任务异步且延迟,java,spring,asynchronous,task,Java,Spring,Asynchronous,Task,我需要做一件事,我不知道这是最好的做法 在我向一个特定的服务发送一个请求后,这个请求返回OK并对我的请求进行排队。我有一个回调服务,用于通知何时结束 问题是,整个过程可能需要很长一段时间而不通知任何东西,然后我需要考虑超时。< /P> 该应用程序是SpringBoot应用程序,我曾考虑在具有睡眠时间的服务方法上使用注释@enablesync和@Async @Configuration @EnableAsync public class AsyncConfiguration implements

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

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

问题是,整个过程可能需要很长一段时间而不通知任何东西,然后我需要考虑超时。< /P> 该应用程序是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;
    }

}
。 .

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

如果没有线程,我怎么做呢?sleep?????

您可以使用来安排任务

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
...
scheduler.schedule(() -> {yourtaskhere}, 15, TimeUnit.MINUTES);
但这不是你想要的。如果服务器在任务调度和执行之间死亡怎么办?你将失去你的任务。
如果您将消息持久化到队列中,稍后再检索它,或者使用任何使用持久化的调度程序(la Quartz)会更好。

我认为我们可以在最新的春季使用@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())); 

}

我知道我迟到了,但可能会帮助正在执行线程的人。您可以在配置中添加@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
);

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

在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库的开发人员

但我只想执行一次,您的示例每15分钟运行一次,然后您就可以使用java定时器了
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);
    }
}