一、前言
关于netty-all源码包(如4.1.6)中的org.jboss.netty.bootstrap.ServerBootstrap、org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory自定义Server接口、NettyServer服务实现类,详情参见代码示例说明。
二、代码示例
1.Server接口
public interface Server {@b@ @b@ public boolean isStart();@b@ @b@ public void start();@b@ @b@ public void stop();@b@}
2. NettyServer实现类(其中StringUtils类参见其他文章页)
import java.net.InetSocketAddress;@b@import java.util.concurrent.SynchronousQueue;@b@import java.util.concurrent.ThreadPoolExecutor;@b@import java.util.concurrent.TimeUnit;@b@@b@import org.jboss.netty.bootstrap.ServerBootstrap;@b@import org.jboss.netty.channel.ChannelFactory;@b@import org.jboss.netty.channel.ChannelPipeline;@b@import org.jboss.netty.channel.ChannelPipelineFactory;@b@import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@ @b@public abstract class NettyServer implements Server {@b@@b@ protected static final Logger log = LoggerFactory.getLogger(NettyServer.class);@b@ @b@ /**@b@ * 虚函数 @b@ * @return 通道流水线@b@ */@b@ protected abstract ChannelPipeline createPipeline() throws Exception;@b@ @b@ /**@b@ * field@b@ */@b@ @b@ protected boolean isStart = false;@b@ protected ServerBootstrap bootstrap = null;@b@ @b@ public static final int DEFAULT_PORT = 5211;@b@ public static final int DEFAULT_CORE_POOL_SIZE = 0;@b@ public static final int DEFAULT_MAX_POOL_SIZE = Integer.MAX_VALUE;@b@ public static final long DEFAULT_KEEP_ALIVE_TIME = 60L;@b@ public static final TimeUnit DEFAULT_UNIT = TimeUnit.SECONDS;@b@ public static final int DEFAULT_WORKER_COUNT = Runtime.getRuntime().availableProcessors() * 2;@b@@b@ private String port;@b@ private String corePoolSize ;@b@ private String maximumPoolSize;@b@ private String keepAliveSeconds;@b@ private String workerCount;@b@ @b@ @Override@b@ public boolean isStart() {@b@ return isStart;@b@ }@b@ @b@ @Override@b@ public void start() {@b@ if (!isStart) {@b@ try {@b@ bootstrap = new ServerBootstrap(createChannelFactory());@b@ bootstrap.setOption("child.keepAlive", true); @b@ bootstrap.setOption("child.tcpNoDelay", true); @b@ bootstrap.setPipelineFactory(createPiplineFactory());@b@ bootstrap.bind(new InetSocketAddress(StringUtils.parseInt(port, DEFAULT_PORT)));@b@ isStart = true;@b@ log.info("Netty server start with port: " + port);@b@ } catch (Exception e) {@b@ isStart = false;@b@ bootstrap = null;@b@ log.error("Netty server start failed: ", e);@b@ }@b@ } else {@b@ log.info("Netty server[{}] is already running with port: " + port, this.getClass().getSimpleName());@b@ }@b@ }@b@ @b@ @Override@b@ public void stop() {@b@ if (isStart) {@b@ bootstrap.releaseExternalResources();@b@@b@ isStart = false;@b@ bootstrap = null;@b@ log.info("Netty server stop!");@b@ } else {@b@ log.info("Netty server is not running!");@b@ }@b@ }@b@@b@ protected ChannelPipelineFactory createPiplineFactory() {@b@ return new ChannelPipelineFactory() {@b@ @Override@b@ public ChannelPipeline getPipeline() throws Exception {@b@ return createPipeline();@b@ }@b@ };@b@ }@b@@b@ protected ChannelFactory createChannelFactory() {@b@ return new NioServerSocketChannelFactory(@b@ createThreadPool(), @b@ createThreadPool(), @b@ StringUtils.parseInt(workerCount, DEFAULT_WORKER_COUNT));@b@ }@b@@b@ protected ThreadPoolExecutor createThreadPool() {@b@@b@ return new ThreadPoolExecutor(@b@ StringUtils.parseInt(corePoolSize, DEFAULT_CORE_POOL_SIZE), @b@ StringUtils.parseInt(maximumPoolSize, DEFAULT_MAX_POOL_SIZE), @b@ StringUtils.parseLong(keepAliveSeconds, DEFAULT_KEEP_ALIVE_TIME), @b@ DEFAULT_UNIT, @b@ new SynchronousQueue<Runnable>());@b@ }@b@ @b@ /**@b@ * getter and setter @b@ */@b@ @b@ public String getPort() {@b@ return port;@b@ }@b@ @b@ public void setPort(String port) {@b@ this.port = port;@b@ }@b@@b@ public String getCorePoolSize() {@b@ return corePoolSize;@b@ }@b@@b@ public void setCorePoolSize(String corePoolSize) {@b@ this.corePoolSize = corePoolSize;@b@ }@b@@b@ public String getMaximumPoolSize() {@b@ return maximumPoolSize;@b@ }@b@@b@ public void setMaximumPoolSize(String maximumPoolSize) {@b@ this.maximumPoolSize = maximumPoolSize;@b@ }@b@@b@ public String getKeepAliveSeconds() {@b@ return keepAliveSeconds;@b@ }@b@@b@ public void setKeepAliveSeconds(String keepAliveSeconds) {@b@ this.keepAliveSeconds = keepAliveSeconds;@b@ }@b@@b@ public String getWorkerCount() {@b@ return workerCount;@b@ }@b@@b@ public void setWorkerCount(String workerCount) {@b@ this.workerCount = workerCount;@b@ }@b@ @b@}