自己实现重试队列

基于Disruptor的重试队列实现

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import java.util.concurrent.Executors;

public class RetryQueueWithDisruptor {
    // 事件类
    static class RetryEvent {
        private Runnable task;
        private int retryCount;
        
        // getters and setters
    }

    // 事件工厂
    static class RetryEventFactory implements EventFactory<RetryEvent> {
        @Override
        public RetryEvent newInstance() {
            return new RetryEvent();
        }
    }

    // 事件处理器
    static class RetryEventHandler implements EventHandler<RetryEvent> {
        private final int maxRetries;
        
        public RetryEventHandler(int maxRetries) {
            this.maxRetries = maxRetries;
        }

        @Override
        public void onEvent(RetryEvent event, long sequence, boolean endOfBatch) {
            try {
                event.getTask().run();
            } catch (Exception e) {
                if (event.getRetryCount() < maxRetries) {
                    event.setRetryCount(event.getRetryCount() + 1);
                    // 计算退避时间
                    long delay = calculateBackoff(event.getRetryCount());
                    // 实际应用中应使用定时器重新发布事件
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                    // 重新发布事件进行重试
                    disruptor.publishEvent((retryEvent, seq) -> {
                        retryEvent.setTask(event.getTask());
                        retryEvent.setRetryCount(event.getRetryCount());
                    });
                } else {
                    // 达到最大重试次数,记录失败
                    System.err.println("任务重试失败: " + e.getMessage());
                }
            }
        }

        private long calculateBackoff(int retryCount) {
            return (long) Math.min(1000 * Math.pow(2, retryCount), 60000);
        }
    }

    private static Disruptor<RetryEvent> disruptor;

    public static void start(int bufferSize, int maxRetries) {
        RetryEventFactory factory = new RetryEventFactory();
        disruptor = new Disruptor<>(
            factory,
            bufferSize,
            Executors.defaultThreadFactory(),
            ProducerType.MULTI,
            new BlockingWaitStrategy()
        );
        
        disruptor.handleEventsWith(new RetryEventHandler(maxRetries));
        disruptor.start();
    }

    public static void submitTask(Runnable task) {
        disruptor.publishEvent((event, sequence) -> {
            event.setTask(task);
            event.setRetryCount(0);
        });
    }
}

基于LinkedBlockingQueue的重试队列实现

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class RetryQueueWithBlockingQueue {
    private final BlockingQueue<Runnable> queue;
    private final ExecutorService executor;
    private final int maxRetries;
    
    public RetryQueueWithBlockingQueue(int queueSize, int threadPoolSize, int maxRetries) {
        this.queue = new LinkedBlockingQueue<>(queueSize);
        this.executor = Executors.newFixedThreadPool(threadPoolSize);
        this.maxRetries = maxRetries;
        startConsumers();
    }
    
    private void startConsumers() {
        for (int i = 0; i < executor.getMaximumPoolSize(); i++) {
            executor.submit(() -> {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        Runnable task = queue.take();
                        executeWithRetry(task);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            });
        }
    }
    
    public boolean submit(Runnable task) {
        return queue.offer(task);
    }
    
    private void executeWithRetry(Runnable task) {
        AtomicInteger retryCount = new AtomicInteger(0);
        while (retryCount.get() < maxRetries) {
            try {
                task.run();
                return;
            } catch (Exception e) {
                int count = retryCount.incrementAndGet();
                if (count >= maxRetries) {
                    System.err.println("任务重试失败: " + e.getMessage());
                    return;
                }
                long delay = calculateBackoff(count);
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        }
    }
    
    private long calculateBackoff(int retryCount) {
        return (long) Math.min(1000 * Math.pow(2, retryCount), 60000);
    }
    
    public void shutdown() {
        executor.shutdownNow();
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值