Search in sources :

Example 16 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project graylog2-server by Graylog2.

the class NettyTransport method launch.

@Override
public void launch(final MessageInput input) throws MisfireException {
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList = getBaseChannelHandlers(input);
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> finalHandlers = getFinalChannelHandlers(input);
    handlerList.putAll(finalHandlers);
    try {
        bootstrap = getBootstrap();
        bootstrap.setPipelineFactory(getPipelineFactory(handlerList));
        // sigh, bindable bootstraps do not share a common interface
        int receiveBufferSize;
        if (bootstrap instanceof ConnectionlessBootstrap) {
            acceptChannel = ((ConnectionlessBootstrap) bootstrap).bind(socketAddress);
            final DefaultDatagramChannelConfig channelConfig = (DefaultDatagramChannelConfig) acceptChannel.getConfig();
            receiveBufferSize = channelConfig.getReceiveBufferSize();
        } else if (bootstrap instanceof ServerBootstrap) {
            acceptChannel = ((ServerBootstrap) bootstrap).bind(socketAddress);
            final ServerSocketChannelConfig channelConfig = (ServerSocketChannelConfig) acceptChannel.getConfig();
            receiveBufferSize = channelConfig.getReceiveBufferSize();
        } else {
            log.error("Unknown Netty bootstrap class returned: {}. Cannot safely bind.", bootstrap);
            throw new IllegalStateException("Unknown netty bootstrap class returned: " + bootstrap + ". Cannot safely bind.");
        }
        if (receiveBufferSize != getRecvBufferSize()) {
            log.warn("receiveBufferSize (SO_RCVBUF) for input {} should be {} but is {}.", input, getRecvBufferSize(), receiveBufferSize);
        }
    } catch (Exception e) {
        throw new MisfireException(e);
    }
}
Also used : MisfireException(org.graylog2.plugin.inputs.MisfireException) SimpleChannelHandler(org.jboss.netty.channel.SimpleChannelHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) DefaultDatagramChannelConfig(org.jboss.netty.channel.socket.DefaultDatagramChannelConfig) ServerSocketChannelConfig(org.jboss.netty.channel.socket.ServerSocketChannelConfig) Callable(java.util.concurrent.Callable) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) MisfireException(org.graylog2.plugin.inputs.MisfireException) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 17 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project graylog2-server by Graylog2.

the class AbstractTcpTransport method getBootstrap.

@Override
protected Bootstrap getBootstrap() {
    final ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(bossExecutor, workerExecutor));
    bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(8192));
    bootstrap.setOption("receiveBufferSize", getRecvBufferSize());
    bootstrap.setOption("child.receiveBufferSize", getRecvBufferSize());
    bootstrap.setOption("child.keepAlive", tcpKeepalive);
    return bootstrap;
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) FixedReceiveBufferSizePredictorFactory(org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Example 18 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project canal by alibaba.

the class CanalServerWithNetty method start.

public void start() {
    super.start();
    if (!embeddedServer.isStart()) {
        embeddedServer.start();
    }
    this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    // 构造对应的pipeline
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipelines = Channels.pipeline();
            pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
            pipelines.addLast(HandshakeInitializationHandler.class.getName(), new HandshakeInitializationHandler());
            pipelines.addLast(ClientAuthenticationHandler.class.getName(), new ClientAuthenticationHandler(embeddedServer));
            SessionHandler sessionHandler = new SessionHandler(embeddedServer);
            pipelines.addLast(SessionHandler.class.getName(), sessionHandler);
            return pipelines;
        }
    });
    // 启动
    if (StringUtils.isNotEmpty(ip)) {
        this.serverChannel = bootstrap.bind(new InetSocketAddress(this.ip, this.port));
    } else {
        this.serverChannel = bootstrap.bind(new InetSocketAddress(this.port));
    }
}
Also used : SessionHandler(com.alibaba.otter.canal.server.netty.handler.SessionHandler) ClientAuthenticationHandler(com.alibaba.otter.canal.server.netty.handler.ClientAuthenticationHandler) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) HandshakeInitializationHandler(com.alibaba.otter.canal.server.netty.handler.HandshakeInitializationHandler) InetSocketAddress(java.net.InetSocketAddress) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) FixedHeaderFrameDecoder(com.alibaba.otter.canal.server.netty.handler.FixedHeaderFrameDecoder) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 19 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project voldemort by voldemort.

