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

17 篇文章 0 订阅
3 篇文章 0 订阅

直接上代码:

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;

@Component
@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 * * ?")即可。

其中:

       @Component注册为bean

       @EnableScheduling使其装配成定时器

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

 

 

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值