redis-StringRedisTemplate和ValueOperations用法&quartz-SimpleScheduleBuilder用法

一、StringRedisTemplate用法
二、ValueOperations用法
三、SimpleScheduleBuilder用法

  1. 首先设定一个定时器
    quartzJob()方法中,对任务进行派遣,
    newJob():通过静态方法创建一个对象实例,或并且制定他的jobType 类型;
    SyncDoJobServiceImpl.class:指的是一个执行定时任务的类;
    withIdentity():就是指定job的名字(一般是SyncDoJobServiceImpl类名),这里名字类型为JobKey。如果没有指定名字,则在Builder的时候制定一个GUID;
    storeDurably():Job持久化 ,默认情况下 job没有trigger的时候会被删除,设置为true则不会删除,默认值是true
    然后创建build()

    quartzTrigger()方法中,指定调度的间隔和重复的次数;
    withIntervalInMilliseconds():指定间隔( 毫秒);
    withIntervalInSeconds():指定间隔( 秒);
    withIntervalInMinutes():指定间隔(分);
    withIntervalInHours():指定间隔(小时);
    repeatForever():永远重复;
    withRepeatCount():重复多少次;

    newTrigger():new一个trigger对象;
    forJob():执行任务派遣quartzJob()
    withIdentity():指定一个TriggerKey(trigger名字);
    withSchedule(): 设置ScheduleBuilder将用于定义触发器的时间表的。

    @Slf4j
    @Configuration
    public class QuartzConfig  {
    
        /**
         * 任务派遣
         */
        @Bean
        public JobDetail quartzJob(){
            return JobBuilder.newJob(SyncDoJobServiceImpl.class).withIdentity("SyncDoJobServiceImpl").storeDurably().build();
        }
    
        /**
         * 触发器
         * @return
         */
        @Bean
        public Trigger quartzTrigger(){
            SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(2)
                    .repeatForever();
    
            log.info("触发了触发器");
            return TriggerBuilder.newTrigger().forJob(quartzJob()).withIdentity("weatherQuartzTrigger")
                    .withSchedule(simpleScheduleBuilder).build();
        }
    }
    
  2. 具体的定时任务
    这里使用了一个例子,通过json数据转为实体类,来获取城市的信息。(可以自定义一个任务)

    @Slf4j
    public class SyncDoJobServiceImpl extends QuartzJobBean {
        @Autowired
        private WeatherService weatherService;
    
        @Override
        protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            //遍历出 5个城市的,得到单个的城市对象,然后调用 WeatherService 接口
    
            log.info("开始 执行任务");
            List<CityObj> cityObjs = null;
            try {
                cityObjs = JsonToCityUtil.getInstance().readJson();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            //遍历城市对象
            for(CityObj city : cityObjs){
                try {
                    //10秒遍历一个城市
                    Thread.sleep(10000);
                    //得到单个城市的名字
                    log.info("遍历的城市名字是:" + city.getCity_name()+ "----------");
                    log.info("时间过了10秒");
                    weatherService.setWeatherInfoToRedis(city.getCity_code());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    log.info("定时任务出现异常!");
                }
            }
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值