Search in sources :

Example 1 with HeaderExchangeClient

use of com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient in project dubbo by alibaba.

the class ChannelWrappedInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    //拿不到client端export 的service path.约定为interface的名称.
    inv.setAttachment(Constants.PATH_KEY, getInterface().getName());
    inv.setAttachment(Constants.CALLBACK_SERVICE_KEY, serviceKey);
    ExchangeClient currentClient = new HeaderExchangeClient(new ChannelWrapper(this.channel));
    try {
        if (getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false)) {
            // 不可靠异步
            currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), Constants.SENT_KEY, false));
            return new RpcResult();
        }
        int timeout = getUrl().getMethodParameter(invocation.getMethodName(), Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
        if (timeout > 0) {
            return (Result) currentClient.request(inv, timeout).get();
        } else {
            return (Result) currentClient.request(inv).get();
        }
    } catch (RpcException e) {
        throw e;
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e);
    } catch (Throwable e) {
        // here is non-biz exception, wrap it.
        throw new RpcException(e.getMessage(), e);
    }
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 2 with HeaderExchangeClient

use of com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient in project dubbo by alibaba.

the class HeartbeatServer method main.

public static void main(String[] args) throws Exception {
    final HeartBeatExchangeHandler serverHandler = new HeartBeatExchangeHandler(handler);
    Thread serverThread = new Thread(new Runnable() {

        public void run() {
            try {
                exchangeServer = new HeaderExchangeServer(Transporters.bind(clientUrl.addParameter(Constants.HEARTBEAT_KEY, 1000), serverHandler));
                serverStarted = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    serverThread.setDaemon(true);
    serverThread.start();
    while (!serverStarted) {
        Thread.sleep(1000);
    }
    HeartBeatExchangeHandler clientHandler = new HeartBeatExchangeHandler(handler);
    ExchangeClient exchangeClient = new HeaderExchangeClient(Transporters.connect(clientUrl, clientHandler));
    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        System.out.print(".");
    }
    System.out.println();
    if (clientHandler.getHeartBeatCount() > 0) {
        System.out.printf("Client receives %d heartbeats", clientHandler.getHeartBeatCount());
    } else {
        throw new Exception("Server heartbeat does not work.");
    }
    exchangeClient.close();
    exchangeServer.close();
}
Also used : HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) HeaderExchangeServer(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer)

Example 3 with HeaderExchangeClient

use of com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient in project dubbo by alibaba.

the class HeartbeatClient method main.

public static void main(String[] args) throws Exception {
    final HeartBeatExchangeHandler serverHandler = new HeartBeatExchangeHandler(handler);
    Thread serverThread = new Thread(new Runnable() {

        public void run() {
            try {
                exchangeServer = new HeaderExchangeServer(Transporters.bind(serverUrl, serverHandler));
                serverStarted = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    serverThread.setDaemon(true);
    serverThread.start();
    while (!serverStarted) {
        Thread.sleep(1000);
    }
    URL url = serverUrl.addParameter(Constants.HEARTBEAT_KEY, 1000);
    HeartBeatExchangeHandler clientHandler = new HeartBeatExchangeHandler(handler);
    ExchangeClient exchangeClient = new HeaderExchangeClient(Transporters.connect(url, clientHandler));
    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        System.out.print(".");
    }
    System.out.println();
    if (serverHandler.getHeartBeatCount() > 0) {
        System.out.printf("Server receives %d heartbeats", serverHandler.getHeartBeatCount());
    } else {
        throw new Exception("Client heartbeat does not work.");
    }
    exchangeClient.close();
    exchangeServer.close();
}
Also used : HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) HeaderExchangeServer(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer) URL(com.alibaba.dubbo.common.URL)

Aggregations

ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)3 HeaderExchangeClient (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient)3 HeaderExchangeServer (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer)2 URL (com.alibaba.dubbo.common.URL)1 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1