Search in sources :

Example 6 with DefaultChannelGroup

use of org.jboss.netty.channel.group.DefaultChannelGroup in project http-client by biasedbit.

the class UploadMirrorHttpServer method init.

// interface ------------------------------------------------------------------------------------------------------
public boolean init() {
    Executor bossExecutor = Executors.newCachedThreadPool();
    Executor workerExecutor = Executors.newCachedThreadPool();
    ChannelFactory factory = new NioServerSocketChannelFactory(bossExecutor, workerExecutor);
    bootstrap = new ServerBootstrap(factory);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            if (useSsl) {
                SSLEngine engine = BogusSslContextFactory.getInstance().getServerContext().createSSLEngine();
                engine.setUseClientMode(false);
                pipeline.addLast("ssl", new SslHandler(engine));
            }
            pipeline.addLast("codec", new HttpServerCodec());
            pipeline.addLast("handler", new RequestHandler());
            return pipeline;
        }
    });
    channelGroup = new DefaultChannelGroup("hotpotato-upload-server-" + Integer.toHexString(hashCode()));
    SocketAddress bindAddress = (host != null) ? new InetSocketAddress(host, port) : new InetSocketAddress(port);
    Channel serverChannel = bootstrap.bind(bindAddress);
    channelGroup.add(serverChannel);
    return (running = serverChannel.isBound());
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) SslHandler(org.jboss.netty.handler.ssl.SslHandler) Executor(java.util.concurrent.Executor) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 7 with DefaultChannelGroup

use of org.jboss.netty.channel.group.DefaultChannelGroup in project databus by linkedin.

the class JmxShutdownThread method doStart.

protected void doStart() {
    _controlLock.lock();
    try {
        // Bind and start to accept incoming connections.
        int portNum = getContainerStaticConfig().getHttpPort();
        _tcpChannelGroup = new DefaultChannelGroup();
        _httpChannelGroup = new DefaultChannelGroup();
        _httpServerChannel = _httpBootstrap.bind(new InetSocketAddress(portNum));
        InetSocketAddress actualAddress = (InetSocketAddress) _httpServerChannel.getLocalAddress();
        _containerPort = actualAddress.getPort();
        // persist the port number (file name should be unique for the container)
        File portNumFile = new File(getHttpPortFileName());
        portNumFile.deleteOnExit();
        try {
            FileWriter portNumFileW = new FileWriter(portNumFile);
            portNumFileW.write(Integer.toString(_containerPort));
            portNumFileW.close();
            LOG.info("Saving port number in " + portNumFile.getAbsolutePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        _httpChannelGroup.add(_httpServerChannel);
        LOG.info("Serving container " + getContainerStaticConfig().getId() + " HTTP listener on port " + _containerPort);
        if (_containerStaticConfig.getTcp().isEnabled()) {
            int tcpPortNum = _containerStaticConfig.getTcp().getPort();
            _tcpServerChannel = _tcpBootstrap.bind(new InetSocketAddress(tcpPortNum));
            _tcpChannelGroup.add(_tcpServerChannel);
            LOG.info("Serving container " + getContainerStaticConfig().getId() + " TCP listener on port " + tcpPortNum);
        }
        _nettyShutdownThread = new NettyShutdownThread();
        Runtime.getRuntime().addShutdownHook(_nettyShutdownThread);
        // Start the producer thread after 5 seconds
        if (null != _jmxConnServer && _containerStaticConfig.getJmx().isRmiEnabled()) {
            try {
                _jmxShutdownThread = new JmxShutdownThread(_jmxConnServer);
                Runtime.getRuntime().addShutdownHook(_jmxShutdownThread);
                _jmxConnServer.start();
                LOG.info("JMX server listening on port " + _containerStaticConfig.getJmx().getJmxServicePort());
            } catch (IOException ioe) {
                if (ioe.getCause() != null && ioe.getCause() instanceof NameAlreadyBoundException) {
                    LOG.warn("Unable to bind JMX server connector. Likely cause is that the previous instance was not cleanly shutdown: killed in Eclipse?");
                    if (_jmxConnServer.isActive()) {
                        LOG.warn("JMX server connector seems to be running anyway. ");
                    } else {
                        LOG.warn("Unable to determine if JMX server connector is running");
                    }
                } else {
                    LOG.error("Unable to start JMX server connector", ioe);
                }
            }
        }
        _globalStatsThread.start();
    } catch (RuntimeException ex) {
        LOG.error("Got runtime exception :" + ex, ex);
        throw ex;
    } finally {
        _controlLock.unlock();
    }
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Aggregations

DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)7 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)5 InetSocketAddress (java.net.InetSocketAddress)4 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)4 IOException (java.io.IOException)2 SocketAddress (java.net.SocketAddress)2 Executor (java.util.concurrent.Executor)2 SSLEngine (javax.net.ssl.SSLEngine)2 Channel (org.jboss.netty.channel.Channel)2 SslHandler (org.jboss.netty.handler.ssl.SslHandler)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 ExecutorService (java.util.concurrent.ExecutorService)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)1 ChannelGroup (org.jboss.netty.channel.group.ChannelGroup)1 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)1 OioServerSocketChannelFactory (org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory)1