spring-boot-starter-quartz配置定时任务及Scheduling配置定时任务

欧阳小白 1年前 ⋅ 4151 阅读

1.spring-boot-starter-quartz配置定时任务

引入依赖(不要找错):

 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-quartz -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

执行类:

package com.demo.weather.job;

import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;



/**
 * Weather Data Sync Job.
 * 
 * @since 1.0.0 2017年11月23日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public class WeatherDataSyncJob extends QuartzJobBean {
	
	private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);  

	/* (non-Javadoc)
	 * @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
	 */
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		logger.info("Weather Data Sync Job. Start!");
		logger.info("Weather Data Sync Job. End!");   //定时执行
	}

}
logger.info("Weather Data Sync Job. Start!"); logger.info("Weather Data Sync Job. End!"); //定时执行 } }

配置类:

package com.demo.weather.config;

import com.demo.weather.job.WeatherDataSyncJob;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Quartz Configuration.
 * 
 * @since 1.0.0 2017年11月23日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@Configuration
public class QuartzConfiguration {

	private static final int TIME = 2; // 更新频率

	// JobDetail
	@Bean
	public JobDetail weatherDataSyncJobDetail() {
		return JobBuilder.newJob(WeatherDataSyncJob.class).withIdentity("weatherDataSyncJob")
		.storeDurably().build();
	}
	
	// Trigger
	@Bean
	public Trigger weatherDataSyncTrigger() {
		
		SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder.simpleSchedule()
				.withIntervalInSeconds(TIME).repeatForever();
		
		return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
				.withIdentity("weatherDataSyncTrigger").withSchedule(schedBuilder).build();
	}
}
private static final int TIME = 2; // 更新频率 // JobDetail @Bean public JobDetail weatherDataSyncJobDetail() { return JobBuilder.newJob(WeatherDataSyncJob.class).withIdentity("weatherDataSyncJob") .storeDurably().build(); } // Trigger @Bean public Trigger weatherDataSyncTrigger() { SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(TIME).repeatForever(); return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail()) .withIdentity("weatherDataSyncTrigger").withSchedule(schedBuilder).build(); } }

2.Scheduling配置定时任务(quartz视乎不能定多少点的任务,所以用这个)

package com.yizhen;

import com.yizhen.dataobject.MidTable;
import com.yizhen.service.MidTableService;
import com.yizhen.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@SpringBootApplication
@EnableScheduling
public class HotelmanageApplication {
	@Autowired
	private RoomService roomService;
	@Autowired
	private MidTableService midTableService;

	public static void main(String[] args) {
		SpringApplication.run(HotelmanageApplication.class, args);
	}
	//    每分钟启动
	//@Scheduled(cron = "0 0/1 * * * ?")
	@Scheduled(cron = "0 0 2 * * ?")//凌晨两点执行
	public void updatejiageandfangjianshuliang(){
		Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String nowDay = sf.format(now);
		List<MidTable> midTableList=midTableService.findnowmidtables(nowDay);
		for(MidTable midTable:midTableList){
			Integer roomid=midTable.getMidTablePk().getRoom_id();
			roomService.updateroomnumbyfangtaiweihu(Integer.valueOf(midTable.getFangtai()),roomid);
			roomService.updateroompricebyfangtaiweihu(midTable.getPrice(),roomid);
		}
		System.out.println("更新成功!");
	}
}
@EnableScheduling public class HotelmanageApplication { @Autowired private RoomService roomService; @Autowired private MidTableService midTableService; public static void main(String[] args) { SpringApplication.run(HotelmanageApplication.class, args); } // 每分钟启动 //@Scheduled(cron = "0 0/1 * * * ?") @Scheduled(cron = "0 0 2 * * ?")//凌晨两点执行 public void updatejiageandfangjianshuliang(){ Date now = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); String nowDay = sf.format(now); List<MidTable> midTableList=midTableService.findnowmidtables(nowDay); for(MidTable midTable:midTableList){ Integer roomid=midTable.getMidTablePk().getRoom_id(); roomService.updateroomnumbyfangtaiweihu(Integer.valueOf(midTable.getFangtai()),roomid); roomService.updateroompricebyfangtaiweihu(midTable.getPrice(),roomid); } System.out.println("更新成功!"); } }

启动类中配置

 


全部评论: 0

    我有话说:

    咨询问题

    点击这里给我发消息

    淘宝咨询

    博主承接代做业务

    真·送云服务器

    咨询博主