java实现可以自行关闭的服务器
普通实现的服务器都无法关闭自身,只有依靠操作系统来强行终止服务程序。这种强行终止服务程序的方式尽管简单方便,但会导致服务器中正在执行的任务突然中断。如果服务器处理的任务非常重要,不允许被突然中断,应该由服务器自身在恰当的时刻关闭自己
代码如下:
- echoserver类
package shutdownserver; import java.io.*; import java.net.serversocket; import java.net.socket; import java.net.socketexception; import java.net.sockettimeoutexception; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.rejectedexecutionexception; import java.util.concurrent.timeunit; public class echoserver { private int port=8000; private serversocket serversocket; private executorservice executorservice; //线程池 private final int pool_size=4; //单个cpu时线程池中工作线程的数目 private int portforshutdown=8001; //用于监听关闭服务器命令的端口 private serversocket serversocketshutdown; private boolean isshutdown=false; //服务器是否已经关闭 private thread shutdownthread=new thread(){ //负责关闭服务器的线程 public void run(){ while(!isshutdown){ socket socketforshutdown=null; try{ socketforshutdown=serversocketshutdown.accept(); bufferedreader br=new bufferedreader( new inputstreamreader(socketforshutdown.getinputstream()) ); string command=br.readline(); if (command.equals("shutdown")){ long begintime=system.currenttimemillis(); socketforshutdown.getoutputstream().write("服务器正在关闭\r\n".getbytes()); isshutdown=true; //请求关闭线程池 //线程池不再接收新的任务,但会继续执行完工作队列中现有的任务 executorservice.shutdown(); //等待关闭线程池,每次等待的超时时间为30s //当使用awaittermination时,主线程会处于一种等待的状态,等待线程池中所有的线程都运行完毕后才继续运行。 //如果等待的时间超过指定的时间,但是线程池中的线程运行完毕,那么awaittermination()返回true。执行分线程已结束 //如果等待的时间超过指定的时间,但是线程池中的线程未运行完毕,那么awaittermination()返回false。不执行分线程已结束 //如果等待时间没有超过指定时间,等待! //可以用awaittermination()方法来判断线程池中是否有继续运行的线程。 while(!executorservice.isterminated()) executorservice.awaittermination(30, timeunit.seconds); //关闭与echoclient客户通信的serversocket serversocket.close(); long endtime=system.currenttimemillis(); socketforshutdown.getoutputstream().write(("服务器关闭,"+"关闭服务器用了"+(endtime-begintime)+"ms\r\n").getbytes()); socketforshutdown.close(); serversocketshutdown.close(); system.out.println("服务器关闭"); } else { socketforshutdown.getoutputstream().write("错误的命令\r\n".getbytes()); socketforshutdown.close(); } } catch (exception e) { e.printstacktrace(); } } } }; public echoserver() throws ioexception { serversocket=new serversocket(port); //设定等待客户连接的超时时间为60s serversocket.setsotimeout(60000); serversocketshutdown=new serversocket(portforshutdown); //创建线程池 executorservice= executors.newfixedthreadpool(runtime.getruntime().availableprocessors() * pool_size); shutdownthread.start(); system.out.println("服务器启动"); } public void service(){ while(!isshutdown){ socket socket=null; try { //可能会抛出sockettimeoutexception和socketexception socket=serversocket.accept(); //把等待客户发送数据的超时时间设为60s socket.setsotimeout(60000); //可能会抛出rejectedexecutionexception executorservice.execute(new handler(socket)); }catch (sockettimeoutexception e){ //不必处理等待客户连接时出现的异常 }catch (rejectedexecutionexception e) { try { if (socket != null) socket.close(); } catch (ioexception ex) { return; } }catch (socketexception e){ if (e.getmessage().indexof("socket closed")!=-1) return; }catch (ioexception e){ e.printstacktrace(); } } } public static void main(string[] args) throws ioexception { //main方法抛出异常,异常直接交给虚拟机,虚拟机直接结束异常 new echoserver().service(); } } //负责与单个客户通信的任务 class handler implements runnable{ private socket socket; public handler(socket socket){ this.socket=socket; } private printwriter getwriter(socket socket) throws ioexception{ outputstream socketout=socket.getoutputstream(); return new printwriter(socketout,true); } private bufferedreader getreader(socket socket) throws ioexception{ inputstream socketin=socket.getinputstream(); return new bufferedreader(new inputstreamreader(socketin)); } public string echo(string msg){ return "echo: "+msg; } @override public void run() { try{ system.out.println("new connection accepted "+socket.getinetaddress()+":"+socket.getport()); bufferedreader br=getreader(socket); printwriter pw=getwriter(socket); string msg=null; //接收和发送数据,直到通信结束 while((msg=br.readline())!=null){ system.out.println("from "+socket.getinetaddress()+":"+socket.getport()+">"+msg); pw.println(echo(msg)); if (msg.equals("bye")) break; } } catch (ioexception e) { e.printstacktrace(); }finally{ try{ if (socket!=null) socket.close(); }catch (ioexception e){ e.printstacktrace(); } } } }
- adminclient类(负责向echoserver发送“shutdown”命令,关闭服务器)
package shutdownserver; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstream; import java.net.socket; public class adminclient { public static void main(string[] args){ socket socket=null; try{ socket=new socket("localhost",8001); //发送关闭命令 outputstream socketout=socket.getoutputstream(); //scanner scanner=new scanner(system.in); //string order=scanner.next(); socketout.write("shutdown\r\n".getbytes()); //接收服务器反馈 bufferedreader br=new bufferedreader(new inputstreamreader(socket.getinputstream())); string msg=null; while ((msg=br.readline())!=null){ system.out.println(msg); } } catch (exception e) { e.printstacktrace(); }finally { try{ if (socket!=null) socket.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
- client类(客户,与服务器进行通讯)
package shutdownserver; import java.io.*; import java.net.socket; public class client { private string host="localhost"; private int port=8000; private socket socket; public client() throws ioexception { socket=new socket(host,port); } private printwriter getwriter(socket socket) throws ioexception{ outputstream socketout=socket.getoutputstream(); return new printwriter(socketout,true); } private bufferedreader getreader(socket socket) throws ioexception{ inputstream socketin=socket.getinputstream(); return new bufferedreader(new inputstreamreader(socketin)); } public void talk() throws ioexception{ try{ bufferedreader br=getreader(socket); printwriter pw=getwriter(socket); bufferedreader localreader=new bufferedreader(new inputstreamreader(system.in)); string msg=null; while((msg=localreader.readline()) != null){ pw.println(msg); system.out.println(br.readline()); if (msg.equals("bye")){ break; } } }catch (ioexception e){ e.printstacktrace(); } finally { try{ socket.close(); }catch (ioexception e){ e.printstacktrace(); } } } public static void main(string args[]) throws ioexception { new client().talk(); } }
shutdownthread线程负责关闭服务器,它一直监听8001端口,如果接收到了adminclient发送的“shutdown”命令,就把isshutdown设置为true。
在关闭服务器时,我们使用了最常用的方法,先调用线程池的shutdown()方法,接着调用线程池的awaittermination()方法。
executorservice.shutdown(); //等待关闭线程池,每次等待的超时时间为30s //当使用awaittermination时,主线程会处于一种等待的状态,等待线程池中所有的线程都运行完毕后才继续运行。 //如果等待的时间超过指定的时间,但是线程池中的线程运行完毕,那么awaittermination()返回true。执行分线程已结束 //如果等待的时间超过指定的时间,但是线程池中的线程未运行完毕,那么awaittermination()返回false。不执行分线程已结束 //如果等待时间没有超过指定时间,等待! //可以用awaittermination()方法来判断线程池中是否有继续运行的线程。 while(!executorservice.isterminated()) executorservice.awaittermination(30, timeunit.seconds);
在线程池执行了shutdown()方法后,线程池不会在接收新的任务,同时该线程因为调用awaittermination()方法而发生阻塞,直到线程池中所有线程的任务执行完毕,该线程才会继续向下
运行结果
先运行echoserver,client,adminclient后,再开启一客户程序client1,显示client1无法被加入线程池
- echoserver(只显示连接了client,未连接client1)
- client
- client2(向服务器发送消息,收到null)
- adminclient(在client没有运行结束时,被阻塞)
当client输入“bye”结束运行后,adminclient关闭服务器
- client类
- echoserver类
- adminclient类
参考java网络编程核心技术详解
到此这篇关于教你利用java实现可以自行关闭服务器的方法的文章就介绍到这了,更多相关java自行关闭服务器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_45821324/article/details/117878699