Search in sources :

Example 41 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850Config method serverBootstrap.

/**
     * Returns a ServerBootstrap setting up a server pipeline listening for
     * incoming IEC61850 register device requests.
     *
     * @return an IEC61850 server bootstrap.
     */
@Bean(destroyMethod = "releaseExternalResources")
public ServerBootstrap serverBootstrap() {
    final ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
    final ServerBootstrap bootstrap = new ServerBootstrap(factory);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws ProtocolAdapterException {
            final ChannelPipeline pipeline = Iec61850Config.this.createChannelPipeline(Iec61850Config.this.iec61850ChannelHandlerServer());
            LOGGER.info("Created new IEC61850 handler pipeline for server");
            return pipeline;
        }
    });
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", false);
    bootstrap.bind(new InetSocketAddress(this.iec61850PortListener()));
    return bootstrap;
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ChannelFactory(org.jboss.netty.channel.ChannelFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) Bean(org.springframework.context.annotation.Bean)

Example 42 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project motan by weibocom.

the class NettyServer method initServerBootstrap.

private synchronized void initServerBootstrap() {
    boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
    int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), URLParamType.workerQueueSize.getIntValue());
    int minWorkerThread = 0, maxWorkerThread = 0;
    if (shareChannel) {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
    } else {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
    }
    standardThreadExecutor = (standardThreadExecutor != null && !standardThreadExecutor.isShutdown()) ? standardThreadExecutor : new StandardThreadExecutor(minWorkerThread, maxWorkerThread, workerQueueSize, new DefaultThreadFactory("NettyServer-" + url.getServerPortStr(), true));
    standardThreadExecutor.prestartAllCoreThreads();
    // 连接数的管理,进行最大连接数的限制 
    channelManage = new NettyServerChannelManage(maxServerConnection);
    bootstrap = new ServerBootstrap(channelFactory);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", true);
    final NettyChannelHandler handler = new NettyChannelHandler(NettyServer.this, messageHandler, standardThreadExecutor);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        // FrameDecoder非线程安全,每个连接一个 Pipeline
        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("channel_manage", channelManage);
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyServer.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder(codec, NettyServer.this));
            pipeline.addLast("handler", handler);
            return pipeline;
        }
    });
}
Also used : DefaultThreadFactory(com.weibo.api.motan.core.DefaultThreadFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 43 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project yyl_example by Relucent.

the class NettyServer method main.

public static void main(String[] args) {
    ServerBootstrap bootstrap = new //
    ServerBootstrap(new //
    NioServerSocketChannelFactory(// boss
    Executors.newCachedThreadPool(), // worker
    Executors.newCachedThreadPool()));
    // Set up the default event pipeline.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
        }
    });
    // Bind and start to accept incoming connections.
    Channel bind = bootstrap.bind(new InetSocketAddress(8000));
    System.out.println("Server started, listening port: " + bind.getLocalAddress() + ", Waiting for client register...");
}
Also used : StringEncoder(org.jboss.netty.handler.codec.string.StringEncoder) InetSocketAddress(java.net.InetSocketAddress) StringDecoder(org.jboss.netty.handler.codec.string.StringDecoder) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Aggregations

ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)43 InetSocketAddress (java.net.InetSocketAddress)29 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)26 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)13 Channel (org.jboss.netty.channel.Channel)12 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)11 ChannelFactory (org.jboss.netty.channel.ChannelFactory)5 DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)5 Test (org.junit.Test)5 HostnamePort (org.neo4j.helpers.HostnamePort)5 IOException (java.io.IOException)4 ExecutorService (java.util.concurrent.ExecutorService)4 ChannelException (org.jboss.netty.channel.ChannelException)4 Executor (java.util.concurrent.Executor)3 HttpRequestDecoder (org.jboss.netty.handler.codec.http.HttpRequestDecoder)3 HttpResponseEncoder (org.jboss.netty.handler.codec.http.HttpResponseEncoder)3 SocketAddress (java.net.SocketAddress)2 SSLEngine (javax.net.ssl.SSLEngine)2 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)2 NioServerBossPool (org.jboss.netty.channel.socket.nio.NioServerBossPool)2