Example usage for io.netty.channel EventLoopGroup next

List of usage examples for io.netty.channel EventLoopGroup next

Introduction

In this page you can find the example usage for io.netty.channel EventLoopGroup next.

Prototype

@Override
EventLoop next();

Source Link

Document

Return the next EventLoop to use

Usage

From source file:c5db.replication.ReplicatorService.java

License:Apache License

/**
 * ReplicatorService creates and starts fibers; it must be stopped (or failed) in
 * order to dispose them.//from   ww  w .  j ava2 s . c  om
 */
public ReplicatorService(EventLoopGroup bossGroup, EventLoopGroup workerGroup, long nodeId, int port,
        ModuleInformationProvider moduleInformationProvider, FiberSupplier fiberSupplier,
        QuorumFileReaderWriter quorumFileReaderWriter) {
    this.bossGroup = bossGroup;
    this.workerGroup = workerGroup;
    this.nodeId = nodeId;
    this.port = port;
    this.moduleInformationProvider = moduleInformationProvider;
    this.fiberSupplier = fiberSupplier;

    this.allChannels = new DefaultChannelGroup(workerGroup.next());
    this.persister = new Persister(quorumFileReaderWriter);
}

From source file:cloudeventbus.server.ClusterManager.java

License:Open Source License

public ClusterManager(final ServerConfig serverConfig, GlobalHub globalHub, EventLoopGroup eventLoopGroup) {
    this.serverConfig = serverConfig;
    this.globalHub = globalHub;
    this.eventLoopGroup = eventLoopGroup;

    globalHub.addRemoteHub(this);

    // Set a peer info for the local server so that it doesn't ever try to connect to itself
    final PeerInfo localPeerInfo = new PeerInfo(new InetSocketAddress(1), true);
    localPeerInfo.setPeer(new Peer() {
        @Override//from ww w  .java2s . c om
        public long getId() {
            return serverConfig.getId();
        }

        @Override
        public SocketAddress getAddress() {
            return localPeerInfo.address;
        }

        @Override
        public void publish(Subject subject, Subject replySubject, String body) {
            // Do nothing
        }

        @Override
        public void close() {
            // Do nothing
        }

        @Override
        public boolean isConnected() {
            return true;
        }
    });
    knownPeers.put(serverConfig.getId(), localPeerInfo);

    if (eventLoopGroup != null) {
        eventLoopGroup.next().scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                cloudCheck();
            }
        }, CLUSTER_CHECK_INTERVAL, CLUSTER_CHECK_INTERVAL, TimeUnit.MILLISECONDS);
    }
}

From source file:com.cloudera.livy.client.local.rpc.Rpc.java

License:Apache License

/**
 * Creates an RPC client for a server running on the given remote host and port.
 *
 * @param config RPC configuration data.
 * @param eloop Event loop for managing the connection.
 * @param host Host name or IP address to connect to.
 * @param port Port where server is listening.
 * @param clientId The client ID that identifies the connection.
 * @param secret Secret for authenticating the client with the server.
 * @param dispatcher Dispatcher used to handle RPC calls.
 * @return A future that can be used to monitor the creation of the RPC object.
 *//*from   w  w  w .ja va2  s  . c om*/
public static Promise<Rpc> createClient(final LocalConf config, final EventLoopGroup eloop, String host,
        int port, final String clientId, final String secret, final RpcDispatcher dispatcher) throws Exception {
    int connectTimeoutMs = (int) config.getTimeAsMs(RPC_CLIENT_CONNECT_TIMEOUT);

    final ChannelFuture cf = new Bootstrap().group(eloop).handler(new ChannelInboundHandlerAdapter() {
    }).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMs).connect(host, port);

    final Promise<Rpc> promise = eloop.next().newPromise();
    final AtomicReference<Rpc> rpc = new AtomicReference<Rpc>();

    // Set up a timeout to undo everything.
    final Runnable timeoutTask = new Runnable() {
        @Override
        public void run() {
            promise.setFailure(new TimeoutException("Timed out waiting for RPC server connection."));
        }
    };
    final ScheduledFuture<?> timeoutFuture = eloop.schedule(timeoutTask,
            config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS);

    // The channel listener instantiates the Rpc instance when the connection is established,
    // and initiates the SASL handshake.
    cf.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            if (cf.isSuccess()) {
                SaslClientHandler saslHandler = new SaslClientHandler(config, clientId, promise, timeoutFuture,
                        secret, dispatcher);
                Rpc rpc = createRpc(config, saslHandler, (SocketChannel) cf.channel(), eloop);
                saslHandler.rpc = rpc;
                saslHandler.sendHello(cf.channel());
            } else {
                promise.setFailure(cf.cause());
            }
        }
    });

    // Handle cancellation of the promise.
    promise.addListener(new GenericFutureListener<Promise<Rpc>>() {
        @Override
        public void operationComplete(Promise<Rpc> p) {
            if (p.isCancelled()) {
                cf.cancel(true);
            }
        }
    });

    return promise;
}

