Example usage for io.netty.channel Channel isActive

List of usage examples for io.netty.channel Channel isActive

Introduction

In this page you can find the example usage for io.netty.channel Channel isActive.

Prototype

boolean isActive();

Source Link

Document

Return true if the Channel is active and so connected.

Usage

From source file:NettyHttpTransportSourceHandler.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *///w ww  .j  a v a2s.co m
static void closeOnFlush(Channel ch) {
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:alluxio.network.connection.NettyChannelPool.java

License:Apache License

/**
 * Checks whether a channel is healthy.// w  w  w .  j a  v  a2  s  . com
 *
 * @param channel the channel to check
 * @return true if the channel is active (i.e. connected)
 */
@Override
protected boolean isHealthy(Channel channel) {
    if (POOL_DISABLED) {
        // If we always return false here, channels acquired by NettyChannelPool#acquire() will always
        // be newly created channels. With this feature turned on, >= 1.3.0 client will be backward
        // compatible with <= 1.2.0 server.
        return false;
    }
    return channel.isActive();
}

From source file:alluxio.network.netty.NettyChannelPool.java

License:Apache License

/**
 * Checks whether a channel is healthy./*  w  ww.  j  a v  a2  s.c o  m*/
 *
 * @param channel the channel to check
 * @return true if the channel is active (i.e. connected)
 */
@Override
protected boolean isHealthy(Channel channel) {
    if (POOL_DISABLED) {
        // If we always return false here, channels acquired by NettyChannelPool#acquire() will always
        // be newly created channels. With this feature turned on, >= 1.3.0 client will be backward
        // compatible with <= 1.2.0 server.
        return false;
    }
    return channel.isOpen() && channel.isActive();
}

From source file:bftsmart.communication.client.netty.NettyClientServerCommunicationSystemClientSide.java

License:Apache License

@Override
public void send(boolean sign, int[] targets, TOMMessage sm) {

    int quorum;//from  w  ww.ja  va  2  s  . co  m

    Integer[] targetArray = Arrays.stream(targets).boxed().toArray(Integer[]::new);
    Collections.shuffle(Arrays.asList(targetArray), new Random());

    if (controller.getStaticConf().isBFT()) {
        quorum = (int) Math.ceil((controller.getCurrentViewN() + controller.getCurrentViewF()) / 2) + 1;
    } else {
        quorum = (int) Math.ceil((controller.getCurrentViewN()) / 2) + 1;
    }

    listener.waitForChannels(quorum); // wait for the previous transmission to complete

    logger.debug("Sending request from " + sm.getSender() + " with sequence number " + sm.getSequence() + " to "
            + Arrays.toString(targetArray));

    if (sm.serializedMessage == null) {

        // serialize message
        DataOutputStream dos = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            dos = new DataOutputStream(baos);
            sm.wExternal(dos);
            dos.flush();
            sm.serializedMessage = baos.toByteArray();
        } catch (IOException ex) {
            logger.debug("Impossible to serialize message: " + sm);
        }
    }

    // Logger.println("Sending message with "+sm.serializedMessage.length+" bytes of
    // content.");

    // produce signature
    if (sign && sm.serializedMessageSignature == null) {
        sm.serializedMessageSignature = signMessage(privKey, sm.serializedMessage);
    }

    int sent = 0;

    for (int target : targetArray) {
        // This is done to avoid a race condition with the writeAndFush method. Since
        // the method is asynchronous,
        // each iteration of this loop could overwrite the destination of the previous
        // one
        try {
            sm = (TOMMessage) sm.clone();
        } catch (CloneNotSupportedException e) {
            logger.error("Failed to clone TOMMessage", e);
            continue;
        }

        sm.destination = targets[target];

        rl.readLock().lock();
        Channel channel = ((NettyClientServerSession) sessionClientToReplica.get(targets[target])).getChannel();
        rl.readLock().unlock();
        if (channel.isActive()) {
            sm.signed = sign;
            ChannelFuture f = channel.writeAndFlush(sm);

            f.addListener(listener);

            sent++;
        } else {
            logger.debug("Channel to " + targets[target] + " is not connected");
        }
    }

    if (targets.length > controller.getCurrentViewF() && sent < controller.getCurrentViewF() + 1) {
        // if less than f+1 servers are connected send an exception to the client
        throw new RuntimeException("Impossible to connect to servers!");
    }
    if (targets.length == 1 && sent == 0)
        throw new RuntimeException("Server not connected");
}

