实现
下面是核心代码的实现,为了保证对Redis的原子性操作,采用lua脚本减少网络传输,避免并发操作的时候没有获取到任务的线程,对zrem的竞争。
lua脚本
获取zset中当前时间前的任务,从延时队列中删除并推送到执行队列
-- created by zy
-- time:2021-03-23 21:11:23
-- KEYS: [1]job:sleeping, [2]job:ready
-- ARGS: [1]currentTime
-- Comments: result is the job id
local key = KEYS[1]
local currentTime = ARGV[1]
local result = KEYS[2]
local jobs=redis.call('zrangebyscore', key, '-inf', currentTime)
local count = table.maxn(jobs)
if count>0 then
-- Comments: remove from Sleeping Job sorted set
redis.call('zremrangebyscore', key, '-inf', currentTime)
-- Comments: add to the Ready Job list
-- Comments: can optimize to use lpush id1,id2,... for better performance
for i=1,count do
redis.call('lpush', result, jobs[i])
end
return true
else
return false
end
延时任务拉取
拉取当前时间近3秒内的任务,没有任务让当前线程等待500ms,节约cpu.
package com.zy.blog.redis;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
/**
* @author zy
* @Description:
*/
@Component
public class DelayQueue {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedisScript redisScript;
public static final String NAME_SPACE = "zy:goods:";
@PostConstruct
public void loop(){
new DelayTask().start();
}
public class DelayTask extends Thread{
@Override
public void run() {
while (!this.isInterrupted()) {
List<String> keys = Arrays.asList(NAME_SPACE + "goods_sale", NAME_SPACE + "current_task");
Long saleTime = System.currentTimeMillis() + 3000;
Boolean result = (Boolean) redisTemplate.execute(redisScript, keys, saleTime);
if (!result) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
任务推送
package com.zy.blog.controller;
import com.alibaba.fastjson.JSONObject;
import com.zy.blog.redis.DelayQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zy
* @Description:
*/
@RestController
public class PushTaskController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/push/task")
public void pushTask() {
JSONObject data = new JSONObject();
data.put("sale_time", System.currentTimeMillis());
redisTemplate.opsForZSet().add(DelayQueue.NAME_SPACE + "goods_sale", data, System.currentTimeMillis());
}
}
redis序列化和lua脚本载入配置
package com.zy.blog.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
/**
* @author Gjing
**/
@Configuration
public class LuaConfiguration {
@Bean
public DefaultRedisScript<Boolean> redisScript() {
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("/script/delay.lua")));
redisScript.setResultType(Boolean.class);
return redisScript;
}
}
package com.zy.blog.config;
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.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
/**
* @author zy
* @date 2020-05-18 10:20
* @description redis 序列化配置
*/
@Configuration
public class RedisSerializeConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
{
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(jackson2JsonRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}