延迟队列2

1、队列工具类

import java.lang.reflect.Method;
import java.util.concurrent.DelayQueue;


public class DelayExecUtil {
/**
 * 延期执行队列
 */
private final static DelayQueue queue=new DelayQueue<>();

/**
 * 控制只允许启动一次
 */
private static boolean execFlag=false;

/**
 * 添加延期执行对象
 * @param delayObject
 */
public static void add(DelayObject delayObject){
    queue.add(delayObject);

    //添加元素后就开始执行
    execute();
}

public static void remove(DelayObject delayObject){
    queue.remove(delayObject);
}
private static void execute(){
    if(execFlag){
        return;
    }
    synchronized (DelayExecUtil.class){
        if(execFlag){
            return;
        }
        execFlag=true;
        new Thread(()->{
                while(true){
                    try {
                        DelayObject delay = queue.take();
                        Class aClass = delay.getCallBackInstance().getClass();
                        Object params = delay.getParams();
                        Method method;
                        if (null != params) {
                            method = aClass.getMethod(delay.getMethodName(), params.getClass());
                            method.invoke(delay.getCallBackInstance(), params);
                        } else {
                            method = aClass.getMethod(delay.getMethodName());
                            method.invoke(delay.getCallBackInstance());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        }).start();
    }
}
}

2、延迟执行对象

import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 延期执行对象
 */
public class DelayObject implements Delayed {
/**
 * 唯一id
 */
private String uuid;
/**
 * 延期执行时间  到达该时间后,当前任务执行
 */
private Date executeTime;
/**
 * 方法参数
 */
private Object params;
/**
 * 回调实例
 */
private Object callBackInstance;
/**
 * 回调方法名
 */
private String methodName;

public DelayObject(String uuid) {
    if(StringUtils.isEmpty(uuid)){
        throw new IllegalArgumentException("extendObject or extendObject.id is null");
    }

    this.uuid = uuid;
}

public Date getExecuteTime() {
    return executeTime;
}

public void setExecuteTime(Date executeTime) {
    this.executeTime = executeTime;
}

public Object getCallBackInstance() {
    return callBackInstance;
}

public void setCallBackInstance(Object callBackInstance) {
    this.callBackInstance = callBackInstance;
}

public String getMethodName() {
    return methodName;
}

public void setMethodName(String methodName) {
    this.methodName = methodName;
}

public Object getParams() {
    return params;
}

public void setParams(Object params) {
    this.params = params;
}

/**
 * Returns the remaining delay associated with this object, in the
 * given time unit.
 *
 * @param unit the time unit
 * @return the remaining delay; zero or negative values indicate
 * that the delay has already elapsed
 */
@Override
public long getDelay(TimeUnit unit) {
    return this.executeTime.getTime() - System.currentTimeMillis();
}
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    DelayObject that = (DelayObject) o;
    return Objects.equals(uuid, that.uuid);
}
/**
 * Compares this object with the specified object for order.  Returns a
 * negative integer, zero, or a positive integer as this object is less
 * than, equal to, or greater than the specified object.
 * 

*

The implementor must ensure sgn(x.compareTo(y)) == * -sgn(y.compareTo(x)) for all x and y. (This * implies that x.compareTo(y) must throw an exception iff * y.compareTo(x) throws an exception.) *

*

The implementor must also ensure that the relation is transitive: * (x.compareTo(y)>0 && y.compareTo(z)>0) implies * x.compareTo(z)>0. *

*

Finally, the implementor must ensure that x.compareTo(y)==0 * implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for * all z. *

*

It is strongly recommended, but not strictly required that * (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any * class that implements the Comparable interface and violates * this condition should clearly indicate this fact. The recommended * language is "Note: this class has a natural ordering that is * inconsistent with equals." *

*

In the foregoing description, the notation * sgn(expression) designates the mathematical * signum function, which is defined to return one of -1, * 0, or 1 according to whether the value of * expression is negative, zero or positive. * * @param o the object to be compared. * @return a negative integer, zero, or a positive integer as this object * is less than, equal to, or greater than the specified object. * @throws NullPointerException if the specified object is null * @throws ClassCastException if the specified object's type prevents it * from being compared to this object. */ @Override public int compareTo(Delayed o) { int result = this.getExecuteTime().compareTo(((DelayObject) o).getExecuteTime()); return result; } }

你可能感兴趣的:(延迟队列2)