如何在多个服务中使用Scheduler?

时间:2018-12-19 06:42:35

标签: java spring scheduler

我尝试了以下方法来使用调度程序,但是找不到答案,所以我想问一个问题。 调度程序服务完全按时运行。 但是,仅运行“ sessionService.getUserInfo()”。

我们不在youtubeSearchService.searchYoutube和youtubeSearchService.searchYoutube上进行任何工作。

为什么只有一项服务有效。我想知道如何使用多种服务。

@Component
public class SchedulerService {
    private static final Logger logger = LoggerFactory.getLogger(SchedulerService.class);

    @Autowired
    private YoutubeSearchService youtubeSearchService;

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private  SessionService sessionService;

    public static int i = 0;
    public static int j = 0;


    @Scheduled(cron="0 40 0/1 * * *") 
    public void weatherSchedulerService() throws Exception { 
        ++i;
        weatherService.insertRTweather(sessionService.getUserInfo());
    } 


    @Scheduled(cron = "0 0/1 * * * *") 

    public void youtubeSearchSchedulerService() throws Exception { 
        ++j;
        youtubeSearchService.searchYoutube(sessionService.getUserInfo(),j);
    }

}

1 个答案:

答案 0 :(得分:-1)

我曾经在Spring Scheduler中遇到过挑战,尤其是在基于注释的配置中,并最终使用了以下解决方案,希望对您有所帮助:

使用石英的SchedulerConfig.java

@Configuration
public class SchedulerConfig
{

private static final Logger logger = Logger.getLogger(SchedulerConfig.class.getName());

@Autowired
private ApplicationContext applicationContext;

@Bean
public SpringBeanJobFactory springBeanJobFactory()
{
    AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
    logger.info("Configuring Job factory");

    jobFactory.setApplicationContext(applicationContext);
    return jobFactory;
}

@Bean
public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job)
{

    SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
    schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));

    logger.info("Setting the Scheduler up");
    schedulerFactory.setJobFactory(springBeanJobFactory());
    schedulerFactory.setJobDetails(job);
    schedulerFactory.setTriggers(trigger);

    return schedulerFactory;
}

@Bean
public JobDetailFactoryBean jobDetail()
{

    JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
    jobDetailFactory.setJobClass(SchedulerService.class);
    jobDetailFactory.setName("Qrtz_Job_Detail");
    jobDetailFactory.setDescription("Invoke Sample Job service...");
    jobDetailFactory.setDurability(true);
    return jobDetailFactory;
}

@Bean
public CronTriggerFactoryBean trigger(JobDetail job)
{

    CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
    //three am
    trigger.setCronExpression("0 0 3 * * ?");                
    trigger.setJobDetail(job);
    trigger.setName("Qrtz_Trigger");
    return trigger;
}

}

quartz.properties:将其放在classptah中

# thread-pool
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=1

org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

# job-store
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

# others
org.quartz.jobStore.misfireThreshold = 60000

您的调度程序服务定义如下:

@Component
public class SchedulerService implements Job {


@Autowired
private YoutubeSearchService youtubeSearchService;

@Autowired
private WeatherService weatherService;

@Autowired
private  SessionService sessionService;


public void execute(JobExecutionContext context) throws JobExecutionException
{
    weatherService.insertRTweather(sessionService.getUserInfo());
} 
}

如果您要两个定义多个时间表,则可以通过添加作业细节并触发将其添加到schedulerconfig。

别忘了添加石英jar文件

<dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>