Search in sources :

Example 16 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class HeartBeatTask method run.

public void run() {
    try {
        long now = System.currentTimeMillis();
        for (Channel channel : channelProvider.getChannels()) {
            if (channel.isClosed()) {
                continue;
            }
            try {
                Long lastRead = (Long) channel.getAttribute(HeaderExchangeHandler.KEY_READ_TIMESTAMP);
                Long lastWrite = (Long) channel.getAttribute(HeaderExchangeHandler.KEY_WRITE_TIMESTAMP);
                if ((lastRead != null && now - lastRead > heartbeat) || (lastWrite != null && now - lastWrite > heartbeat)) {
                    Request req = new Request();
                    req.setVersion("2.0.0");
                    req.setTwoWay(true);
                    req.setEvent(Request.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");
                    }
                }
                if (lastRead != null && now - lastRead > heartbeatTimeout) {
                    logger.warn("Close channel " + channel + ", because heartbeat read idle time out: " + heartbeatTimeout + "ms");
                    if (channel instanceof Client) {
                        try {
                            ((Client) channel).reconnect();
                        } catch (Exception e) {
                        // do nothing
                        }
                    } else {
                        channel.close();
                    }
                }
            } catch (Throwable t) {
                logger.warn("Exception when heartbeat to remote channel " + channel.getRemoteAddress(), t);
            }
        }
    } catch (Throwable t) {
        logger.warn("Unhandled exception when heartbeat, cause: " + t.getMessage(), t);
    }
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) Client(com.alibaba.dubbo.remoting.Client)

Example 17 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class AbstractClient method send.

public void send(Object message, boolean sent) throws RemotingException {
    if (send_reconnect && !isConnected()) {
        connect();
    }
    Channel channel = getChannel();
    // TODO Can the value returned by getChannel() be null? need improvement.
    if (channel == null || !channel.isConnected()) {
        throw new RemotingException(this, "message can not send, because channel is closed . url:" + getUrl());
    }
    channel.send(message, sent);
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 18 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class AbstractClient method removeAttribute.

public void removeAttribute(String key) {
    Channel channel = getChannel();
    if (channel == null)
        return;
    channel.removeAttribute(key);
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel)

Example 19 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class TraceFilter method invoke.

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    long start = System.currentTimeMillis();
    Result result = invoker.invoke(invocation);
    long end = System.currentTimeMillis();
    if (tracers.size() > 0) {
        String key = invoker.getInterface().getName() + "." + invocation.getMethodName();
        Set<Channel> channels = tracers.get(key);
        if (channels == null || channels.isEmpty()) {
            key = invoker.getInterface().getName();
            channels = tracers.get(key);
        }
        if (channels != null && !channels.isEmpty()) {
            for (Channel channel : new ArrayList<Channel>(channels)) {
                if (channel.isConnected()) {
                    try {
                        int max = 1;
                        Integer m = (Integer) channel.getAttribute(TRACE_MAX);
                        if (m != null) {
                            max = (int) m;
                        }
                        int count = 0;
                        AtomicInteger c = (AtomicInteger) channel.getAttribute(TRACE_COUNT);
                        if (c == null) {
                            c = new AtomicInteger();
                            channel.setAttribute(TRACE_COUNT, c);
                        }
                        count = c.getAndIncrement();
                        if (count < max) {
                            String prompt = channel.getUrl().getParameter(Constants.PROMPT_KEY, Constants.DEFAULT_PROMPT);
                            channel.send("\r\n" + RpcContext.getContext().getRemoteAddress() + " -> " + invoker.getInterface().getName() + "." + invocation.getMethodName() + "(" + JSON.toJSONString(invocation.getArguments()) + ")" + " -> " + JSON.toJSONString(result.getValue()) + "\r\nelapsed: " + (end - start) + " ms." + "\r\n\r\n" + prompt);
                        }
                        if (count >= max - 1) {
                            channels.remove(channel);
                        }
                    } catch (Throwable e) {
                        channels.remove(channel);
                        logger.warn(e.getMessage(), e);
                    }
                } else {
                    channels.remove(channel);
                }
            }
        }
    }
    return result;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(com.alibaba.dubbo.remoting.Channel) ArrayList(java.util.ArrayList) Result(com.alibaba.dubbo.rpc.Result)

Example 20 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class NettyServer method doClose.

@Override
protected void doClose() throws Throwable {
    try {
        if (channel != null) {
            // unbind.
            channel.close();
        }
    } catch (Throwable e) {
        logger.warn(e.getMessage(), e);
    }
    try {
        Collection<com.alibaba.dubbo.remoting.Channel> channels = getChannels();
        if (channels != null && channels.size() > 0) {
            for (com.alibaba.dubbo.remoting.Channel channel : channels) {
                try {
                    channel.close();
                } catch (Throwable e) {
                    logger.warn(e.getMessage(), e);
                }
            }
        }
    } catch (Throwable e) {
        logger.warn(e.getMessage(), e);
    }
    try {
        if (bootstrap != null) {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    } catch (Throwable e) {
        logger.warn(e.getMessage(), e);
    }
    try {
        if (channels != null) {
            channels.clear();
        }
    } catch (Throwable e) {
        logger.warn(e.getMessage(), e);
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(com.alibaba.dubbo.remoting.Channel) Channel(com.alibaba.dubbo.remoting.Channel)

Aggregations

Channel (com.alibaba.dubbo.remoting.Channel)35 Test (org.junit.Test)16 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)15 Request (com.alibaba.dubbo.remoting.exchange.Request)14 Response (com.alibaba.dubbo.remoting.exchange.Response)10 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)8 HeaderExchangeHandler (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 URL (com.alibaba.dubbo.common.URL)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 RemotingException (com.alibaba.dubbo.remoting.RemotingException)4 RpcResult (com.alibaba.dubbo.rpc.RpcResult)4 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TMessage (org.apache.thrift.protocol.TMessage)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 ExchangeHandler (com.alibaba.dubbo.remoting.exchange.ExchangeHandler)3 Client (com.alibaba.dubbo.remoting.Client)2 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2