【仿写tomcat】六、解析xml文件配置端口、线程池核心参数
线程池改造
上一篇文章中我们用了Excutors创建了线程,这里我们将它改造成包含所有线程池核心参数的形式。
package com.tomcatServer.http;import java.util.concurrent.*;/*** 线程池跑龙套** @author ez4sterben* @date 2023/08/05*/
public class ThreadPool {private int corePoolSize;private int maximumPoolSize;private long keepAliveTime;private static ThreadPoolExecutor threadPoolExecutor;public ThreadPool() {}public synchronized ThreadPoolExecutor getInstance() {if (threadPoolExecutor == null) {BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();ThreadFactory threadFactory = Executors.defaultThreadFactory();threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,TimeUnit.SECONDS,workQueue,threadFactory);}return threadPoolExecutor;}public static synchronized void shutdown() {if (threadPoolExecutor != null) {threadPoolExecutor.shutdown();threadPoolExecutor = null;}}public int getCorePoolSize() {return corePoolSize;}public void setCorePoolSize(int corePoolSize) {this.corePoolSize = corePoolSize;}public void setMaximumPoolSize(int maximumPoolSize) {this.maximumPoolSize = maximumPoolSize;}public void setKeepAliveTime(long keepAliveTime) {this.keepAliveTime = keepAliveTime;}
}
主方法中对多线程操作部分改为使用CompletableFuture执行
// 5.初始化线程池ThreadPoolExecutor executor = XmlParseUtil.initThreadPool(ROOT);
// 6.处理http请求try {SocketStore.connect(port);while (true){Socket accept = SocketStore.getSocket().accept();if (accept != null){CompletableFuture.runAsync(() -> {try {SocketStore.handleRequest(accept);} catch (IOException e) {throw new RuntimeException(e);}}, executor);}}} catch (IOException e) {throw new RuntimeException(e);}finally {SocketStore.close();}
解析xml文件
现在我们有一个server.xml文件,我想解析其中的端口号以及线程池参数
<tomcat-server><port>80</port><core-pool-size>4</core-pool-size><maximum-pool-size>8</maximum-pool-size><keep-alive-time>60</keep-alive-time>
</tomcat-server>
如果想完成这个功能可以直接使用java本身自带的工具类,下面附上代码
package com.tomcatServer.utils;import com.tomcatServer.http.ThreadPool;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;public class XmlParseUtil {public static Integer parseServerConfig(String root){int port = 8080;try {NodeList nodeList = getServerConfig(root);for (int i = 0; i < nodeList.getLength(); i++) {Node node = nodeList.item(i);if (node.getNodeType() == Node.ELEMENT_NODE) {Element element = (Element) node;port = Integer.parseInt(element.getElementsByTagName("port").item(0).getTextContent().trim());}}} catch (Exception e) {e.printStackTrace();}return port;}public static ThreadPoolExecutor initThreadPool(String root){ThreadPool threadPool = new ThreadPool();int corePoolSize = 4;int maximumPoolSize = 8;int keepAliveTime = 60;try {NodeList nodeList = getServerConfig(root);for (int i = 0; i < nodeList.getLength(); i++) {Node node = nodeList.item(i);if (node.getNodeType() == Node.ELEMENT_NODE) {Element element = (Element) node;corePoolSize = Integer.parseInt(element.getElementsByTagName("core-pool-size").item(0).getTextContent().trim());maximumPoolSize = Integer.parseInt(element.getElementsByTagName("maximum-pool-size").item(0).getTextContent().trim());keepAliveTime = Integer.parseInt(element.getElementsByTagName("keep-alive-time").item(0).getTextContent().trim());}}} catch (Exception e) {e.printStackTrace();}threadPool.setCorePoolSize(corePoolSize);threadPool.setMaximumPoolSize(maximumPoolSize);threadPool.setKeepAliveTime(keepAliveTime);System.out.println(threadPool.getCorePoolSize());return threadPool.getInstance();}private static NodeList getServerConfig(String root) throws ParserConfigurationException, SAXException, IOException {File inputFile = new File(root + "\\src\\main\\java\\com\\tomcatServer\\config\\server.xml");DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse(inputFile);document.getDocumentElement().normalize();return document.getElementsByTagName("tomcat-server");}
}
启动测试

现在我的配置文件是这样的,在主方法中打印一下端口号,如果是80说明这个xml扫描成功了,然后我们再去访问80端口的Index页面。


http://localhost:8080/index.html
尝试访问8080时,已经无法访问了

接下来访问80端口

