All Products
Search
Document Center

SchedulerX:FAQ about Spring jobs

Last Updated:Jul 03, 2024

This topic provides answers to some commonly asked questions about managing Spring jobs in SchedulerX.

Why does the original Spring timer still run after SchedulerX takes over scheduled Spring jobs?

If a custom scheduler is specified in your application, SchedulerX overwrites the custom scheduler. Check whether the class that implements the org.springframework.scheduling.annotation.SchedulingConfigurer interface exists in your application project, and whether the setScheduler method of ScheduledTaskRegistrar is called to overwrite the default scheduler. If the class exists or the default scheduler is overwritten, comment out the related code.

How do I obtain the context for a Spring job?

Add the following code to your application project code to obtain the context:

JobContext jobContext = ContainerFactory.getContainerPool().getContext();

Does a Spring job return a processing result?

If the agent version is later than 1.10.11, Spring jobs can return processing results. The processing results are returned based on the specified scheduling methods.

@Scheduled(cron = "0/5 * * * * ?")
public ProcessResult helloStandalone1() {
    try {
        logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. start");
        TimeUnit.SECONDS.sleep(2L);
        logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. end");
    } catch (Exception e) {
        e.printStackTrace();
        logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. exception end..");
    }
    return new ProcessResult(true, "Processing result");
}

@Scheduled(cron = "0/5 * * * * ?")
public String helloStandalone2() {
    try {
        logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. start");
        TimeUnit.SECONDS.sleep(2L);
        logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. end");
    } catch (Exception e) {
        e.printStackTrace();
        logger.info(DateUtil.now() + " " + Thread.currentThread().getName() + " hello world. exception end..");
    }
    return "Processing result";
}