一. 导入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
|
二. 模型数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package io.coderyeah.ymcc.dto;
import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data;
import java.util.Date;
@Data public class GoodsDTO { private Long id; private Integer num; private String name; private Long userId; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Long time = new Date().getTime(); }
|
三. 自定义redis模板配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package io.coderyeah.ymcc.config;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
@Configuration public class RedisConfig {
@Resource private RedisConnectionFactory factory;
@Bean public RedisTemplate<Object, Object> redisTemplate() { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory); GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(serializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(serializer); return redisTemplate; }
}
|
yml配置
1 2 3 4
| sue: spring: quartz: cron: "*/10 * * * * ?"
|
四. Quartz配置
定时任务代码功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package io.coderyeah.ymcc.quartz;
import com.alibaba.fastjson2.JSON; import io.coderyeah.ymcc.dto.GoodsDTO; import lombok.extern.slf4j.Slf4j; import org.quartz.JobExecutionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.List; import java.util.Set;
@Slf4j public class QuartzCartJob extends QuartzJobBean { @Autowired private RedisTemplate<Object, Object> redisTemplate;
@Override protected void executeInternal(JobExecutionContext context) { Set<Object> keys = redisTemplate.keys(("cache:goods:*")); assert keys != null; keys.forEach(key -> { List<Object> values = redisTemplate.opsForHash().values(key); List<GoodsDTO> goodsDTOS = JSON.parseArray(JSON.toJSONString(values), GoodsDTO.class); goodsDTOS.forEach(goodsDTO -> { if (System.currentTimeMillis() - goodsDTO.getTime() > 1000 * 60) { redisTemplate.opsForHash().delete(key, goodsDTO.getId().toString()); log.info("定时删除成功"); } }); }); System.out.println(keys);
String userName = (String) context.getJobDetail().getJobDataMap().get("userName"); System.out.println("userName:" + userName); } }
|
配置执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package io.coderyeah.ymcc.quartz;
import org.quartz.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class QuartzConfig { @Value("${sue.spring.quartz.cron}") private String cron;
@Bean public JobDetail quartzCartJobDetail() { JobDetail jobDetail = JobBuilder.newJob(QuartzCartJob.class) .withIdentity("quartzCartJobDetail", "QUARTZ_CART") .usingJobData("userName", "coderyeah") .storeDurably() .build(); return jobDetail; }
@Bean public Trigger quartzCartJobTrigger() { CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron); Trigger trigger = TriggerBuilder.newTrigger() .forJob(quartzCartJobDetail()) .withIdentity("quartzCartJobTrigger", "QUARTZ_CART_JOB_TRIGGER") .withSchedule(cronScheduleBuilder) .build(); return trigger; } }
|
Springboot使用Quartz定时任务删除redis中的物品