the class AbstractRestService method startInner.

@Override
protected void startInner() {
    initialize();
    // Configure the service
    this.workerPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), workerPool));
    this.bootstrap.setOption("backlog", this.coordinatorConfig.getNettyServerBacklog());
    this.bootstrap.setOption("child.tcpNoDelay", true);
    this.bootstrap.setOption("child.keepAlive", true);
    this.bootstrap.setOption("child.reuseAddress", true);
    // Set up the event pipeline factory.
    this.bootstrap.setPipelineFactory(getPipelineFactory());
    // Assuming JMX is always enabled for service
    JmxUtils.registerMbean(this, JmxUtils.createObjectName(JmxUtils.getPackageName(this.getClass()), JmxUtils.getClassName(this.getClass())));
    // Register MBeans for connection stats
    JmxUtils.registerMbean(this.connectionStats, JmxUtils.createObjectName(JmxUtils.getPackageName(this.getClass()), JmxUtils.getClassName(this.connectionStats.getClass())));
    // Bind and start to accept incoming connections.
    this.channel = this.bootstrap.bind(new InetSocketAddress(getServicePort()));
    logger.info(getServiceName() + " service started on port " + getServicePort());
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Example 20 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project voldemort by voldemort.

the class RestService method startInner.

@Override
protected void startInner() {
    // Configure the server.
    this.workerPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(config.getNumRestServiceNettyWorkerThreads());
    this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newFixedThreadPool(config.getNumRestServiceNettyBossThreads()), workerPool));
    this.bootstrap.setOption("backlog", config.getRestServiceNettyServerBacklog());
    this.bootstrap.setOption("child.tcpNoDelay", true);
    this.bootstrap.setOption("child.keepAlive", true);
    this.bootstrap.setOption("child.reuseAddress", true);
    this.bootstrap.setPipelineFactory(new RestPipelineFactory(storeRepository, config, localZoneId, storeDefinitions, allChannels));
    // Bind and start to accept incoming connections.
    this.nettyServerChannel = this.bootstrap.bind(new InetSocketAddress(this.port));
    allChannels.add(nettyServerChannel);
    logger.info("REST service started on port " + this.port);
    // Register MBeans for Netty worker pool stats
    if (config.isJmxEnabled()) {
        JmxUtils.registerMbean(this, JmxUtils.createObjectName(JmxUtils.getPackageName(this.getClass()), JmxUtils.getClassName(this.getClass())));
    }
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Aggregations

ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)94 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)58 TrackerServer (org.traccar.TrackerServer)42 InetSocketAddress (java.net.InetSocketAddress)38 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)34 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)25 StringEncoder (org.jboss.netty.handler.codec.string.StringEncoder)16 Channel (org.jboss.netty.channel.Channel)13 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)12 StringDecoder (org.jboss.netty.handler.codec.string.StringDecoder)12 LengthFieldBasedFrameDecoder (org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder)11 ChannelFactory (org.jboss.netty.channel.ChannelFactory)8 ExecutorService (java.util.concurrent.ExecutorService)5 DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)5 Test (org.junit.Test)5 HostnamePort (org.neo4j.helpers.HostnamePort)5 CharacterDelimiterFrameDecoder (org.traccar.CharacterDelimiterFrameDecoder)5 IOException (java.io.IOException)4 ChannelException (org.jboss.netty.channel.ChannelException)4 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)4