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

net.dongliu.prettypb.rpc.client.ClientCallTask Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.rpc.client;

import net.dongliu.prettypb.rpc.info.MethodInfo;
import net.dongliu.prettypb.runtime.include.RpcCallback;

/**
 * @author dongliu
 */
public class ClientCallTask {

    private final int correlationId;

    private final RpcCallback callback;
    private final MethodInfo methodInfo;
    /**
     * timeout milliseconds for this requests
     */
    private int timeout;
    private final long start;

    private Exception exception;

    private volatile boolean canceled;

    public ClientCallTask(int correlationId, MethodInfo methodInfo, RpcCallback callback,
                          int timeout) {
        this.correlationId = correlationId;
        this.methodInfo = methodInfo;
        this.callback = callback;
        this.start = System.currentTimeMillis();
        this.timeout = timeout;
    }

    public boolean isTimeout() {
        return timeout > 0 && System.currentTimeMillis() > start + timeout;
    }

    public void handleResponse(Object response) {
        callback.onFinished(response);
    }

    public void handleFailure(Exception e) {
        this.exception = e;
        callback.onError(e);
    }

    /**
     * start time stamp
     *
     * @return
     */
    public long getStart() {
        return start;
    }

    public MethodInfo getMethodInfo() {
        return methodInfo;
    }

    public int getCorrelationId() {
        return correlationId;
    }

    public RpcCallback getCallback() {
        return callback;
    }

    public int getTimeout() {
        return timeout;
    }

    /**
     * exception mes
     *
     * @return null if succeed, msg if failed
     */
    public Exception getException() {
        return exception;
    }

    public boolean isCanceled() {
        return canceled;
    }

    public void setCanceled(boolean canceled) {
        this.canceled = canceled;
    }

    public boolean isAsync() {
        return methodInfo.getServiceInfo().isAsync();
    }
}