MQ的定时任务改装,实现自定义定时:MQ+redis

package com.*.server.live.listener;

import com.*.common.core.entity.constant.LiveConstant;
import com.*.common.redis.service.RedisService;
import com.*.server.live.core.recreation.LiveRecreation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * 功能描述:自定义倒计时
 * 作者:唐泽齐
 */
@Slf4j
@Service
@RequiredArgsConstructor
@RocketMQMessageListener(consumerGroup = TopConstant.STUDIO_RECREATION, topic = TopConstant.STUDIO_RECREATION)
public class StudioRecreationRocketMQListener implements RocketMQListener {

    //rockteMQ延时 1s、 5s、 10s、 30s、 1m、 2m、 3m、 4m、 5m、 6m、 7m、 8m、 9m、 10m、 20m、 30m、 1h、 2h
    final static private List<Long> delays = Arrays.asList(1l,5l,10l,30l,1*60l,2*60l,3*60l,4*60l,5*60l,6*60l,7*60l,8*60l,9*60l,10*60l,20*60l,30*60l,1*60*60l,2*60*60l);

    final private RedisService redisService;

    final RocketMQTemplate rocketMQTemplate;

    final ExecutorService fixedThreadPool;

    final LiveRecreation liveRecreation;

    @Override
    public void onMessage(Object message) {
        log.info(LiveConstant.STUDIO_RECREATION+":"+message);
        if(!redisService.hasKey(message.toString())) {
            return;
        }
        //获取到期时间
        Long expire = redisService.getExpire(message.toString());
        //获取延时级别
        int delay = getDelay(expire-5l);
        //无需延时则立即执行
        if(delay == 0) {
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    liveRecreation.run(message.toString());
                }
            });
        }
        //需要延时则推送延时信息到MQ
        else {
            rocketMQTemplate.asyncSend(TopConstant.STUDIO_RECREATION, new GenericMessage<>(message.toString()), new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("Recreation===>delay::****未到时,继续等待====》"+message);
                }

                @SneakyThrows
                @Override
                public void onException(Throwable e) {
                    log.error("Recreation===>error::****计时异常,开始无限重试模式====》"+message,e);
                    onMessage(message);
                }
            },60l,delay);
        }
    }

    /**
     *
     * 获取延时级别
     * @return
     */
    private Integer getDelay(Long delay) {
        int level= 0;
      //如果小于等于1秒则无需延时
      if(delay>1l){
          for (int i = 0; i < delays.size(); i++) {
              if(delay<delays.get(i)) {
                  break;
              }
              level =  i;
          }
      }
      return level;
    }
}

 

posted on 2023-02-13 14:29  instr  阅读(54)  评论(0编辑  收藏  举报

导航