访问成功
现在我们的tomcat已经有一定的功能了,下一篇作者将对整个tomcat的代码结构做一些优化,并将现阶段的代码分享给读者。
【仿写tomcat】七、项目结构优化以及代码开源
相关文章:
【仿写tomcat】六、解析xml文件配置端口、线程池核心参数
线程池改造 上一篇文章中我们用了Excutors创建了线程,这里我们将它改造成包含所有线程池核心参数的形式。 package com.tomcatServer.http;import java.util.concurrent.*;/*** 线程池跑龙套** author ez4sterben* date 2023/08/05*/ public class ThreadPool {pr…...
Android Studio 接入OpenCV最简单的例子 : 实现灰度图效果
1. 前言 上文 我们在Windows电脑上实现了人脸功能,接下来我们要把人脸识别的功能移植到Android上。 那么首先第一步,就是要创建一个Native的Android项目,并且配置好OpenGL,并能够调用成功。 这里我们使用的是openCV-4.8.0&#x…...
(1)、扩展SpringCache一站式解决缓存击穿,穿透,雪崩
1、问题描述 我们在使用SpringCache的@Cacheable注解时,发现并没有设置过期时间这个功能。 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @I...
Rancher使用cert-manager安装报错解决
报错: rancher-rke-01:~/rke/rancher-helm/rancher # helm install rancher rancher-stable/rancher --namespace cattle-system --set hostnamewww.rancher.local Error: INSTALLATION FAILED: Internal error occurred: failed calling webhook "webhook…...
Harvard transformer NLP 模型 openNMT 简介入门
项目网址: OpenNMT - Open-Source Neural Machine Translation logo: 一,从应用的层面先跑通 Harvard transformer GitHub - harvardnlp/annotated-transformer: An annotated implementation of the Transformer paper. git clone https…...
【数据结构OJ题】用栈实现队列
原题链接:https://leetcode.cn/problems/implement-queue-using-stacks/ 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 用两个栈实现,一个栈进行入队操作,另一个栈进行出队操作。 出队操作: 当出队的栈…...
通达信指标公式15:除权除息数据统计分析
#1.关于除权除息指标的介绍:本指标是小红牛原创指标之一,觉得有必要研究一下这个问题,所以就花时间整理一下这个指标相关内容,大家可以在本源码基础上,进一步优化自己的思路。本指标为通达信幅图指标,可以做…...
day-27 代码随想录算法训练营(19)回溯part03
39.组合总和 分析:同一个数可以选多次,但是不能有重复的答案; 思路:横向遍历,纵向递归(不同的是递归的时候不需要跳到下一个位置,因为同一个数可以选多次) class Solution { publ…...
CSDN编程题-每日一练(2023-08-22)
CSDN编程题-每日一练(2023-08-22) 一、题目名称:最长递增区间二、题目名称:K树三、题目名称:小Q的价值无向图一、题目名称:最长递增区间 时间限制:1000ms内存限制:256M 题目描述: 给一个无序数组,求最长递增的区间长度。如:[5,2,3,8,1,9] 最长区间 2,3,8 长度为 3。…...
使用 KubeBlocks 为 K8s 提供稳如老狗的数据库服务
原文链接:https://forum.laf.run/d/994 大家好!今天这篇文章主要向大家介绍 Sealos 的数据库服务。在 Sealos 上数据库后端服务由 KubeBlocks 提供,为用户的数据库应用保驾护航。无论你是在公有云还是本地环境中使用,Sealos 都能为…...
SFL212B-10-21-15、SFL212B-20-21-40喷嘴挡板伺服阀
SFL212B-05-21-10、SFL212B-10-21-15、SFL212B-20-21-40、SFL212-05-32-10、SFL212-10-32-15、SFL212-20-32-40、SFL212A-05-21-10、SFL212A-10-21-15、SFL212A-20-21-40喷嘴挡板力反馈伺服阀,外置伺服放大器,四通,带阀芯阀套的两级伺服阀&am…...
阿里云100元预算可选的云服务器配置2核2G3M带宽
阿里云服务器100元可以买到哪些配置?如果是一年时长,轻量应用服务器2核2G3M带宽一年108元,系统盘为50GB高效云盘。以前阿里云服务器ECS卖过35元一年、69元、88元、89元和99元的都有过,但是现在整体费用上涨,入门级云服…...
Linux问题--docker启动mysql时提示3306端口被占用
问题描述: 解决方法: 1.如果需要kill掉mysqld服务可以先通过 lsof -i :3306 2. 查询到占用3306的PID,随后使用 kill -15 PID 来kill掉mysqld服务。 最后结果...
2023年中秋月饼市场趋势分析(月饼京东销售数据分析)
中秋将至,月饼作为节令食品将再次掀起消费热潮。今年月饼市场的需求如何呢,是更受欢迎还是热度有所降低,结合数据我们一起来看今年月饼市场的销售表现。 在这里,我们分别选取了2022年第31周-32周和2023年第31周-32周(…...
A Survey on Model Compression for Large Language Models
本文是LLM系列文章,关于模型压缩相关综述,针对《A Survey on Model Compression for Large Language Models》的翻译。 大模型的模型压缩综述 摘要1 引言2 方法3 度量和基准3.1 度量3.2 基准 4 挑战和未来方向5 结论 摘要 大型语言模型(LLM…...
读取/加载 properties/yml 配置文件
大家好 , 我是苏麟 , 今天带来一个简单好用的东西 . 读取/加载 properties/yml配置文件 基于PropertiesConfiguration读取配置文件 引入依赖 <!--加载yml资源--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-b…...
UG\NX二次开发 创建中心线
文章作者:里海 来源网站:王牌飞行员_里海_里海NX二次开发3000例,C\C++,Qt-CSDN博客 简介: 下面是在制图模块创建中心线的例子,用的是ufun函数。 效果: 代码: #include "me.hpp"#include <stdio.h> #include <string.h> #include <uf.h>…...
用java语言写一个网页爬虫 用于获取图片
以下是一个简单的Java程序,用于爬取网站上的图片并下载到本地文件夹: import java.io.*; import java.net.*;public class ImageSpider {public static void main(String[] args) {// 确定要爬取的网站URL和本地保存目录String url "https://www.…...
三数之和-LeetCode
给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k ,同时还满足 nums[i] nums[j] nums[k] 0 。请 你返回所有和为 0 且不重复的三元组。 注意:答案中不可以包含重复的三元组。 示例 1&a…...
ubuntu 对多CPU统一设置高性能模式
一、问题描述 之前在网上找到的CPU设置高性能模式,只能设置CPU0单个CPU,下述是对多核CPU统一设置工作模式。 二、软件安装与设置 执行下述命令sudo apt-get install indicator-cpufreq,然后重启电脑。此时,界面右上角会出现如下图标…...
浅谈 React Hooks
React Hooks 是 React 16.8 引入的一组 API,用于在函数组件中使用 state 和其他 React 特性(例如生命周期方法、context 等)。Hooks 通过简洁的函数接口,解决了状态与 UI 的高度解耦,通过函数式编程范式实现更灵活 Rea…...
Java 语言特性(面试系列2)
一、SQL 基础 1. 复杂查询 (1)连接查询(JOIN) 内连接(INNER JOIN):返回两表匹配的记录。 SELECT e.name, d.dept_name FROM employees e INNER JOIN departments d ON e.dept_id d.dept_id; 左…...
【Python】 -- 趣味代码 - 小恐龙游戏
文章目录 文章目录 00 小恐龙游戏程序设计框架代码结构和功能游戏流程总结01 小恐龙游戏程序设计02 百度网盘地址00 小恐龙游戏程序设计框架 这段代码是一个基于 Pygame 的简易跑酷游戏的完整实现,玩家控制一个角色(龙)躲避障碍物(仙人掌和乌鸦)。以下是代码的详细介绍:…...
shell脚本--常见案例
1、自动备份文件或目录 2、批量重命名文件 3、查找并删除指定名称的文件: 4、批量删除文件 5、查找并替换文件内容 6、批量创建文件 7、创建文件夹并移动文件 8、在文件夹中查找文件...
中南大学无人机智能体的全面评估!BEDI:用于评估无人机上具身智能体的综合性基准测试
作者:Mingning Guo, Mengwei Wu, Jiarun He, Shaoxian Li, Haifeng Li, Chao Tao单位:中南大学地球科学与信息物理学院论文标题:BEDI: A Comprehensive Benchmark for Evaluating Embodied Agents on UAVs论文链接:https://arxiv.…...
线程同步:确保多线程程序的安全与高效!
全文目录: 开篇语前序前言第一部分:线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分:synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分ÿ…...
iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版分享
平时用 iPhone 的时候,难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵,或者买了二手 iPhone 却被原来的 iCloud 账号锁住,这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...
学习STC51单片机31(芯片为STC89C52RCRC)OLED显示屏1
每日一言 生活的美好,总是藏在那些你咬牙坚持的日子里。 硬件:OLED 以后要用到OLED的时候找到这个文件 OLED的设备地址 SSD1306"SSD" 是品牌缩写,"1306" 是产品编号。 驱动 OLED 屏幕的 IIC 总线数据传输格式 示意图 …...
【Go语言基础【12】】指针:声明、取地址、解引用
文章目录 零、概述:指针 vs. 引用(类比其他语言)一、指针基础概念二、指针声明与初始化三、指针操作符1. &:取地址(拿到内存地址)2. *:解引用(拿到值) 四、空指针&am…...
使用Spring AI和MCP协议构建图片搜索服务
目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式(本地调用) SSE模式(远程调用) 4. 注册工具提…...
