当前位置: 首页 > 知识库问答 >
问题:

Sync Scheduled方法运行异步方法-Spring@Scheduled,@Async

颛孙越
2023-03-14

我使用的是Spring的@计划和@异步注释。

我的目的

计划一个同步方法——它运行一个for循环,而这个循环将运行一个异步方法,因此循环中的下一个值不需要等到方法完成。

见下面我的代码:

/**
 *  Explanation: Scheduling async task for getting new hired candidates from the Lumesse Queue and saving the received data.
 */
@LoggableDebug
@Scheduled(fixedRateString = "${scheduler.insertCandidateFromQueueInDB.fixedRate}")
public void insertNewHiredCandidateFromQueueInDbScheduler() {
    for(EnvironmentContext environmentContext: context) {
        if(environmentContext.isActive()) {
            environmentAsyncHandlerInsertNewHiredCandidateFromQueueInDb(environmentContext);
        }
    }
}

/**
 * @param environmentContext
 * Explanation: Async task for getting new hired candidates from the Lumesse Queue and saving the received data.
 */
@LoggableDebug
@Async
public void environmentAsyncHandlerInsertNewHiredCandidateFromQueueInDb(EnvironmentContext environmentContext) {
    CandidateLumesse candidateLumesse;
    candidateLumesse = lumesseConnectorService.getNewHiredCandidateDataFromQueue(environmentContext);   
}

问题:

我的异步方法不会在不同的任务上运行。只有在我将@Async注释也放在我的计划方法上时,它才起作用。但我的计划方法将异步运行到,这不是我想要的。计划的方法需要同步运行,但for循环中被调用的方法需要异步运行。

尝试

我试过这样的解决方法:

Spring@Async方法调用@Scheduled方法内部

-

@LoggableDebug
@Scheduled(fixedRateString = "${scheduler.insertCandidateFromQueueInDB.fixedRate}")
public void insertNewHiredCandidateFromQueueInDbScheduler() {
    for(EnvironmentContext environmentContext: context) {
        if(environmentContext.isActive()) {
            asyncServiceExecutor.environmentAsyncHandlerInsertNewHiredCandidateFromQueueInDb(environmentContext);
        }
    }
}

现在的问题是我的计划任务被执行了两次...

最新消息

我做了一些更多的日志记录和我的对象只被创建一次:

2018-02-06 13:17:19.053  WARN 13144 --- [           main] 
c.d.l.c.s.LumesseConnectorScheduler      : #### SCHEDULER CLASS CONSTRUCTOR

我的固定利率是3000-

2018-02-06 13:17:26.388  WARN 13144 --- [pool-7-thread-1] 
c.d.l.c.s.LumesseConnectorScheduler      : #### START SCHEDULING
2018-02-06 13:17:26.397  WARN 13144 --- [pool-7-thread-1] 
c.d.l.c.s.LumesseConnectorScheduler      : #### START SCHEDULING

-

有什么想法吗?

共有1个答案

公冶子安
2023-03-14

-

尝试

我试过这样的解决方法:

Spring@Async方法调用@Scheduled方法内部

,但我遇到了一个新问题,调度器执行了两次:

解决方案

@user27149尝试删除@ConfigurationProperties(prefix=“environment”)或@Component

所以我改变了

@Component
@ConfigurationProperties(prefix="environment")
public class LumesseConnectorScheduler {

    private List<EnvironmentContextImpl> context;

致:

@Component
public class LumesseConnectorScheduler {

    @Autowired
    EnvironmentContextList environmentContextList;

谢谢你的回答!

 类似资料:
  • 问题内容: 我尝试运行一个异步进程。基于以下示例:http : //tomee.apache.org/examples-trunk/async- methods/README.html 但是,只有在完全完成其中的代码后,该方法才会返回。 然后当它返回并被调用时,我将得到异常: 原因:java.lang.IllegalStateException:对象不代表实际的Future 有什么建议我想念的吗?

  • 我有一个spring boot应用程序,我想有多个方法运行在一天的不同时间。第一个方法运行,但没有后续方法运行。我需要做什么来解决这个问题?这里是我的代码:@enableScheduling@configuration//@conditionalonproperty(name=“spring.enable.scheduling”)@springbootapplication@propertysou

  • 问题内容: 我使用的是Spring 4,我注意到了一个奇怪的行为……如果我从普通实例方法多次调用异步方法,那么它们都将在不同的线程中调用,并在随机时间完成。但是,如果我多次从另一个异步方法中调用一个异步方法,那么它们将按顺序完成。我有这样的事情: 我正在使用默认的异步执行器。我应该换一个吗?但是,该执行程序不会重用任何线程,而是每次都启动另一个线程,因此应该没问题……这仅仅是巧合吗?但是我尝试了十

  • 我对异步方法有一个奇怪的问题。如果我以异步方式运行它并且它的作业抛出一些特定的异常,它不会显示它并简单地停止执行(没有捕获,没有日志)。 我发现它可以使用jasperreport。这是故障块代码: 如果此代码位于异步注释方法内,则不会引发异常,也不会记录(只是停止执行)。如果删除异步注释,它会抛出以下内容: 我的问题不是异常本身,而是为什么异步方法抓不到它?

  • 是否可以调用一个异步方法,以便它从一个同步的方法异步运行?我不关心它挂起同步调用程序直到它返回,而是希望该方法被异步调用。