背景
1、hibernate框架操作数据库
2、netty框架接受数据(非阻塞)
3、绑定多个端口
存储在数据库里面的数据如下
关键代码在这里
package cn.com.service;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class ImServer implements Runnable{
private int port;
public ImServer(int i) {
// TODO Auto-generated constructor stub
this.port=i;
}
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerStringHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
try {
ChannelFuture f = bootstrap.bind(port).sync();
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void mystart() {
new Thread(new ImServer(6013)).start();
new Thread(new ImServer(6019)).start();
new Thread(new ImServer(6061)).start();
new Thread(new ImServer(6011)).start();
new Thread(new ImServer(6071)).start();
new Thread(new ImServer(6021)).start();
new Thread(new ImServer(6023)).start();
new Thread(new ImServer(6014)).start();
new Thread(new ImServer(6015)).start();
new Thread(new ImServer(6088)).start();
new Thread(new ImServer(6016)).start();
new Thread(new ImServer(6024)).start();
new Thread(new ImServer(6017)).start();
new Thread(new ImServer(6022)).start();
new Thread(new ImServer(6020)).start();
new Thread(new ImServer(6018)).start();
new Thread(new ImServer(6040)).start();
new Thread(new ImServer(6010)).start();
}
}
完整的代码在这里https://download.csdn.net/download/qq_37591637/11266521