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
相关推荐
SunnyDays101122 分钟前
如何使用 Java 删除 Word 文档中的水印
java·删除word文档水印
IT逆夜1 小时前
实现Yum本地仓库自动同步的完整方案(CentOS 7)
linux·运维·windows
毕设源码-邱学长1 小时前
【开题答辩全过程】以 基于Java企业人事工资管理系统为例,包含答辩的问题和答案
java·开发语言
转转技术团队1 小时前
回收系统架构演进实战:与Cursor结对扫清系统混沌
java·架构·cursor
AI分享猿1 小时前
Java后端实战:SpringBoot接口遇异常请求,轻量WAF兼顾安全与性能
java·spring boot·安全
稚辉君.MCA_P8_Java1 小时前
Gemini永久会员 Java中的四边形不等式优化
java·后端·算法
DKPT1 小时前
ZGC和G1收集器相比哪个更好?
java·jvm·笔记·学习·spring
n***F8752 小时前
修改表字段属性,SQL总结
java·数据库·sql
q***69772 小时前
【Spring Boot】统一数据返回
java·spring boot·后端
v***59832 小时前
DeepSeek API 调用 - Spring Boot 实现
windows·spring boot·后端