Search in sources :

Example 46 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeHandler method received.

@Override
public void received(Channel channel, Object message) throws RemotingException {
    final ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    if (message instanceof Request) {
        // handle request.
        Request request = (Request) message;
        if (request.isEvent()) {
            handlerEvent(channel, request);
        } else {
            if (request.isTwoWay()) {
                handleRequest(exchangeChannel, request);
            } 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);
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutionException(org.apache.dubbo.remoting.ExecutionException)

Example 47 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeHandler method caught.

@Override
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(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ExecutionException(org.apache.dubbo.remoting.ExecutionException)

Example 48 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeartbeatTimerTask method doTask.

@Override
protected void doTask(Channel channel) {
    try {
        Long lastRead = lastRead(channel);
        Long lastWrite = lastWrite(channel);
        if ((lastRead != null && now() - lastRead > heartbeat) || (lastWrite != null && now() - lastWrite > heartbeat)) {
            Request req = new Request();
            req.setVersion(Version.getProtocolVersion());
            req.setTwoWay(true);
            req.setEvent(HEARTBEAT_EVENT);
            channel.send(req);
            if (logger.isDebugEnabled()) {
                logger.debug("Send heartbeat to remote channel " + channel.getRemoteAddress() + ", cause: The channel has no data-transmission exceeds a heartbeat period: " + heartbeat + "ms");
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception when heartbeat to remote channel " + channel.getRemoteAddress(), t);
    }
}
Also used : Request(org.apache.dubbo.remoting.exchange.Request)

Example 49 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeServer method sendChannelReadOnlyEvent.

private void sendChannelReadOnlyEvent() {
    Request request = new Request();
    request.setEvent(READONLY_EVENT);
    request.setTwoWay(false);
    request.setVersion(Version.getProtocolVersion());
    Collection<Channel> channels = getChannels();
    for (Channel channel : channels) {
        try {
            if (channel.isConnected()) {
                channel.send(request, getUrl().getParameter(Constants.CHANNEL_READONLYEVENT_SENT_KEY, true));
            }
        } catch (RemotingException e) {
            logger.warn("send cannot write message error.", e);
        }
    }
}
Also used : ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request)

Example 50 with Request

use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeartbeatHandler method received.

@Override
public void received(Channel channel, Object message) throws RemotingException {
    setReadTimestamp(channel);
    if (isHeartbeatRequest(message)) {
        Request req = (Request) message;
        if (req.isTwoWay()) {
            Response res = new Response(req.getId(), req.getVersion());
            res.setEvent(HEARTBEAT_EVENT);
            channel.send(res);
            if (logger.isInfoEnabled()) {
                int heartbeat = channel.getUrl().getParameter(Constants.HEARTBEAT_KEY, 0);
                if (logger.isDebugEnabled()) {
                    logger.debug("Received heartbeat from remote channel " + channel.getRemoteAddress() + ", cause: The channel has no data-transmission exceeds a heartbeat period" + (heartbeat > 0 ? ": " + heartbeat + "ms" : ""));
                }
            }
        }
        return;
    }
    if (isHeartbeatResponse(message)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Receive heartbeat response in thread " + Thread.currentThread().getName());
        }
        return;
    }
    handler.received(channel, message);
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request)

Aggregations

Request (org.apache.dubbo.remoting.exchange.Request)51 Test (org.junit.jupiter.api.Test)30 Response (org.apache.dubbo.remoting.exchange.Response)21 Channel (org.apache.dubbo.remoting.Channel)18 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)10 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 RemotingException (org.apache.dubbo.remoting.RemotingException)7 TMessage (org.apache.thrift.protocol.TMessage)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)5 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 IOException (java.io.IOException)3