From source file:com.cloudera.livy.rsc.rpc.Rpc.java

License:Apache License

/**
 * Creates an RPC client for a server running on the given remote host and port.
 *
 * @param config RPC configuration data.
 * @param eloop Event loop for managing the connection.
 * @param host Host name or IP address to connect to.
 * @param port Port where server is listening.
 * @param clientId The client ID that identifies the connection.
 * @param secret Secret for authenticating the client with the server.
 * @param dispatcher Dispatcher used to handle RPC calls.
 * @return A future that can be used to monitor the creation of the RPC object.
 *//*from   w w w.  j a  v a2  s .com*/
public static Promise<Rpc> createClient(final RSCConf config, final EventLoopGroup eloop, String host, int port,
        final String clientId, final String secret, final RpcDispatcher dispatcher) throws Exception {
    int connectTimeoutMs = (int) config.getTimeAsMs(RPC_CLIENT_CONNECT_TIMEOUT);

    final ChannelFuture cf = new Bootstrap().group(eloop).handler(new ChannelInboundHandlerAdapter() {
    }).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMs).connect(host, port);

    final Promise<Rpc> promise = eloop.next().newPromise();
    final AtomicReference<Rpc> rpc = new AtomicReference<Rpc>();

    // Set up a timeout to undo everything.
    final Runnable timeoutTask = new Runnable() {
        @Override
        public void run() {
            promise.setFailure(new TimeoutException("Timed out waiting for RPC server connection."));
        }
    };
    final ScheduledFuture<?> timeoutFuture = eloop.schedule(timeoutTask,
            config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS);

    // The channel listener instantiates the Rpc instance when the connection is established,
    // and initiates the SASL handshake.
    cf.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            if (cf.isSuccess()) {
                SaslClientHandler saslHandler = new SaslClientHandler(config, clientId, promise, timeoutFuture,
                        secret, dispatcher);
                Rpc rpc = createRpc(config, saslHandler, (SocketChannel) cf.channel(), eloop);
                saslHandler.rpc = rpc;
                saslHandler.sendHello(cf.channel());
            } else {
                promise.setFailure(cf.cause());
            }
        }
    });

    // Handle cancellation of the promise.
    promise.addListener(new GenericFutureListener<Promise<Rpc>>() {
        @Override
        public void operationComplete(Promise<Rpc> p) {
            if (p.isCancelled()) {
                cf.cancel(true);
            }
        }
    });

    return promise;
}

From source file:com.github.pgasync.impl.netty.NettyPgProtocolStream.java

License:Apache License

public NettyPgProtocolStream(EventLoopGroup group, SocketAddress address, boolean useSsl, boolean pipeline) {
    this.group = group;
    this.eventLoop = group.next();
    this.address = address;
    this.useSsl = useSsl; // TODO: refactor into SSLConfig with trust parameters
    this.pipeline = pipeline;
    this.subscribers = new LinkedBlockingDeque<>(); // TODO: limit pipeline queue depth
    this.onError = future -> {
        if (!future.isSuccess()) {
            subscribers.peek().onError(future.cause());
        }/*from w w w  .j a  va 2 s  .  c o m*/
    };
}

From source file:io.advantageous.conekt.impl.ContextImpl.java

License:Open Source License

