Skip to content

Commit af916c5

Browse files
author
litongjava
committed
add EnviormentUtils
1 parent b105551 commit af916c5

File tree

4 files changed

+126
-15
lines changed

4 files changed

+126
-15
lines changed

frameworks/Java/tio-server/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,28 @@ All implementations use the same URLs.
7272
http://localhost:8080/updates?queries=5
7373

7474
## Hot to run
75+
### docker
7576
```
7677
docker build -t tio-server-benchmark -f tio-server.dockerfile .
7778
```
78-
79+
The run is to specify the mysql database
7980
```
80-
docker run --rm -p 8080:8080 tio-server-benchmark
81+
docker run --rm -p 8080:8080 tio-server-benchmark --jdbc.url=jdbc:mysql://192.168.3.9/hello_world --jdbc.user=root --jdbc.pswd=robot_123456#
8182
```
8283

83-
The run is to specify the mysql database
84-
**java**
84+
### windows
85+
8586
-windows
8687
```
8788
D:\java\jdk1.8.0_121\bin\java -jar target\tio-server-benchmark-1.0.jar --jdbc.url=jdbc:mysql://192.168.3.9/hello_world --jdbc.user=root --jdbc.pswd=robot_123456#
8889
```
89-
**docker**
90-
90+
or
9191
```
92-
docker run --rm -p 8080:8080 tio-server-benchmark --jdbc.url=jdbc:mysql://192.168.3.9/hello_world --jdbc.user=root --jdbc.pswd=robot_123456#
92+
set jdbc.url=jdbc:mysql://192.168.3.9/hello_world
93+
set jdbc.user=root
94+
set jdbc.pswd=robot_123456#
95+
D:\java\jdk1.8.0_121\bin\java -jar target\tio-server-benchmark-1.0.jar
9396
```
97+
98+
99+

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/MainApp.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
import com.litongjava.tio.http.server.handler.HttpRoutes;
1414
import com.litongjava.tio.http.server.handler.SimpleHttpDispahterHanlder;
1515
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
16+
import com.litongjava.tio.http.server.utils.EnviormentUtils;
1617
import com.litongjava.tio.server.ServerTioConfig;
17-
import com.litongjava.tio.utils.enviorment.EnviormentUtils;
1818

1919
public class MainApp {
2020

21-
public static void main(String[] args) throws IOException {
21+
public static void main(String[] args) {
22+
long start = System.currentTimeMillis();
2223
EnviormentUtils.buildCmdArgsMap(args);
2324
// add route
2425
IndexController controller = new IndexController();
@@ -51,11 +52,17 @@ public static void main(String[] args) throws IOException {
5152
serverTioConfig.setHeartbeatTimeout(0);
5253
serverTioConfig.statOn = false;
5354
// start server
54-
httpServerStarter.start();
55-
56-
new MysqlDbConfig().init();
57-
new EnjoyEngineConfig().engine();
58-
new CaffeineCacheConfig().register();
55+
try {
56+
httpServerStarter.start();
57+
new MysqlDbConfig().init();
58+
new EnjoyEngineConfig().engine();
59+
new CaffeineCacheConfig().register();
60+
long end = System.currentTimeMillis();
61+
System.out.println((end - start) + "ms");
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
System.exit(1);
65+
}
5966

6067
}
6168
}

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/config/MysqlDbConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
44
import com.jfinal.plugin.activerecord.OrderedFieldContainerFactory;
55
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
6-
import com.litongjava.tio.utils.enviorment.EnviormentUtils;
6+
import com.litongjava.tio.http.server.utils.EnviormentUtils;
77

88
public class MysqlDbConfig {
99

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.litongjava.tio.http.server.utils;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class EnviormentUtils {
7+
private static String[] args;
8+
private static Map<String, String> cmdArgsMap = new HashMap<>();
9+
10+
public static String[] getArgs() {
11+
return args;
12+
}
13+
14+
public Map<String, String> getCmdArgsMap() {
15+
return cmdArgsMap;
16+
}
17+
18+
public static Map<String, String> buildCmdArgsMap(String[] args) {
19+
EnviormentUtils.args = args;
20+
Map<String, String> result = new HashMap<>();
21+
for (String arg : args) {
22+
if (arg.startsWith("--")) {
23+
String[] parts = arg.substring(2).split("=", 2);
24+
if (parts.length == 2) {
25+
result.put(parts[0], parts[1]);
26+
}
27+
}
28+
}
29+
cmdArgsMap = result;
30+
return result;
31+
}
32+
33+
public static String getStr(String key) {
34+
// comamdn line
35+
String value = cmdArgsMap.get(key);
36+
// enviorment
37+
if (value == null) {
38+
value = System.getProperty(key);
39+
}
40+
41+
// config file
42+
if (value == null) {
43+
value = System.getenv(key);
44+
45+
}
46+
47+
return value;
48+
}
49+
50+
public static String get(String key) {
51+
return getStr(key);
52+
}
53+
54+
public static Integer getInt(String key) {
55+
String value = getStr(key);
56+
if (value != null) {
57+
return Integer.valueOf(value);
58+
} else {
59+
return null;
60+
}
61+
62+
}
63+
64+
public static int getInt(String key, int defaultValue) {
65+
String value = get(key);
66+
if (value != null) {
67+
return Integer.parseInt(value);
68+
} else {
69+
return defaultValue;
70+
}
71+
}
72+
73+
public static String get(String key, String defaultValue) {
74+
String value = get(key);
75+
if (value != null) {
76+
return value;
77+
} else {
78+
return defaultValue;
79+
}
80+
}
81+
82+
public static boolean getBoolean(String key) {
83+
return Boolean.parseBoolean(get(key));
84+
}
85+
86+
public static boolean getBoolean(String key, boolean defaultValue) {
87+
String value = get(key);
88+
if (value != null) {
89+
return Boolean.parseBoolean(value);
90+
} else {
91+
return defaultValue;
92+
}
93+
}
94+
// public static void main(String[] args) {
95+
// String string = EnviormentUtils.get("jdbc.user");
96+
// System.out.println(string);
97+
// }
98+
}

0 commit comments

Comments
 (0)