Skip to content

Commit fa7eb5c

Browse files
committed
Merge branch 'main' of https://github.com/JavaCourse00/JavaCourseCodes into JavaCourse00-main
# Conflicts: # 02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java # 02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundInitializer.java
2 parents 373ce28 + 672a304 commit fa7eb5c

File tree

456 files changed

+25096
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

456 files changed

+25096
-83
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@
2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
2424
*.iml
25-
*.idea/
25+
*.idea/
26+
27+
classes/
28+
target/
29+
build/
30+
31+
.DS_Store

01jvm/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 第1周作业
2+
3+
4+
参见 我的教室 -> 本周作业
5+
6+
## 作业内容
7+
8+
9+
> Week01 作业题目(周四):
10+
11+
1.(选做)自己写一个简单的 Hello.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。
12+
13+
2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。
14+
15+
3.(必做)画一张图,展示 Xmx、Xms、Xmn、Meta、DirectMemory、Xss 这些内存参数的关系。
16+
17+
4.(选做)检查一下自己维护的业务系统的 JVM 参数配置,用 jstat 和 jstack、jmap 查看一下详情,并且自己独立分析一下大概情况,思考有没有不合理的地方,如何改进。
18+
19+
注意:如果没有线上系统,可以自己 run 一个 web/java 项目。
20+
21+
> Week01 作业题目(周六):
22+
23+
1.(选做)本机使用 G1 GC 启动一个程序,仿照课上案例分析一下 JVM 情况。
24+
25+
26+
## 操作步骤
27+
28+
29+
### 作业2
30+
31+
1. 打开 Spring 官网: https://spring.io/
32+
2. 找到 Projects --> Spring Initializr: https://start.spring.io/
33+
3. 填写项目信息, 生成 maven 项目; 下载并解压。
34+
4. Idea或者Eclipse从已有的Source导入Maven项目。
35+
5. 增加课程资源 Hello.xlass 文件到 src/main/resources 目录。
36+
6. 编写代码,实现 findClass 方法,解码方法
37+
7. 编写main方法,调用 loadClass 方法;
38+
8. 创建实例,以及调用方法
39+
9. 执行.
40+
41+
具体的参见: [https://github.com/renfufei/JAVA-000/blob/main/Week_01/homework01/src/main/java/com/renfufei/homework01/XlassLoader.java](XlassLoader.java)

01jvm/TestAddUrl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.kimmking.kmq;
2+
3+
import lombok.SneakyThrows;
4+
5+
import java.io.File;
6+
import java.lang.reflect.Method;
7+
import java.net.URL;
8+
import java.net.URLClassLoader;
9+
10+
public class TestAddUrl {
11+
12+
@SneakyThrows
13+
public static void main(String[] args) {
14+
URLClassLoader classLoader = (URLClassLoader) TestAddUrl.class.getClassLoader();
15+
String dir = "/Users/kimmking/Downloads/Hello";
16+
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
17+
method.setAccessible(true);
18+
method.invoke(classLoader, new File(dir).toURL());
19+
20+
Class klass = Class.forName("Hello",true, classLoader);
21+
Object obj = klass.newInstance();
22+
Method hello = klass.getDeclaredMethod("hello");
23+
hello.invoke(obj);
24+
}
25+
26+
}

02nio/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 第2周作业
2+
3+
4+
## 作业内容
5+
6+
> Week02 作业题目(周四):
7+
8+
1.使用 GCLogAnalysis.java 自己演练一遍串行 / 并行 /CMS/G1 的案例。
9+
2.使用压测工具(wrk 或 sb),演练 gateway-server-0.0.1-SNAPSHOT.jar 示例。
10+
3.(选做) 如果自己本地有可以运行的项目,可以按照 2 的方式进行演练。
11+
4.(必做) 根据上述自己对于 1 和 2 的演示,写一段对于不同 GC 的总结,提交到 Github。
12+
13+
> Week02 作业题目(周六):
14+
15+
1.(选做)运行课上的例子,以及 Netty 的例子,分析相关现象。
16+
17+
2.(必做)写一段代码,使用 HttpClient 或 OkHttp 访问 http://localhost:8801 ,代码提交到 Github。
18+
19+
20+
## 操作步骤
21+
22+
23+
### 第二周-周六-作业2
24+
25+
1. 打开 Spring 官网: https://spring.io/
26+
2. 找到 Projects --> Spring Initializr: https://start.spring.io/
27+
3. 填写项目信息, 生成 maven 项目; 下载并解压。
28+
4. Idea或者Eclipse从已有的Source导入Maven项目。
29+
5. 搜索依赖, 推荐 mvnrepository: https://mvnrepository.com/
30+
6. 搜索 OkHttp 或者 HttpClient,然后在 pom.xml 之中增加对应的依赖。
31+
7. 使用OkHttp
32+
- 7.1 查找OkHttp官网: https://square.github.io/okhttp/
33+
- 7.2 参照官方示例编写代码: [OkHttpUtils.java](https://github.com/renfufei/JAVA-000/blob/main/Week_02/homework02/src/main/java/com/renfufei/homework02/OkHttpUtils.java)
34+
8. 使用HttpClient
35+
- 8.1 查找官网: http://hc.apache.org/
36+
- 8.2 参照官方示例编写代码: [HttpClientHelper.java](https://github.com/renfufei/JAVA-000/blob/main/Week_02/homework02/src/main/java/com/renfufei/homework02/HttpClientHelper.java)
37+
- 8.3 执行如果报错, 根据提示,增加 commons-logging 或者其他日志依赖。
38+
9. 执行与测试.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java0.nio01</groupId>
5+
<artifactId>nio01</artifactId>
6+
<version>1.0</version>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<configuration>
12+
<source>${maven.compile.source}</source>
13+
<target>${maven.compile.target}</target>
14+
</configuration>
15+
</plugin>
16+
<plugin>
17+
<artifactId>maven-shade-plugin</artifactId>
18+
<version>3.2.0</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>shade</goal>
23+
</goals>
24+
<configuration>
25+
<transformers>
26+
<transformer>
27+
<manifestEntries>
28+
<Main-Class>${app.main.class}</Main-Class>
29+
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
30+
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
31+
</manifestEntries>
32+
</transformer>
33+
</transformers>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
<properties>
41+
<app.main.class>Main</app.main.class>
42+
<maven.compile.source>8</maven.compile.source>
43+
<maven.compile.target>8</maven.compile.target>
44+
</properties>
45+
</project>

02nio/nio01/pom.xml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,59 @@
77
<groupId>java0.nio01</groupId>
88
<artifactId>nio01</artifactId>
99
<version>1.0</version>
10+
11+
<properties>
12+
<app.main.class>Main</app.main.class>
13+
<maven.compile.source>8</maven.compile.source>
14+
<maven.compile.target>8</maven.compile.target>
15+
</properties>
16+
1017
<build>
1118
<plugins>
1219
<plugin>
1320
<groupId>org.apache.maven.plugins</groupId>
1421
<artifactId>maven-compiler-plugin</artifactId>
1522
<configuration>
16-
<source>8</source>
17-
<target>8</target>
23+
<source>${maven.compile.source}</source>
24+
<target>${maven.compile.target}</target>
1825
</configuration>
1926
</plugin>
27+
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-shade-plugin</artifactId>
31+
<version>3.2.0</version>
32+
<executions>
33+
<execution>
34+
<goals>
35+
<goal>shade</goal>
36+
</goals>
37+
<configuration>
38+
<transformers>
39+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
40+
<manifestEntries>
41+
<Main-Class>${app.main.class}</Main-Class>
42+
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
43+
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
44+
</manifestEntries>
45+
</transformer>
46+
</transformers>
47+
</configuration>
48+
</execution>
49+
</executions>
50+
</plugin>
2051
</plugins>
2152
</build>
2253

2354

55+
56+
<dependencies>
57+
<dependency>
58+
<groupId>io.netty</groupId>
59+
<artifactId>netty-all</artifactId>
60+
<version>4.1.51.Final</version>
61+
</dependency>
62+
</dependencies>
63+
64+
2465
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java0.nio01.HttpServer01;
2+
import java0.nio01.HttpServer02;
3+
import java0.nio01.HttpServer03;
4+
import java0.nio01.netty.NettyHttpServer;
5+
6+
import java.lang.reflect.Method;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
Map<String, Class> map = new HashMap<>();
14+
map.put("1", HttpServer01.class);
15+
map.put("2", HttpServer02.class);
16+
map.put("3", HttpServer03.class);
17+
map.put("8", NettyHttpServer.class);
18+
19+
String id = (null == args || args.length == 0) ? "1" : args[0];
20+
Class clazz = map.get(id);
21+
if( null == clazz ) {
22+
System.out.println("No class for id: " + id);
23+
}
24+
25+
try {
26+
Method method = clazz.getDeclaredMethod("main", new Class[]{String[].class});
27+
method.invoke(null, new Object[]{new String[]{}});
28+
} catch (Exception e) {
29+
e.printStackTrace();
30+
}
31+
32+
}
33+
34+
}

02nio/nio01/src/main/java/java0/nio01/HttpServer01.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.ServerSocket;
66
import java.net.Socket;
77

8+
// 单线程的socket程序
89
public class HttpServer01 {
910
public static void main(String[] args) throws IOException{
1011
ServerSocket serverSocket = new ServerSocket(8801);
@@ -20,17 +21,17 @@ public static void main(String[] args) throws IOException{
2021

2122
private static void service(Socket socket) {
2223
try {
23-
Thread.sleep(20);
24+
// Thread.sleep(5);
2425
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2526
printWriter.println("HTTP/1.1 200 OK");
2627
printWriter.println("Content-Type:text/html;charset=utf-8");
27-
String body = "hello,nio";
28+
String body = "hello,nio1";
2829
printWriter.println("Content-Length:" + body.getBytes().length);
2930
printWriter.println();
3031
printWriter.write(body);
3132
printWriter.close();
3233
socket.close();
33-
} catch (IOException | InterruptedException e) {
34+
} catch (IOException e) { // | InterruptedException e) {
3435
e.printStackTrace();
3536
}
3637
}

02nio/nio01/src/main/java/java0/nio01/HttpServer02.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.ServerSocket;
66
import java.net.Socket;
77

8+
// 每个请求一个线程
89
public class HttpServer02 {
910
public static void main(String[] args) throws IOException{
1011
ServerSocket serverSocket = new ServerSocket(8802);
@@ -22,17 +23,17 @@ public static void main(String[] args) throws IOException{
2223

2324
private static void service(Socket socket) {
2425
try {
25-
Thread.sleep(20);
26+
// Thread.sleep(5);
2627
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2728
printWriter.println("HTTP/1.1 200 OK");
2829
printWriter.println("Content-Type:text/html;charset=utf-8");
29-
String body = "hello,nio";
30+
String body = "hello,nio2";
3031
printWriter.println("Content-Length:" + body.getBytes().length);
3132
printWriter.println();
3233
printWriter.write(body);
3334
printWriter.close();
3435
socket.close();
35-
} catch (IOException | InterruptedException e) {
36+
} catch (IOException e) { // | InterruptedException e) {
3637
e.printStackTrace();
3738
}
3839
}

02nio/nio01/src/main/java/java0/nio01/HttpServer03.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import java.util.concurrent.ExecutorService;
88
import java.util.concurrent.Executors;
99

10+
// 创建了一个固定大小的线程池处理请求
1011
public class HttpServer03 {
1112
public static void main(String[] args) throws IOException{
12-
ExecutorService executorService = Executors.newFixedThreadPool(40);
13+
14+
ExecutorService executorService = Executors.newFixedThreadPool(
15+
Runtime.getRuntime().availableProcessors() + 2);
1316
final ServerSocket serverSocket = new ServerSocket(8803);
1417
while (true) {
1518
try {
@@ -23,7 +26,7 @@ public static void main(String[] args) throws IOException{
2326

2427
private static void service(Socket socket) {
2528
try {
26-
Thread.sleep(20);
29+
// Thread.sleep(5);
2730
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2831
printWriter.println("HTTP/1.1 200 OK");
2932
printWriter.println("Content-Type:text/html;charset=utf-8");
@@ -33,7 +36,7 @@ private static void service(Socket socket) {
3336
printWriter.write(body);
3437
printWriter.close();
3538
socket.close();
36-
} catch (IOException | InterruptedException e) {
39+
} catch (IOException e) { // | InterruptedException e) {
3740
e.printStackTrace();
3841
}
3942
}

0 commit comments

Comments
 (0)