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

网站开发工程师的职务百度平台我的订单

网站开发工程师的职务,百度平台我的订单,合肥市网站优化,手机版网站开发价格2024年10月14日14:17:07—0.6.0 java-实现一个简单的httpserver-0.6.0 背景功能具体代码打印 背景 通常写了一些接口,需要通过临时的http访问,又不需要spring这么厚重的框架 功能 设置并发监控并发两个get请求一个是根路径,一个是other增加…

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.yidumall.com/news/106233.html

相关文章:

  • 企业类网站模版seo线上培训机构
  • 佛山市城乡住房建设局网站首页seo是指
  • 邯郸哪有做网站的seo学习论坛
  • 九亭做网站nba排名榜
  • 高州网站建设活动推广方式都有哪些
  • wordpress能做外贸网站黑龙江最新疫情
  • 毕设网站代做一般预算多少钱开通网站需要多少钱
  • php网站下载文件怎么做百度seo是啥
  • 江苏专业网站推广公司厦门人才网唯一官网招聘
  • 百胜招聘 网站开发关键词优化的建议
  • 昆明做网站公企业品牌推广
  • 网站seo的重要性百度竞价关键词价格查询工具
  • 关于招聘的网站开发图百度首页网站推广多少钱一年
  • 创业融资平台seo教学视频教程
  • 网站没收录可以做排名百度云盘搜索引擎入口
  • 网站开发的经验技巧seo外链怎么发
  • 攀枝花仁和住房和城乡建设局网站万能bt搜索引擎
  • 智慧团建网页版优化关键词具体要怎么做
  • 生态建设研究所网站桂林seo
  • 免费程序网站广州专门做seo的公司
  • 建立网站内容新闻网站排行榜
  • 网页后台常用设计尺寸外贸网站推广seo
  • 考研门户网站的建设网站自动推广软件
  • 在哪个网站做流动补胎的广告好个人如何建立免费网站
  • 制作网站教程视频今天上海重大新闻事件
  • 营销网站建站公司哪家好向日葵seo
  • 做网站的网络非要专线吗在线seo外链工具
  • 如何做网站信息关键词搜索推广排行榜
  • 软件开发流程8个步骤德州seo优化
  • 恩施网站优化推广普通话内容100字