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

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

2024年10月14日14:17:07—0.6.0

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

    • 背景
    • 功能
    • 具体代码
    • 打印

背景

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

功能

  1. 设置并发
  2. 监控并发
  3. 两个get请求一个是根路径,一个是other
  4. 增加了一个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.lryc.cn/news/461270.html

相关文章:

  • 【论文#码率控制】ADAPTIVE RATE CONTROL FOR H.264
  • 2024-10-16 学习人工智能的Day8
  • Python Django 数据库优化与性能调优
  • 基于SpringBoot+微信小程序的农产品销售平台
  • 微前端学习以及分享
  • 【Linux-进程间通信】vscode使用通信引入匿名管道引入
  • nerd bug:VPG多次计算vnetloss的计算图报错的解决
  • BigDecimal类Date类JDK8日期
  • MybatisWebApp
  • 第十五章 RabbitMQ延迟消息之延迟插件
  • OpenAI 公布了其新 o1 模型家族的元提示(meta-prompt)
  • Java基础14-网络编程
  • sed命令详解
  • Linux高阶——1013—正则表达式练习
  • 【CMake】为可执行程序或静态库添加 Qt 资源文件,静态库不生效问题
  • 服务器、jvm、数据库的CPU飙高怎么处理
  • 自适应过滤法—初级
  • UML图有用吗?真正厉害的软件开发,有用的吗?什么角色用?
  • 基于Java+Springboot+Vue开发的酒店客房预订管理系统
  • OpenCV高级图形用户界面(5)获取指定滑动条(trackbar)的当前位置函数getTrackbarPos()的使用
  • 拓扑排序在实际开发中的应用
  • 【CTF-SHOW】Web入门 Web27-身份证日期爆破 【关于bp intruder使用--详记录】
  • Windows 添加右键以管理员身份运行 PowerShell
  • 数学建模算法与应用 第15章 预测方法
  • HC32F460KETA PETB JATA 工业 自动化 电机
  • linux系统,不定时kernel bug :soft lockup的问题
  • 【C语言教程】【常用类库】(十四)可移植库 - <unistd.h> 和 <sys/types.h>
  • Java项目实战II基于Spring Boot的周边游平台设计与实现(源码+数据库+文档)
  • 远程控制软件哪个好用:4款主流的远程控制软件大点评,谁最给力?
  • 基于springboot实习管理系统