From source file:c5db.client.C5NettyConnectionManager.java

License:Apache License

@Override
public Channel getOrCreateChannel(String host, int port)
        throws InterruptedException, ExecutionException, TimeoutException {
    final String hash = getHostPortHash(host, port);
    if (!regionChannelMap.containsKey(hash)) {
        final Channel channel = connect(host, port);
        regionChannelMap.put(hash, channel);
        Log.warn("Channel" + channel);
        return channel;
    }//from  w  ww  . j ava  2  s .  c  o  m

    Channel channel = regionChannelMap.get(hash);

    // Clear stale channels
    if (!(channel.isOpen() && channel.isActive() && isHandShakeConnected(channel))) {
        closeChannel(host, port);
        channel.disconnect();
        channel = getOrCreateChannel(host, port);
    }

    return channel;
}

From source file:cn.david.socks.SocksServerUtils.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *//* w  w  w .  ja v  a 2 s  .c o  m*/
public static void closeOnFlush(Channel ch) {
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty.NettyChannel.java

License:Apache License

static NettyChannel getOrAddChannel(Channel ch, URL url, ChannelHandler handler) {
    if (ch == null) {
        return null;
    }//from  w w w  .j  a v a  2  s.  co  m
    NettyChannel ret = channelMap.get(ch);
    if (ret == null) {
        NettyChannel nettyChannel = new NettyChannel(ch, url, handler);
        if (ch.isActive()) {
            ret = channelMap.putIfAbsent(ch, nettyChannel);
        }
        if (ret == null) {
            ret = nettyChannel;
        }
    }
    return ret;
}

From source file:com.alibaba.dubbo.remoting.transport.netty.NettyChannel.java

License:Apache License

static void removeChannelIfDisconnected(Channel ch) {
    if (ch != null && !ch.isActive()) {
        channelMap.remove(ch);
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty.NettyClient.java

License:Apache License

@Override
protected com.alibaba.dubbo.remoting.Channel getChannel() {
    Channel c = channel;
    if (c == null || !c.isActive())
        return null;
    return NettyChannel.getOrAddChannel(c, getUrl(), this);
}

From source file:com.alibaba.rocketmq.remoting.netty.NettyRemotingClient.java

License:Apache License

@Override
public RemotingCommand invokeSync(String addr, final RemotingCommand request, long timeoutMillis)
        throws InterruptedException, RemotingConnectException, RemotingSendRequestException,
        RemotingTimeoutException {/*from   ww  w. j av  a 2s .  com*/
    final Channel channel = this.getAndCreateChannel(addr);
    if (channel != null && channel.isActive()) {
        try {
            if (this.rpcHook != null) {
                this.rpcHook.doBeforeRequest(addr, request);
            }
            RemotingCommand response = this.invokeSyncImpl(channel, request, timeoutMillis);
            if (this.rpcHook != null) {
                this.rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(channel), request, response);
            }
            return response;
        } catch (RemotingSendRequestException e) {
            log.warn("invokeSync: send request exception, so close the channel[{}]", addr);
            this.closeChannel(addr, channel);
            throw e;
        } catch (RemotingTimeoutException e) {
            log.warn("invokeSync: wait response timeout exception, the channel[{}]", addr);
            throw e;
        }
    } else {
        this.closeChannel(addr, channel);
        throw new RemotingConnectException(addr);
    }
}