实例
@Component
public class NettyServer implements InitializingBean{
@Resource
private ChannelInitializer<SocketChannel> childHandler;
@Value("${netty.host}")
private String host;
@Value("${netty.port}")
private int port;
public void afterPropertiesSet() throws Exception {
System.out.println("server afterPropertiesSet");
EventLoopGroup parentGroup = new NioEventLoopGroup(Runtime.getRuntime()
.availableProcessors() * 2);
EventLoopGroup childGroup = new NioEventLoopGroup(Runtime.getRuntime()
.availableProcessors() * 2);
ServerBootstrap server = new ServerBootstrap();
server.group(parentGroup, childGroup);
server.channel(NioServerSocketChannel.class);
server.childHandler(childHandler);
server.bind(new InetSocketAddress(host, port)).sync();
}
public void setChildHandler(ChannelInitializer<SocketChannel> childHandler) {
this.childHandler = childHandler;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
}
@Component
public class NettyServer