Spring boot设置定时器类的简单方法

直接上代码:

package com.ismartgo.uqcode.schedule;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ismartgo.uqcode.common.utils.DateUtil;
import com.ismartgo.uqcode.mapper.UqcGameMapper;
import com.ismartgo.uqcode.mapper.UqcStatsProductDateMapper;
import com.ismartgo.uqcode.model.UqcProduct;
import com.ismartgo.uqcode.model.UqcStatsProductDate;
import com.ismartgo.uqcode.service.ProductService;
import com.ismartgo.uqcode.service.StatsProductDateService;

@Configuration
@Lazy(false)
@EnableAsync
@EnableScheduling
public class StatsProductDateSchedule {
	
	@Autowired
	private UqcStatsProductDateMapper statsProductDateMapper;
	
	@Autowired
	private StatsProductDateService statsProductDateService;
	
	@Autowired
	private UqcGameMapper gameMapper;
	
	@Autowired
	private ProductService productService;
	
	//每天晚上凌晨1点执行,统计上一天的数据
	@Scheduled(cron = "0 0 1 * * ?")
	public void excute() {
		//首先今天零点时间
		Date todayDate = DateUtil.getTodayDate();
		//然后得到上一天零点时间
		Date lastDayDate = DateUtil.getDateAddDay(todayDate, -1);
		
		List<String> list = gameMapper.getAllsysTenantCode();
		for (String sysTenantCode : list) {
			//根据系统商户编号查询产品列表
			UqcProduct product = new UqcProduct();
			product.setSysTenantCode(sysTenantCode);
			List<UqcProduct> products = productService.queryList(product);
			
			for (UqcProduct p : products) {
				UqcStatsProductDate statsProductDate = new UqcStatsProductDate();
				statsProductDate.setStatsDate(lastDayDate);
				statsProductDate.setSysTenantCode(sysTenantCode);
				statsProductDate.setProductId(p.getId());
				statsProductDate = statsProductDateService.queryOne(statsProductDate);
				if(statsProductDate == null) {
					statsProductDate = new UqcStatsProductDate();
					statsProductDate.setStatsDate(lastDayDate);
					statsProductDate.setSysTenantCode(sysTenantCode);
					statsProductDate.setProductId(p.getId());
					
					statsProductDate.setJoinQty(statsProductDateMapper.getJoinQtyByDate(lastDayDate, todayDate, p.getId(),sysTenantCode));
					statsProductDate.setJoinUserQty(statsProductDateMapper.getJoinUserQtyByDate(lastDayDate, todayDate, p.getId(), sysTenantCode));
					statsProductDateService.save(statsProductDate);
				}else {
					
					statsProductDate.setJoinQty(statsProductDateMapper.getJoinQtyByDate(lastDayDate, todayDate, p.getId(),sysTenantCode));
					statsProductDate.setJoinUserQty(statsProductDateMapper.getJoinUserQtyByDate(lastDayDate, todayDate, p.getId(), sysTenantCode));
					statsProductDateService.update(statsProductDate);
				}
			}
		}
		
	}

}

在spring boot的项目中,直接在类加入注解@Component,@Configuration,@Lazy(false),@EnableAsync,@EnableScheduling。在方法加上注解@Scheduled(cron = "0 0 1 * * ?")即可。

其中:

       @Configuration注册为bean

       @Lazy(false)为非延迟加载(即预加载bean)

       @EnableAsync使其为异步操作,即开启多线程意思

       @EnableScheduling使其装配成定时器

       @Scheduled(cron = "0 0 1 * * ?")为定时器执行的定时规则,这里为每天晚上凌晨1点执行

猜你喜欢

转载自blog.csdn.net/syilt/article/details/91399207