商品开售订阅通知设计(二)

实现

下面是核心代码的实现,为了保证对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;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知始行末

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值