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

背景

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

功能

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

具体代码

java 复制代码
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 {
        @Override
        public 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 {
        @Override
        public 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();
        }
    }
}

打印

java 复制代码
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
相关推荐
张槊哲4 分钟前
函数的定义与使用(python)
开发语言·python
船长@Quant9 分钟前
文档构建:Sphinx全面使用指南 — 实战篇
python·markdown·sphinx·文档构建
iuyou️10 分钟前
Spring Boot知识点详解
java·spring boot·后端
北辰浮光13 分钟前
[Mybatis-plus]
java·开发语言·mybatis
一弓虽22 分钟前
SpringBoot 学习
java·spring boot·后端·学习
南客先生26 分钟前
互联网大厂Java面试:RocketMQ、RabbitMQ与Kafka的深度解析
java·面试·kafka·rabbitmq·rocketmq·消息中间件
ai大佬30 分钟前
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
java·spring·自动化·api中转·apikey
Mr__Miss1 小时前
面试踩过的坑
java·开发语言
偶尔微微一笑1 小时前
AI网络渗透kali应用(gptshell)
linux·人工智能·python·自然语言处理·编辑器
爱喝一杯白开水1 小时前
POI从入门到上手(一)-轻松完成Apache POI使用,完成Excel导入导出.
java·poi