`
sillycat
  • 浏览: 2543094 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot and RESTful API(7)scheduled Tag

 
阅读更多
Spring Boot and RESTful API(7)scheduled Tag

Enable the Schedule on Application
package com.sillycat.jobsmonitorapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class JobsMonitorAPIApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(JobsMonitorAPIApplication.class);
    }

}

Cron Service Class
package
com.sillycat.jobsmonitorapi.service;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.j2c.jobsmonitorapi.domain.Job;
import com.j2c.jobsmonitorapi.repository.JobRepositorySolr;

@Component
public class
JobCronService {

    @Autowired

    JobRepositorySolr    jobRepositorySolr;

    @Scheduled(cron = "${cron.everyminutes}")
    public void hourlyRecordJobCount() {
        // https://crontab.guru/every-1-hour
        Iterator<Job> jobsIt = jobRepositorySolr.findAll().iterator();
        @SuppressWarnings("unchecked")
        List<Job> jobs = IteratorUtils.toList(jobsIt);
        System.out.println("logging----" + jobs);
    }
}

Set up application.yaml every 10 seconds
cron:
    everyminutes: '*/10 * * * * *'


References:
http://www.baeldung.com/spring-scheduled-tasks




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics