spring定时任务的实现(用cron表达式)

spring定时任务

1.在spring下的task.xml中添加配置

      spring定时任务的实现(用cron表达式)_第1张图片



    

    

    
    


    
    


2.新建Java类

package com.ybl.task;

import com.ybl.clickcube.admin.advertiser.mapper.AdvertiserDOMapper;
import com.ybl.clickcube.admin.agency.mapper.AgencyDOMapper;
import com.ybl.clickcube.notification.bean.NotificationDO;
import com.ybl.clickcube.notification.mapper.NotificationDOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by wangxin on 2018/4/4.
 */
@Component
public class TimerTask {
    @Autowired
    AdvertiserDOMapper advertiserDOMapper;
    @Autowired
    NotificationDOMapper notificationDOMapper;
    @Autowired
    AgencyDOMapper agencyDOMapper;

    @Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
    public void notifiTimer() {
        System.out.println("开始查询");
        List advertiserIdList=advertiserDOMapper.selectuserIdListByAccount();
        List agencyIdList=agencyDOMapper.selectuserIdListByAccount();
        advertiserIdList.addAll(agencyIdList);
        System.out.println(advertiserIdList);
        if(advertiserIdList.size()!=0) {
            notificationDOMapper.insertIdIn(advertiserIdList);
        }
        else{
        }
    }
}

3.corn解释

 

Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: 

Seconds Minutes Hours DayofMonth Month DayofWeek Year或 
Seconds Minutes Hours DayofMonth Month DayofWeek

每一个域可出现的字符如下: 
Seconds:可出现", - * /"四个字符,有效范围为0-59的整数 
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数 
Hours:可出现", - * /"四个字符,有效范围为0-23的整数 
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数 
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc 
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推 
Year:可出现", - * /"四个字符,有效范围为1970-2099年

 

你可能感兴趣的:(后端)