net.dongliu.prettypb.rpc.common.Task Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prettypb-rpc Show documentation
Show all versions of prettypb-rpc Show documentation
proto rpc libs, compatible with proto-rpc-pro
package net.dongliu.prettypb.rpc.common;
/**
* @author dongliu
*/
public abstract class Task implements Comparable {
private final int id;
private final long timeout;
private final long start;
private volatile boolean cancel;
protected Task(int id, long timeout, long start) {
this.id = id;
this.timeout = timeout;
this.start = start;
}
/**
* called when task is timeout
*/
public abstract void onTimeout();
/**
* called when channel/taskSet is closed
*/
public abstract void onClosed();
public int id() {
return this.id;
}
/**
* has been time out
*
* @return
*/
public boolean timeout() {
return timeout >= 0 && System.currentTimeMillis() - start > timeout;
}
/**
* timeout been set for this task
*
* @return
*/
public boolean timeoutSet() {
return timeout >= 0;
}
/**
* till when task is timeout
*
* @return
*/
public long life() {
return timeout + start;
}
/**
* cancel the task
*/
public void cancel() {
this.cancel = true;
}
public boolean canceled() {
return this.cancel;
}
@Override
public int compareTo(T task) {
return (int) (this.life() - task.life());
}
}