做网店好还是网站好,个人简历自我评价怎么写,35岁了还能学平面设计吗,建筑培训学校2024年10月14日14:17:07—0.6.0 java-实现一个简单的httpserver-0.6.0 背景功能具体代码打印 背景
通常写了一些接口#xff0c;需要通过临时的http访问#xff0c;又不需要spring这么厚重的框架
功能
设置并发监控并发两个get请求一个是根路径#xff0c;一个是other增加… 2024年10月14日14:17:07—0.6.0 java-实现一个简单的httpserver-0.6.0 背景功能具体代码打印 背景
通常写了一些接口需要通过临时的http访问又不需要spring这么厚重的框架
功能
设置并发监控并发两个get请求一个是根路径一个是other增加了一个post请求的方法other1
具体代码
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;public class server {private static AtomicInteger concurrentConnections new AtomicInteger(0);public static void main(String[] args) throws IOException {HttpServer server HttpServer.create(new InetSocketAddress(localhost, 8222), 0);// 处理根路径get请求server.createContext(/, new RootHandler());// 处理 /other 路径get请求server.createContext(/other, new OtherHandler());// 处理 /other1 路径post请求server.createContext(/other1, new Other1Handler());// 设置并发连接数server.setExecutor(java.util.concurrent.Executors.newFixedThreadPool(10));server.start();System.out.println(Server started on port 8000.);// 定期打印当前并发连接数new Thread(() - {while (true) {try {Thread.sleep(5000);System.out.println(Current concurrent connections: concurrentConnections.get());} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}).start();}static class RootHandler implements HttpHandler {Overridepublic void handle(HttpExchange exchange) throws IOException {concurrentConnections.incrementAndGet();if (GET.equals(exchange.getRequestMethod())) {String response Hello from root path!;exchange.sendResponseHeaders(200, response.length());OutputStream os exchange.getResponseBody();os.write(response.getBytes());os.close();// 监控连接释放事件System.out.println(root Connection released);}concurrentConnections.decrementAndGet();}}static class OtherHandler implements HttpHandler {Overridepublic void handle(HttpExchange exchange) throws IOException {concurrentConnections.incrementAndGet();if (GET.equals(exchange.getRequestMethod())) {String response This is a response for /other path.;exchange.sendResponseHeaders(200, response.length());OutputStream os exchange.getResponseBody();os.write(response.getBytes());os.close();}// 监控连接释放事件System.out.println(/other Connection released);concurrentConnections.decrementAndGet();}}static class Other1Handler implements HttpHandler {Overridepublic void handle(HttpExchange exchange) throws IOException {concurrentConnections.incrementAndGet();if (POST.equals(exchange.getRequestMethod())) {String response This is a response for /other1 post path.;exchange.sendResponseHeaders(200, response.length());OutputStream os exchange.getResponseBody();os.write(response.getBytes());os.close();}// 监控连接释放事件System.out.println(/other Connection released);concurrentConnections.decrementAndGet();}}
}
打印
root Connection released
Current concurrent connections: 0
/other Connection released
root Connection released
/other Connection released
/other Connection released
/other Connection released
/other Connection released
/other Connection released
Current concurrent connections: 0
/other Connection released
/other Connection released
/other Connection released
root Connection released
Current concurrent connections: 0
Current concurrent connections: 0
文章转载自: http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.zplzj.cn.gov.cn.zplzj.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.bwdnx.cn.gov.cn.bwdnx.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.dpbdq.cn.gov.cn.dpbdq.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.wfzdh.cn.gov.cn.wfzdh.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.cpkcq.cn.gov.cn.cpkcq.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.kaylyea.com.gov.cn.kaylyea.com http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn