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
相关推荐
PT_silver10 分钟前
tryhackme——Abusing Windows Internals(进程注入)
windows·microsoft
程序员岳焱18 分钟前
深度剖析:Spring AI 与 LangChain4j,谁才是 Java 程序员的 AI 开发利器?
java·人工智能·后端
有风南来18 分钟前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
wangjinjin18018 分钟前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python
都叫我大帅哥26 分钟前
AQS(AbstractQueuedSynchronizer)深度解剖:从“奶茶店排队”到源码级设计哲学
java
斯奕sky_small-BAD31 分钟前
C++ if语句完全指南:从基础到工程实践
java·开发语言·php
云之渺32 分钟前
125java
java
都叫我大帅哥33 分钟前
Java ReentrantLock:从“舔狗式等待”到源码级征服指南
java
程序员岳焱40 分钟前
Java 高级泛型实战:8 个场景化编程技巧
java·后端·编程语言
钢铁男儿1 小时前
C# 类和继承(扩展方法)
java·servlet·c#