Search in sources :

Example 11 with DefaultChannelGroup

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

the class DummyHttpServer method init.

// interface ------------------------------------------------------------------------------------------------------
public boolean init() {
    Executor bossExecutor = Executors.newCachedThreadPool();
    Executor workerExecutor = Executors.newCachedThreadPool();
    ChannelFactory factory;
    if (useOldIo)
        factory = new OioServerSocketChannelFactory(bossExecutor, workerExecutor);
    else
        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("encoder", new HttpResponseEncoder());
            pipeline.addLast("decoder", new HttpRequestDecoder());
            if (compressionLevel > 0)
                pipeline.addLast("compressor", new HttpContentCompressor(compressionLevel));
            // 5MB
            pipeline.addLast("aggregator", new HttpChunkAggregator(5242880));
            pipeline.addLast("handler", new RequestHandler());
            return pipeline;
        }
    });
    channelGroup = new DefaultChannelGroup("hotpotato-dummy-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) OioServerSocketChannelFactory(org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory) 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) OioServerSocketChannelFactory(org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 12 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 13 with DefaultChannelGroup

use of org.jboss.netty.channel.group.DefaultChannelGroup in project pinpoint by naver.

the class HealthCheckManagerTest method legacyPingPacketTest.

@Test
public void legacyPingPacketTest() throws Exception {
    ChannelGroup channelGroup = new DefaultChannelGroup();
    HealthCheckManager healthCheckManager = new HealthCheckManager(timer, 3000, channelGroup);
    healthCheckManager.start(1000);
    Channel mockChannel = createMockChannel(HealthCheckState.RECEIVED_LEGACY);
    channelGroup.add(mockChannel);
    try {
        verify(mockChannel, timeout(3000).atLeastOnce()).write(PingPacket.PING_PACKET);
    } finally {
        healthCheckManager.stop();
    }
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) Channel(org.jboss.netty.channel.Channel) DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) ChannelGroup(org.jboss.netty.channel.group.ChannelGroup) Test(org.junit.Test)

Example 14 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)

Example 15 with DefaultChannelGroup

use of org.jboss.netty.channel.group.DefaultChannelGroup in project pinpoint by pinpoint-apm.

the class HealthCheckManagerTest method withoutPacketTest.

@Test
public void withoutPacketTest() throws Exception {
    ChannelGroup channelGroup = new DefaultChannelGroup();
    HealthCheckManager healthCheckManager = new HealthCheckManager(timer, 3000, channelGroup);
    healthCheckManager.start(1000);
    Channel mockChannel = createMockChannel(HealthCheckState.WAIT);
    channelGroup.add(mockChannel);
    try {
        verify(mockChannel, timeout(5000).atLeastOnce()).close();
    } finally {
        healthCheckManager.stop();
    }
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) Channel(org.jboss.netty.channel.Channel) DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) ChannelGroup(org.jboss.netty.channel.group.ChannelGroup) Test(org.junit.Test)

Aggregations

DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)18 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)9 Channel (org.jboss.netty.channel.Channel)9 ChannelGroup (org.jboss.netty.channel.group.ChannelGroup)8 InetSocketAddress (java.net.InetSocketAddress)7 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)7 Test (org.junit.Test)6 IOException (java.io.IOException)5 SocketAddress (java.net.SocketAddress)2 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)2 Executor (java.util.concurrent.Executor)2 SSLEngine (javax.net.ssl.SSLEngine)2 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)2 SslHandler (org.jboss.netty.handler.ssl.SslHandler)2 Point (java.awt.Point)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 MalformedURLException (java.net.MalformedURLException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1