Search in sources :

Example 1 with ExchangeChannel

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

the class HeaderExchangeHandler method caught.

public void caught(Channel channel, Throwable exception) throws RemotingException {
    if (exception instanceof ExecutionException) {
        ExecutionException e = (ExecutionException) exception;
        Object msg = e.getRequest();
        if (msg instanceof Request) {
            Request req = (Request) msg;
            if (req.isTwoWay() && !req.isHeartbeat()) {
                Response res = new Response(req.getId(), req.getVersion());
                res.setStatus(Response.SERVER_ERROR);
                res.setErrorMessage(StringUtils.toString(e));
                channel.send(res);
                return;
            }
        }
    }
    ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    try {
        handler.caught(exchangeChannel, exception);
    } 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)

Example 2 with ExchangeChannel

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

the class HeaderExchangeHandler method sent.

public void sent(Channel channel, Object message) throws RemotingException {
    Throwable exception = null;
    try {
        channel.setAttribute(KEY_WRITE_TIMESTAMP, System.currentTimeMillis());
        ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
        try {
            handler.sent(exchangeChannel, message);
        } finally {
            HeaderExchangeChannel.removeChannelIfDisconnected(channel);
        }
    } catch (Throwable t) {
        exception = t;
    }
    if (message instanceof Request) {
        Request request = (Request) message;
        DefaultFuture.sent(channel, request);
    }
    if (exception != null) {
        if (exception instanceof RuntimeException) {
            throw (RuntimeException) exception;
        } else if (exception instanceof RemotingException) {
            throw (RemotingException) exception;
        } else {
            throw new RemotingException(channel.getLocalAddress(), channel.getRemoteAddress(), exception.getMessage(), exception);
        }
    }
}
Also used : RemotingException(com.alibaba.dubbo.remoting.RemotingException) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel)

Example 3 with ExchangeChannel

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

the class HeaderExchangeServer method getExchangeChannels.

public Collection<ExchangeChannel> getExchangeChannels() {
    Collection<ExchangeChannel> exchangeChannels = new ArrayList<ExchangeChannel>();
    Collection<Channel> channels = server.getChannels();
    if (channels != null && channels.size() > 0) {
        for (Channel channel : channels) {
            exchangeChannels.add(HeaderExchangeChannel.getOrAddChannel(channel));
        }
    }
    return exchangeChannels;
}
Also used : ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Channel(com.alibaba.dubbo.remoting.Channel) ArrayList(java.util.ArrayList) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel)

Example 4 with ExchangeChannel

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

the class HeaderExchangeHandlerTest method test_received_request_event_other_discard.

@Test
public void test_received_request_event_other_discard() throws RemotingException {
    final Request request = new Request();
    request.setTwoWay(true);
    request.setEvent("my event");
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Assert.fail();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler() {

        @Override
        public Object reply(ExchangeChannel channel, Object request) throws RemotingException {
            Assert.fail();
            throw new RemotingException(channel, "");
        }

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            Assert.fail();
            throw new RemotingException(channel, "");
        }
    });
    hexhandler.received(mchannel, request);
}
Also used : ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Channel(com.alibaba.dubbo.remoting.Channel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) RemotingException(com.alibaba.dubbo.remoting.RemotingException) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Test(org.junit.Test)

Example 5 with ExchangeChannel

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

the class HeaderExchangeHandlerTest method test_received_request_twoway.

@Test
public void test_received_request_twoway() throws RemotingException {
    final Person requestdata = new Person("charles");
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(requestdata);
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response) message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.OK, res.getStatus());
            Assert.assertEquals(requestdata, res.getResult());
            Assert.assertEquals(null, res.getErrorMessage());
            count.incrementAndGet();
        }
    };
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        @Override
        public Object reply(ExchangeChannel channel, Object request) throws RemotingException {
            return request;
        }

        public void received(Channel channel, Object message) throws RemotingException {
            Assert.fail();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Channel(com.alibaba.dubbo.remoting.Channel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) ExchangeHandler(com.alibaba.dubbo.remoting.exchange.ExchangeHandler) Test(org.junit.Test)

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