protected ContextImpl(ConektInternal vertx, Executor orderedInternalPoolExec, Executor workerExec,
        String deploymentID, ClassLoader tccl) {
    if (DISABLE_TCCL && !tccl.getClass().getName().equals("sun.misc.Launcher$AppClassLoader")) {
        log.warn("You have disabled TCCL checks but you have a custom TCCL to set.");
    }/*from w  ww.  j a va  2  s .  c om*/
    this.orderedInternalPoolExec = orderedInternalPoolExec;
    this.workerExec = workerExec;
    this.deploymentID = deploymentID;
    EventLoopGroup group = vertx.getEventLoopGroup();
    if (group != null) {
        this.eventLoop = group.next();
    } else {
        this.eventLoop = null;
    }
    this.tccl = tccl;
    this.owner = vertx;
}

From source file:io.codis.nedis.NedisClientPoolImpl.java

License:Apache License

public NedisClientPoolImpl(EventLoopGroup group, Class<? extends Channel> channelClass, long timeoutMs,
        SocketAddress remoteAddress, byte[] password, int database, byte[] clientName, int maxPooledConns,
        boolean exclusive) {
    this.group = group;
    this.channelClass = channelClass;
    this.timeoutMs = timeoutMs;
    this.remoteAddress = remoteAddress;
    this.password = password;
    this.database = database;
    this.clientName = clientName;
    this.maxPooledConns = maxPooledConns;
    this.exclusive = exclusive;
    this.pool = new NedisClientHashSet(maxPooledConns);
    this.closePromise = group.next().newPromise();
}

From source file:io.jsync.impl.DefaultContext.java

License:Open Source License

protected DefaultContext(AsyncInternal async, Executor orderedBgExec) {
    this.async = async;
    this.orderedBgExec = orderedBgExec;
    EventLoopGroup group = async.getEventLoopGroup();
    if (group != null) {
        this.eventLoop = group.next();
        this.tccl = Thread.currentThread().getContextClassLoader();
    } else {/*from   w  w  w  .j a va2  s  .c  o  m*/
        this.eventLoop = null;
        this.tccl = null;
    }
}

From source file:io.netty.example.ocsp.OcspClientExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (!OpenSsl.isAvailable()) {
        throw new IllegalStateException("OpenSSL is not available!");
    }// ww w.  j av  a  2 s . c  o m

    if (!OpenSsl.isOcspSupported()) {
        throw new IllegalStateException("OCSP is not supported!");
    }

    // Using Wikipedia as an example. I'd rather use Netty's own website
    // but the server (Cloudflare) doesn't support OCSP stapling. A few
    // other examples could be Microsoft or Squarespace. Use OpenSSL's
    // CLI client to assess if a server supports OCSP stapling. E.g.:
    //
    // openssl s_client -tlsextdebug -status -connect www.squarespace.com:443
    //
    String host = "www.wikipedia.org";

    ReferenceCountedOpenSslContext context = (ReferenceCountedOpenSslContext) SslContextBuilder.forClient()
            .sslProvider(SslProvider.OPENSSL).enableOcsp(true).build();

    try {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Promise<FullHttpResponse> promise = group.next().newPromise();

            Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(group)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5 * 1000)
                    .handler(newClientHandler(context, host, promise));

            Channel channel = bootstrap.connect(host, 443).syncUninterruptibly().channel();

            try {
                FullHttpResponse response = promise.get();
                ReferenceCountUtil.release(response);
            } finally {
                channel.close();
            }
        } finally {
            group.shutdownGracefully();
        }
    } finally {
        context.release();
    }
}

From source file:io.vertx.core.impl.ContextImpl.java

License:Open Source License

protected ContextImpl(VertxInternal vertx, WorkerPool internalBlockingPool, WorkerPool workerPool,
        String deploymentID, JsonObject config, ClassLoader tccl) {
    if (DISABLE_TCCL && !tccl.getClass().getName().equals("sun.misc.Launcher$AppClassLoader")) {
        log.warn("You have disabled TCCL checks but you have a custom TCCL to set.");
    }//  w ww  .  j  a v a 2s .c o  m
    this.deploymentID = deploymentID;
    this.config = config;
    EventLoopGroup group = vertx.getEventLoopGroup();
    if (group != null) {
        this.eventLoop = group.next();
    } else {
        this.eventLoop = null;
    }
    this.tccl = tccl;
    this.owner = vertx;
    this.workerPool = workerPool;
    this.internalBlockingPool = internalBlockingPool;
    this.orderedTasks = new TaskQueue();
    this.internalOrderedTasks = new TaskQueue();
    this.closeHooks = new CloseHooks(log);
}