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

net.dongliu.prettypb.rpc.server.ServerCallTask Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
/**
 *   Copyright 2010-2014 Peter Klauser
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.dongliu.prettypb.rpc.server;

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

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author Peter Klauser
 */
public class ServerCallTask implements Runnable {

    private final ServiceInfo serviceInfo;
    private final MethodInfo methodInfo;
    private final Object request;
    private final long start;
    /**
     * rpc time out in milliseconds
     */
    private final int timeout;
    private final int correlationId;

    private final RpcServerChannel rpcServerChannel;
    private final Map serverCallMapMap;

    /**
     * this task has been set as canceled
     */
    private volatile boolean cancel;

    public ServerCallTask(ServiceInfo serviceInfo, MethodInfo methodInfo, Object request,
                          long start, int timeout, int correlationId,
                          RpcServerChannel rpcServerChannel,
                          Map serverCallMapMap) {
        this.serviceInfo = serviceInfo;
        this.methodInfo = methodInfo;
        this.request = request;
        this.start = start;
        this.timeout = timeout;
        this.correlationId = correlationId;
        this.rpcServerChannel = rpcServerChannel;
        this.serverCallMapMap = serverCallMapMap;
    }

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

    public Object getRequest() {
        return request;
    }

    public long getStart() {
        return start;
    }

    public MethodInfo getMethodInfo() {
        return methodInfo;
    }

    public ServiceInfo getServiceInfo() {
        return serviceInfo;
    }

    public int getTimeout() {
        return timeout;
    }

    public int getCorrelationId() {
        return correlationId;
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    @Override
    public void run() {
        if (cancel) {
            //should already be removed
            serverCallMapMap.remove(correlationId);
            return;
        }
        if (taskTimeout()) {
            serverCallMapMap.remove(correlationId);
            rpcServerChannel.sendRpcError(correlationId, "Rpc call time out");
        }

        Runnable runnable;
        if (serviceInfo.isAsync()) {
            runnable = new Runnable() {
                @Override
                public void run() {
                    RpcCallback rpcCallback = new RpcCallback() {
                        @Override
                        public void onFinished(Object value) {
                            serverCallMapMap.remove(correlationId);
                            rpcServerChannel.sendRpcResponse(correlationId, value,
                                    methodInfo.getResponseType());
                        }

                        @Override
                        public void onError(Exception e) {
                            serverCallMapMap.remove(correlationId);
                            rpcServerChannel.sendRpcError(correlationId, e.getMessage());
                        }
                    };
                    try {
                        methodInfo.getImplMethod().invoke(serviceInfo.getImpl(), request,
                                rpcCallback);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        serverCallMapMap.remove(correlationId);
                        rpcServerChannel.sendRpcError(correlationId, e.getMessage());
                    }
                }
            };
        } else {
            runnable = new Runnable() {
                @Override
                public void run() {
                    Object result;
                    try {
                        result = methodInfo.getImplMethod().invoke(serviceInfo.getImpl(),
                                request);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        serverCallMapMap.remove(correlationId);
                        rpcServerChannel.sendRpcError(correlationId, e.getMessage());
                        return;
                    }
                    serverCallMapMap.remove(correlationId);
                    rpcServerChannel.sendRpcResponse(correlationId, result,
                            methodInfo.getResponseType());

                }
            };
        }
        runnable.run();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy