使用@Scheduled实现定时任务
用一个实现签到功能的Demo工程为例
1.SignApplication 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author hzc8893 2023/1/2 12:25
* @EnableAsync 使用异步方法
* @EnableScheduling 开启任务
*/
@SpringBootApplication
@EnableAsync
@EnableScheduling
public class SignApplication {
public static void main(String[] args) {
SpringApplication.run(SignApplication.class, args);
}
}
2.RedisConfig Redis序列化配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author hzc8893 2023/1/2 12:25
*/
@Configuration
public class RedisConfig {
/**
* springboot 默认帮我们创建的RedisTemplate的key和value的序列化方式是jdk默认的方式,
* 我们有时候手动向redis中添加的数据可能无法被查询解析出来,所以我们需要修改序列化方式
* @param connectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer); //设置key的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);//设置hash类型的数据的key的序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);//非final类型的数据才会被序列化
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//设置value的序列化方式为json
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
3.MyTask 定时任务配置类
@Scheduled 中需要使用cron表达式
可以去这个网站上便捷生成Cron表达式:quartz/Cron/Crontab表达式在线生成工具-BeJSON.com
我使用的cron表达式"20 * * * * ?", 表示会在每分钟的第20秒执行该定时任务去删除Redis中的对应数据
import com.hzc.contant.SignPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Set;
/**
* @author hzc8893 2023/1/2 12:25
*/
@Configuration
public class MyTask {
@Autowired
private RedisTemplate redisTemplate;
@Async
@Scheduled(cron = "20 * * * * ?")
public void myTask() {
Set<String> keys = redisTemplate.keys(SignPrefix.SIGN_PREFIX + "*");
if (keys != null) {
redisTemplate.delete(keys);
}
}
}
标签:Scheduled,springframework,任务,org,import,定时,序列化,annotation,redisTemplate
From: https://www.cnblogs.com/hzxc/p/17019760.html