limax远程过程调用RPC

RPC是双向通信的Protocol

1.RPC,Protocol公共子类Skeleton

public abstract class Skeleton {
    private volatile Object note;

    Skeleton() {
    }

    abstract void setTransport(Transport transport);

    abstract void _unmarshal(OctetsStream os) throws MarshalException;

    abstract void dispatch();

    public abstract int getType();

    public abstract void process() throws Exception;

    public final Object getNote() {
        return note;
    }

    public final void setNote(Object note) {
        this.note = note;
    }
}

2.发送RPC协议

public final void send(Transport transport)
        throws InstantiationException, SizePolicyException, CodecException, ClassCastException {
    try {
        //RPC中区分请求与相应
        isRequest = true;
        Manager manager = transport.getManager();
        sid = ((SupportRpcContext) manager).addContext(protocol);
        checkSend(transport, new OctetsStream().marshal(protocol));
        //定时器超时处理
        if (future == null)
            protocol.future = Engine.getProtocolScheduler().schedule(new Timeout(manager, sid), getTimeout(),
                    TimeUnit.MILLISECONDS);
    } catch (RuntimeException e) {
        throw new CodecException(e);
    }
}

3.对方收到协议处理逻辑后调用response响应

public final void response()
        throws InstantiationException, SizePolicyException, CodecException, ClassCastException {
    try {
        clearRequest().checkSend(protocol.getTransport(), new OctetsStream().marshal(protocol));
    } catch (RuntimeException e) {
        throw new CodecException(e);
    }
}

4.发送方收到response信息处理

@Override
final void dispatch() {
    //请求rpc直接当协议处理
    if (isRequest) {
        protocol.dispatch();
        return;
    }
    // response
    InnerProtocol inner = ((SupportRpcContext) protocol.getManager()).removeContext(sid, protocol);
    if (null == inner) {
        if (Trace.isInfoEnabled())
            Trace.info("limax.net.Rpc.response context lost! " + this);
        return;
    }
    if (inner.future != null)
        inner.future.cancel(false);
    Rpc<A, R> rpc = inner.getRpc().clearRequest().setResult(result);
    rpc.setTransport(getTransport());
    if (rpc.future != null) {
        if (Trace.isDebugEnabled())
            Trace.debug("limax.net.Rpc.response with submit " + this);
        rpc.future.set(result);
        return;
    }
    if (Trace.isDebugEnabled())
        Trace.debug("limax.net.Rpc.response execute " + this);
    inner.dispatch();
}
//主动调用onClient处理响应信息
public void process() throws Exception {
    if (isRequest) {
        onServer();
        response();
    } else {
        _onClient();
    }
}

5.总结

(1)RPC在Global模块中使用,用于全局名字分配,此时可以在一个过程中确认是否调用成功

(2)RPC可以充分利用资源,多机交互。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值