当前位置: 首页 > news >正文

java-实现一个简单的httpserver-0.5.0

背景

通常写了一些接口,需要通过临时的http访问,又不需要spring这么厚重的框架

功能

  1. 设置并发
  2. 监控并发
  3. 两个get请求一个是根路径,一个是other

具体代码

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);// 处理根路径请求server.createContext("/", new RootHandler());// 处理 /other 路径请求server.createContext("/other", new OtherHandler());// 设置并发连接数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();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();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();}}
}

打印

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.mrgr.cn/news/48681.html

相关文章:

  • 记录IDEA编译报错:不支持发行版本
  • 【Termux使用指南】
  • 机器学习pytorch实践1
  • 元学习案例(学习如何学习)
  • 基于 CSS Grid 的简易拖拉拽 Vue3 组件,从代码到NPM发布(2)- NPM发布、在线示例
  • ROS2 与 ROS1 的 CMakeLists.txt 文件区别
  • K8s-services+pod详解1
  • 剧场的客户端形式区别,APP,小程序,H5的不同优势以及推广方案
  • Spring框架中的单例Bean是线程安全的么?
  • 论文 | Context-faithful Prompting for Large Language Models
  • [红队apt]自解压文件攻击
  • 无人机之交互系统篇
  • JAVA开发中SpringMVC框架的使用及常见的404问题原因以及SpringMVC框架基于注解的开发实例
  • 财政部今日新闻发布会深度解析
  • 空间解析几何4-空间中线段到圆的距离【附MATLAB代码】
  • 【入门第3课】Splunk字段提取
  • 微前端简单实用
  • 哪个软件可以在线编辑ppt? 一口气推荐5个做ppt的得力助手!
  • 微调大型语言模型:根据您的需求定制Llama 3 8B
  • flutter 仿淘宝推荐二级分类效果