All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.delaytask.impl.BoundedDelayQueueProducer Maven / Gradle / Ivy

The newest version!
/*
 * @(#)BoundedDelayQueueProducer.java	2022-4-9
 *
 * Copyright (c) 2022. All Rights Reserved.
 *
 */

package com.github.javaclub.delaytask.impl;

import java.time.Duration;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.javaclub.delaytask.DelayJob;
import com.github.javaclub.delaytask.DelayQueueProducer;

/**
 * BoundedDelayQueueProducer
 *
 * @author Gerald Chen
 * @version $Id: BoundedDelayQueueProducer.java 2022-4-9 11:34:29 Exp $
 */
public class BoundedDelayQueueProducer implements DelayQueueProducer {

	private final Logger logger = LoggerFactory.getLogger(BoundedDelayQueueProducer.class);
	 
    private DelayQueueProducer delegate;
    
    /**
     * Warning: 按每个job 512字节计算,200,000个job将占用redis约100MB内存 * 后续需要考虑对queue分片
     */
    private int maxQueueSize = 200000;
 
    private int warningSize = 100000;
 
    public void setDelegate(DelayQueueProducer delegate) {
		this.delegate = delegate;
	}

	public void setMaxQueueSize(int maxQueueSize) {
		this.maxQueueSize = maxQueueSize;
	}

	public void setWarningSize(int warningSize) {
		this.warningSize = warningSize;
	}

	@Override
    public void submit(String queue, DelayJob job, Duration delayed) {
        int queueSize = delegate.getQueueSize(queue);
        if (queueSize > maxQueueSize) {
            logger.error("task queue:{} exceed limit:{}, task discard", queue, maxQueueSize);
        } else if (queueSize > warningSize) {
            logger.warn("task queue:{} size warning, current size:{}", queue, queueSize);
            delegate.submit(queue, job, delayed);
        } else {
            logger.debug("task queue:{}, size:{}", queue, queueSize);
            delegate.submit(queue, job, delayed);
        }
    }
 
    @Override
    public boolean update(String queue, DelayJob job, Duration delayed) {
        return delegate.update(queue, job, delayed);
    }
    
 
    @Override
	public void submit(String queue, DelayJob job, Date futureTime) {
    	int queueSize = delegate.getQueueSize(queue);
        if (queueSize > maxQueueSize) {
            logger.error("task queue:{} exceed limit:{}, task discard", queue, maxQueueSize);
        } else if (queueSize > warningSize) {
            logger.warn("task queue:{} size warning, current size:{}", queue, queueSize);
            delegate.submit(queue, job, futureTime);
        } else {
            logger.debug("task queue:{}, size:{}", queue, queueSize);
            delegate.submit(queue, job, futureTime);
        }
	}

	@Override
	public boolean update(String queue, DelayJob job, Date futureTime) {
		return delegate.update(queue, job, futureTime);
	}

	@Override
    public void cancel(String queue, DelayJob job) {
        delegate.cancel(queue, job);
    }
 
    @Override
    public String getGroupId() {
        return delegate.getGroupId();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy