redis list数据结构实现队列

消息队列的好处

提高响应速度、解耦、稳定性(故障处理)、可扩展性、有序性、异步性

redis实现消息队列方法

  • 抓取异常,封装异常对象,插入redis队列中
try{
    // 业务代码
}catch (ServiceException e){
    // 异常信息封装
    Access access = '封装逻辑';
    // 调用redis 
    redisSentinelProxy.Lpush(errorCode, access);
    continue;
}


  • 监听redis队列
@Component
@Slf4j
public class AccessRedis {

    // 线程数
    private int threadNum = 5;
    
    if (threadNum <= 0) {
        threadNum = 5;
    }

    // 此注解的作用在对象注入容器后执行给注解修饰的方法,且只执行一次
    @PostConstruct
    public void init() {
       for (int i = 0; i <= threadNum ; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 此方法内部为死循环从redis队列中读取数据,有就处理,没有就继续循环
                    accessHandler.doBusiness(null);
                }
            }).start();
        }
    }

}



@Service
public class AccessHandler {

    boolean flag = true;
    // 死循环
    while (flag) {
        try {
         // 从redis队列中读取数据
         TAccess access = redisSentinelProxy.bRpop(errorCode, 0, TAccess.class);
         // 异常信息如表
         sql语句
        } catch () {
           
        }
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值