Simple way to set the timer class Spring boot

Directly on the code:

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);
				}
			}
		}
		
	}

}

In spring boot project directly add comments @ Component, @ Configuration, @ Lazy (false), @ EnableAsync, @ EnableScheduling in class. In the method of annotate @Scheduled (cron = "0 0 1 * *?") Can be.

among them:

       @Configuration registered as bean

       @Lazy (false) non-lazy loading (i.e., pre-loading the bean)

       @EnableAsync it is asynchronous operation, meaning that is open multithreading

       @EnableScheduling assembled so that the timer

       @Scheduled (cron = "0 0 1 * *?") Timer timing rules for the execution, performed here for the 1:00 pm every day

 

 

Guess you like

Origin blog.csdn.net/syilt/article/details/91399207