Search in sources :

Example 11 with ExchangeChannel

use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class HeaderExchangeHandler method received.

public void received(Channel channel, Object message) throws RemotingException {
    channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
    ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    try {
        if (message instanceof Request) {
            // handle request.
            Request request = (Request) message;
            if (request.isEvent()) {
                handlerEvent(channel, request);
            } else {
                if (request.isTwoWay()) {
                    Response response = handleRequest(exchangeChannel, request);
                    channel.send(response);
                } else {
                    handler.received(exchangeChannel, request.getData());
                }
            }
        } else if (message instanceof Response) {
            handleResponse(channel, (Response) message);
        } else if (message instanceof String) {
            if (isClientSide(channel)) {
                Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl());
                logger.error(e.getMessage(), e);
            } else {
                String echo = handler.telnet(channel, (String) message);
                if (echo != null && echo.length() > 0) {
                    channel.send(echo);
                }
            }
        } else {
            handler.received(exchangeChannel, message);
        }
    } finally {
        HeaderExchangeChannel.removeChannelIfDisconnected(channel);
    }
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) ExecutionException(com.alibaba.dubbo.remoting.ExecutionException) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 12 with ExchangeChannel

use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class Main method dataPackageTest.

static void dataPackageTest(int port) throws Exception {
    ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
    Random random = new Random();
    for (int i = 5; i < 100; i++) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i * 100; j++) sb.append("(" + random.nextLong() + ")");
        Main.Data d = new Main.Data();
        d.setData(sb.toString());
        client.request(d).get();
    }
    System.out.println("send finished.");
}
Also used : Random(java.util.Random) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel)

Example 13 with ExchangeChannel

use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class Main method test.

private static void test(int port) throws Exception {
    ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
    MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class<?>[] { int.class, int.class }, new Object[] { 55, 25 })).get();
    System.out.println("55+25=" + result.getResult());
    for (int i = 0; i < 100; i++) client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class<?>[] { String.class }, new Object[] { "qianlei" + i }));
    for (int i = 0; i < 100; i++) client.request(new Main.Data());
    System.out.println("=====test invoke=====");
    for (int i = 0; i < 100; i++) {
        ResponseFuture future = client.request(new Main.Data());
        System.out.println("invoke and get");
        System.out.println("invoke result:" + future.get());
    }
    System.out.println("=====the end=====");
}
Also used : ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture)

Example 14 with ExchangeChannel

use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class NettyClientTest method testClientClose.

@Test
public void testClientClose() throws Exception {
    List<ExchangeChannel> clients = new ArrayList<ExchangeChannel>(100);
    for (int i = 0; i < 100; i++) {
        ExchangeChannel client = Exchangers.connect(URL.valueOf("exchange://localhost:10001?client=netty"));
        Thread.sleep(5);
        clients.add(client);
    }
    for (ExchangeChannel client : clients) {
        client.close();
    }
    Thread.sleep(1000);
}
Also used : ArrayList(java.util.ArrayList) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Test(org.junit.Test)

Example 15 with ExchangeChannel

use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.

the class ExchangeServerPeer method getExchangeChannel.

@Override
public ExchangeChannel getExchangeChannel(InetSocketAddress remoteAddress) {
    String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName();
    int port = remoteAddress.getPort();
    ExchangeChannel channel = super.getExchangeChannel(remoteAddress);
    if (channel == null) {
        for (Map.Entry<URL, ExchangeClient> entry : clients.entrySet()) {
            URL url = entry.getKey();
            if (url.getIp().equals(host) && url.getPort() == port) {
                return entry.getValue();
            }
        }
    }
    return channel;
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL)

Aggregations

ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)19 Request (com.alibaba.dubbo.remoting.exchange.Request)6 Channel (com.alibaba.dubbo.remoting.Channel)4 RemotingException (com.alibaba.dubbo.remoting.RemotingException)4 Response (com.alibaba.dubbo.remoting.exchange.Response)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ExchangeServer (com.alibaba.dubbo.remoting.exchange.ExchangeServer)3 HeaderExchangeHandler (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)3 ExecutionException (com.alibaba.dubbo.remoting.ExecutionException)2 ExchangeHandler (com.alibaba.dubbo.remoting.exchange.ExchangeHandler)2 ExchangeHandlerAdapter (com.alibaba.dubbo.remoting.exchange.support.ExchangeHandlerAdapter)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 URL (com.alibaba.dubbo.common.URL)1 Page (com.alibaba.dubbo.container.page.Page)1 ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)1 ResponseFuture (com.alibaba.dubbo.remoting.exchange.ResponseFuture)1 ReplierDispatcher (com.alibaba.dubbo.remoting.exchange.support.ReplierDispatcher)1 Date (java.util.Date)1