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

com.alachisoft.ncache.client.internal.communication.DistributedRPCCallBase Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.communication;

import Alachisoft.NCache.Common.Communication.IChannelFormatter;
import Alachisoft.NCache.Common.Extensibility.Client.RPC.IResponseConsolidator;
import Alachisoft.NCache.Common.Extensibility.Client.RPC.Partition;
import Alachisoft.NCache.Common.Extensibility.Client.RPC.RPCCallInfo;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Map;

abstract class DistributedRPCCallBase //: MethodCall
{
    public IResponseConsolidator consolidator;
    public String objectUID;
    private IChannelFormatter _formatter;
    private ArrayList> _partitonWiseArguments = new ArrayList<>();
    private RPCTransport _transport;
    private ModuleOperation _operation;
    private String module = "Lucene";
    private String version = "5.0";

    public DistributedRPCCallBase(RPCTransport transport, IChannelFormatter formatter) {
        if (transport == null) {
            throw new NullPointerException("transport");
        }
        if (formatter == null) {
            throw new NullPointerException("formatter");
        }

        this._transport = transport;
        this._formatter = formatter;
    }

    public IResponseConsolidator getConsolidator() {
        return consolidator;
    }

    public void setConsolidator(IResponseConsolidator consolidator) {
        this.consolidator = consolidator;
    }

    public final String GetModule() {
        return module;
    }

    public final void SetModule(String value) {
        module = value;
    }

    public final String GetVersion() {
        return version;
    }

    public final void SetVersion(String value) {
        version = value;
    }

    public String getObjectUID() {
        return objectUID;
    }

    public void setObjectUID(String objectUID) {
        this.objectUID = objectUID;
    }

    public final void AddArguments(Partition partiton, Object arguments) {
        _partitonWiseArguments.add(new AbstractMap.SimpleEntry<>(partiton, arguments));
    }

    public void PrepareCall() {
        _operation = new ModuleOperation();
        _operation.setModule(this.GetModule());
        _operation.setVersion(this.GetVersion());

        if (consolidator != null) {
            consolidator.Initialize();
        }


        for (Map.Entry arguments : _partitonWiseArguments) {
            RPCCallInfo rpcInfo = CreateRPCCallInfo();
            rpcInfo.setArguments(arguments.getValue());
            byte[] buffer = null;
            try {
                buffer = _formatter.Serialize(rpcInfo);
            } catch (Exception e) {
            }
            _operation.AddPayload(arguments.getKey(), buffer);
        }
    }

    public abstract RPCCallInfo CreateRPCCallInfo();

    public final  TResult Execute() throws Exception {
        if (_operation == null) {
            throw new Exception(String.format("Module operation is Null. Call %1$s before calling %2$s", "PrepareCall", "Execute"));
        }

        try {
            _transport.ExecuteModuleOperation(_operation);

            if (_operation.GetFailures() != null && _operation.GetFailures().size() > 0) {
                throw _operation.GetFailures().get(0).getValue();
            }

            for (Map.Entry resultPair : _operation.GetResults()) {
                Object result = _formatter.Deserialize(resultPair.getValue());

                if (result instanceof RuntimeException) {
                    throw result instanceof RuntimeException ? (RuntimeException) result : null;
                }

                if (consolidator != null) {
                    consolidator.CollectResponse(resultPair.getKey(), result);
                }
            }

            if (consolidator != null) {
                return (TResult) consolidator.GetFinalResponse();
            }

        } finally {
            if (consolidator != null) {
                consolidator.Reset();
            }
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy