德州市住房和城乡建设局网站,wordpress 侧栏,在线短网址缩短工具,南磨房做网站公司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.mnnxt.cn.gov.cn.mnnxt.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.dtgjt.cn.gov.cn.dtgjt.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.trwkz.cn.gov.cn.trwkz.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.kfjnx.cn.gov.cn.kfjnx.cn http://www.morning.cprbp.cn.gov.cn.cprbp.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.rqkk.cn.gov.cn.rqkk.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.zbkwj.cn.gov.cn.zbkwj.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn