Netty Server

This commit is contained in:
lyr90329 2014-07-31 14:53:04 +08:00
parent 21b7f5adfe
commit 4026bba33b
1 changed files with 45 additions and 0 deletions

45
src/server/Server.java Normal file
View File

@ -0,0 +1,45 @@
package server;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class Server
{
static Server server;
ServerBootstrap bootstrap;
public void init(int port)
{
bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Set up the default event pipeline.
bootstrap.setPipelineFactory(new MServerPipelineFactory());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("reuseAddress", true);
// Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(port));
System.out.println("server init complete");
}
public void stop()
{
bootstrap.releaseExternalResources();
}
public static Server getInstance()
{
if (server == null)
{
server = new Server();
}
return server;
}
}