引言
本文回忆一下Java中的io编程,看一下目前Java提供的网络编程库的演进过程。
本文所有的代码都在这类代码地址
BIO编程
采用BIO通信模型的服务端,由独立的Acceptor线程负责监听客户端连接,它接收到客户端连接后为每一个客户创建一个新的线程模型进行业务处理,处理完成之后,通过输出流返回给客户端,然后线程就销毁了,基本流程图可以看下图:
代码
BIODemoClient
public class BIODemoClient {
public static void main(String[] args) throws IOException {
int port = 8080;
final Socket socket = new Socket("localhost", port);
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("查询时间");
out.println("QUERY TIME ORDER");
final String resp = in.readLine();
System.out.println("服务器返回时间是:"+ resp);
}
}
BIODemoServer
public class BIODemoServer {
public static void main(String[] args) throws IOException {
int port = 8080;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("服务器启动,端口:"+8080);
Socket socket = null;
while(true){
socket = serverSocket.accept();
new Thread(new TimeServerHandler(socket)).start();
}
} finally {
if(serverSocket != null){
System.out.println("服务器关闭");
serverSocket.close();
serverSocket = null;
}
}
}
}
TimeServerHandler
public class TimeServerHandler implements Runnable{
private Socket socket;
public TimeServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
String body = null;
String currentTime = null;
while (true){
body = in.readLine();
if(body == null){
break;
}
System.out.println("时间服务器接收命令:"+body);
currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)? new Date(System.currentTimeMillis()).toString(): "BAD ORDER";
out.println(currentTime);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//关闭资源
if(in !=null){
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(out != null){
out.close();
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
socket = null;
} }
}
}
BIO编程的优化
完成上面的demo之后我们发现,每次请求都对应一次线程的创建,大家都知道在Java中线程资源十分昂贵,自然而然我们就想到用线程池去优化。所以,我们的编程模型做了一点稍微的改动,具体如下图:
代码
只需要简单修改下服务端的代码即可
public class BIODemoServer2 {
public static void main(String[] args) throws IOException {
int port = 8080;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("服务器启动,端口:"+8080);
//增加一个线程池
final ExecutorService executorService = Executors.newFixedThreadPool(2);
Socket socket = null;
while(true){
socket = serverSocket.accept();
executorService.execute(new TimeServerHandler(socket));
// new Thread(new TimeServerHandler(socket)).start();
}
} finally {
if(serverSocket != null){
System.out.println("服务器关闭");
serverSocket.close();
serverSocket = null;
}
}
}
}
NIO编程
NIO编程,也叫非阻塞编程。是JDK在1.4引入,祢补了同步IO的不足,为Java提供了高速的,面向块的IO(BIO是流模型)。
NIO这一套IO其核心思想就是通过事件通知的方式告诉select哪些连接已经就绪可以进行IO操作,这种模型提供了更好的资源管理:
- 是用较少的线程可以处理许多连接,因此也减少了内存管理和上下文切换所带来开销
- 当没有 I/O 操作需要处理的时候,线程也可以被用于其他任务
下面是IO模型图:
代码
TimeServerHandler
public class TimeServerHandler implements Runnable{
private Socket socket;
public TimeServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
String body = null;
String currentTime = null;
while (true){
body = in.readLine();
if(body == null){
break;
}
System.out.println("时间服务器接收命令:"+body);
currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)? new Date(System.currentTimeMillis()).toString(): "BAD ORDER";
out.println(currentTime);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//关闭资源
if(in !=null){
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(out != null){
out.close();
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
socket = null;
} }
}
}
TimeClientHandle
public class TimeClientHandle implements Runnable {
private String host;
private int port;
private Selector selector;
private SocketChannel socketChannel;
private volatile boolean stop;
public TimeClientHandle(String host, int port) {
this.host = host == null ? "127.0.0.1" : host;
this.port = port;
try {
//1.打开SocketChannel
socketChannel = SocketChannel.open();
//2.设置SocketChannel为非阻塞模式
socketChannel.configureBlocking(false);
//4.创建多路复用器
selector = Selector.open();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void run() {
try {
doConnect();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
while (!stop) {
try {
//5.轮询准备就绪的key
selector.select(1000);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
SelectionKey key = null;
while (iterator.hasNext()) {
key = iterator.next();
iterator.remove();
try {
handleInput(key);
} catch (Exception e) {
if (key != null) {
key.cancel();
if (key.channel() != null)
key.channel().close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
//多路复用器关闭后,所有注册在上面的Channel和Pipe等资源都会被自动去注册并关闭,所以不需要重复释放资源
if (selector != null) {
try {
selector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException {
if (key.isValid()) {
SocketChannel sc = (SocketChannel) key.channel();
//6.接收connect事件进行处理
if (key.isConnectable()) {
//7.连接成功,注册读事件到多路复用器
if (sc.finishConnect()) {
sc.register(selector, SelectionKey.OP_READ);
doWrite(sc);
} else {
System.exit(1);
}
}
//8.异步读服务器返回消息到缓冲区
if (key.isReadable()) {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("Now is:" + body);
this.stop = true;
} else if (readBytes < 0) {
key.cancel();
sc.close();
} else {
System.out.println("0 size bytes.");
}
}
}
}
private void doConnect() throws IOException {
//3.异步连接服务器
//成功,注册
if (socketChannel.connect(new InetSocketAddress(host, port))) {
socketChannel.register(selector, SelectionKey.OP_READ);
doWrite(socketChannel);
} else {//注册,监听服务器的ack
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
private void doWrite(SocketChannel sc) throws IOException {
//9.调用SocketChannel的异步write接口,将消息异步发送给服务器
byte[] req = "QUERY TIME ORDER".getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
writeBuffer.put(req);
writeBuffer.flip();
sc.write(writeBuffer);
if (!writeBuffer.hasRemaining()) {
System.out.println("Send order 2 server succeed.");
}
}
}
我们可以看出,使用Java NIO API开发的程序过程十分繁琐,API设计的一点都不易用,最关键的是臭民昭著的epoll bug至今还没有完善,这就是为什么netty会诞生。Netty设计的更加合理易于使用,性能比原生的NIO还要好,并且占用资源更加低,还有什么理由不去使用呢?
ok,这次先聊到这,下一篇应该是进入正题了,开始学习netty的开发。