一、订单撤单,我们用到的流程,虽然我到现在还没明白为什么这么用
代码:
1 对Redis订阅发布的设置
@Bean public RedisMessageListenerContainer configRedisMessageListenerContainer(Executor executor) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); // 设置Redis的连接工厂 container.setConnectionFactory(redisTemplate.getConnectionFactory()); // 设置监听使用的线程池 container.setTaskExecutor(executor); // 设置监听的Topic ChannelTopic channelTopic = new ChannelTopic("__keyevent@1__:expired"); // 设置监听器 container.addMessageListener(messageListener, channelTopic); return container; }
2 对redis的连接工厂
container.setConnectionFactory(redisTemplate.getConnectionFactory());
3 线程池的引用以及初始化
// 设置监听使用的线程池 container.setTaskExecutor(executor);
线程池参数的设置
// 配置线程池 @Bean public Executor myTaskAsyncPool() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(config.getCorePoolSize()); executor.setMaxPoolSize(config.getMaxPoolSize()); executor.setQueueCapacity(config.getQueueCapacity()); executor.setKeepAliveSeconds(config.getKeepAliveSeconds()); // rejection-policy:当pool已经达到max size的时候,如何处理新任务 // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }
4 topic的订阅发布 1对多(还有很多其他的模式,参考我其他博客)
5 topic监听器(主要对撤单进行监听)
@Autowired private OrdersService ordersService; @Override public void onMessage(Message message, byte[] pattern) {// 客户端监听订阅的topic,当有消息的时候,会触发该方法 byte[] body = message.getBody();// 请使用valueSerializer String itemValue = new String(body); // 请参考配置文件,本例中key,value的序列化方式均为string。 if (itemValue.startsWith(SystemConstant.REDIS_EXPIRE_KEY)) { ordersService.optListenerExpireKey(itemValue); } }
对Redis的操作类
@Component public class RedisOperationDao { @Autowired private RedisTemplate<String, String> redisTemplate; public void appendKeyValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public void deleteByKey(String key) { redisTemplate.opsForValue().set(key, "", 0); } public String getInfoByKey(String key) { return redisTemplate.opsForValue().get(key); } public void setex(String key, String value, long expire) { redisTemplate.opsForValue().set(key, value, expire, SECONDS); } public Long getExpire(String key) { return redisTemplate.getExpire(key); } }线程池的参数配置
@Component @ConfigurationProperties(prefix = "spring.task.pool") public class TaskThreadPoolConfig { private int corePoolSize; private int maxPoolSize; private int keepAliveSeconds; private int queueCapacity; public int getCorePoolSize() { return corePoolSize; } public void setCorePoolSize(int corePoolSize) { this.corePoolSize = corePoolSize; } public int getMaxPoolSize() { return maxPoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } public int getKeepAliveSeconds() { return keepAliveSeconds; } public void setKeepAliveSeconds(int keepAliveSeconds) { this.keepAliveSeconds = keepAliveSeconds; } public int getQueueCapacity() { return queueCapacity; } public void setQueueCapacity(int queueCapacity) { this.queueCapacity = queueCapacity; } }先更新这么多吧~ 代码看懂了 原理没弄懂~