spring定时器使用注解@Scheduled执行定时任务的小例子

下面是简单的定时任务,其中:

(一)和(二)都是关于查询id与name的关系的map集合;
(三)是关于查询id与user对象的关系的map集合;
<<有关属性介绍均转载自Pagegle博主的文章特此说明>>

@Scheduled()中的属性介绍说明

① 注解@Scheduled 可以作为一个触发源添加到一个方法中,例如,以下的方法将以一个固定延迟(fixedDelay)时间5秒钟调用一次执行,这个周期是以上一个调用任务的完成时间为基准,在上一个任务完成之后,5s后再次执行:

	@Scheduled(fixedDelay=5000)
	public void doSomething() {    
	    // 定时的去执行的方法
	}

② 如果需要以固定速率执行,只要将注解中指定的属性名称改成(fixedRate)即可,以下方法将以一个固定速率5s来调用一次执行,这个周期是以上一个任务开始时间为基准,从上一任务开始执行后5s再次调用:

	@Scheduled(fixedRate=5000)
	public void doSomething() {    
	    // 定时的去执行的方法
	}

③ 对于固定延迟固定速率的任务,可以指定一个"初始延迟"表示该方法在第一被调用执行之前等待的毫秒数:

	@Scheduled(initialDelay=5000, fixedRate=5000)
	public void doSomething() {    
	    // 初始延迟5s,执行完后每隔5s执行一次
	}

④ 如果简单的定期调度不能满足,那么cron表达式提供了可能。例如,下面的方法将只会在工作日执行:

	@Scheduled(cron="*/5 * * * * MON-FRI")
	public void doSomething() {    
	    // 只会在工作日执行的方法
	}

以下小案例,是定时扫描查询一些数据并返回相应的Map集合:

@Service
public class TimingTask {
    public final static String MY_RULE_OF_CANSHU = "thesomestrings";

    public final static long TEN_MINUTES = 10 * 60 * 1000;

    public final static long ONE_MINUTES = 60 * 1000;

    public static final ConcurrentMap<String, String> ID_NAME_MAPPING = new ConcurrentHashMap<>();
    
    public static final ConcurrentMap<Long, UserInfo> ID_USER_MAPPING = new ConcurrentHashMap<>();

    @Autowired
    private UserMapper userMapper;

    /**
     * 定时任务,扫描数据库表中的id与name的对应关系,同步到ID_NAME_MAPPING供查询。
     * fixedDelay = TEN_MINUTES 延迟十分钟 == public final static long TEN_MINUTES = 10 * 60 * 1000;
     */
    @Scheduled(fixedDelay = TEN_MINUTES)
    public void scheduleUserInfo() {
    	//根据规则参数'MY_RULE_OF_CANSHU'查询相应的user用户
        UserInfo user = userDao.selectUserIfonByRule(MY_RULE_OF_CANSHU);
        //根据用户id查询用户信息集合
        List<UserInfo> userInfoList = userMapper.selectUserInfoListById(user.getId());
        //对userInfoList进行判空
        if (CollectionUtils.isNotEmpty(userInfoList)) {
        	//userInfoList非空,通过for循环依次将list集合信息放入到map集合'ID_NAME_MAPPING'中
            for (UserInfo user : userInfoList) {
                ID_NAME_MAPPING.put(user.getId(), user.getName());
            }
        }
    }

    /**
     * 定时任务,扫描user表中的id与name的对应关系,同步到ID__NAME_MAPPING供查询。
     * fixedDelay = ONE_MINUTES 延迟一分钟 == public final static long ONE_MINUTES = 60 * 1000;
     */
    @Scheduled(fixedDelay = ONE_MINUTES)
    public void scheduleUserInfo() {
    	//new一个example对象
        UserInfoExample example = new UserInfoExample();
        //添加条件 example对象的enabled属性为true
        example.createCriteria().andEnabledEqualTo(true);
        List<UserInfo> userInfoList = userMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(userInfoList)) {
        		//list集合非空,通过stream流将集合put进Map集合'ID_NAME_MAPPING'==(id,name) 
                ID_NAME_MAPPING.putAll(userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, UserInfo::getName)));
        }
    }

    /**
     * 定时任务,扫描数据库表中的id与user对象的对应关系,同步到ID_USER_MAPPING供查询。
     * fixedDelay = ONE_MINUTES 延迟一分钟 == public final static long ONE_MINUTES = 60 * 1000;
     */
    @Scheduled(fixedDelay = ONE_MINUTES)
    public void scheduleTemplateSceneMapping() {
    	//new一个example对象
        UserInfoExample example = new UserInfoExample();
        List<UserInfo> userInfoList = userMapper.selectByExample(example);
		//判断list集合是否"非空"
        if (CollectionUtils.isNotEmpty(userInfoList)) {
			//list集合非空,通过stream流将集合put进Map集合'ID_USER_MAPPING'==(id,user对象)            
            ID_USER_MAPPING.putAll(userInfoList .stream().collect(Collectors.toMap(UserInfo::getId, x -> x)));
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值