Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 64 additions & 151 deletions frameworks/Java/tio-http-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>tio-http-server</artifactId>
<version>3.7.3.v20240919-RELEASE</version>
<version>3.7.3.v20250301-RELEASE</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>java-db</artifactId>
<version>1.2.6</version>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -31,16 +31,6 @@
</dependency>


<!-- https://mvnrepository.com/artifact/com.jfinal/activerecord -->
<!--
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>activerecord</artifactId>
<version>5.1.6</version>
</dependency>
-->


<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
Expand All @@ -66,144 +56,67 @@
</dependency>

</dependencies>
<profiles>
<!-- 开发环境 -->
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.13</version>
</dependency>
</dependencies>
</profile>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
<pluginRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>

<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<debug>false</debug>
</configuration>
</plugin>

<!-- 生产环境 -->
<profile>
<id>production</id>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.4</version>
<configuration>
<mainClass>${main.class}</mainClass>
<excludeGroupIds>org.projectlombok</excludeGroupIds>
</configuration>
<!-- 设置执行目标 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- assembly -->
<profile>
<id>assembly</id>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>native</id>
<dependencies>
<!-- GraalVM 环境使用 jdk log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.31</version>
</dependency>
<!-- GraalVM -->
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>21.2.0</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<imageName>${project.artifactId}</imageName>
<mainClass>${main.class}</mainClass>
<buildArgs>
-H:+RemoveSaturatedTypeFlows
--allow-incomplete-classpath
--no-fallback
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.litongjava.tio.http.server.controller.DbController;
import com.litongjava.tio.http.server.controller.IndexController;
import com.litongjava.tio.http.server.handler.DefaultHttpRequestDispatcher;
import com.litongjava.tio.http.server.router.DefaultHttpReqeustRouter;
import com.litongjava.tio.http.server.router.DefaultHttpRequestRouter;
import com.litongjava.tio.http.server.router.HttpRequestRouter;
import com.litongjava.tio.server.ServerTioConfig;
import com.litongjava.tio.utils.environment.EnvUtils;
Expand All @@ -18,11 +18,12 @@ public class MainApp {

public static void main(String[] args) {
long start = System.currentTimeMillis();
EnvUtils.buildCmdArgsMap(args);
EnvUtils.load();
// add route
IndexController controller = new IndexController();

HttpRequestRouter simpleHttpRoutes = new DefaultHttpReqeustRouter();
HttpRequestRouter simpleHttpRoutes = new DefaultHttpRequestRouter();
simpleHttpRoutes.add("/", controller::index);
simpleHttpRoutes.add("/plaintext", controller::plaintext);
simpleHttpRoutes.add("/json", controller::json);
Expand All @@ -49,9 +50,16 @@ public static void main(String[] args) {
// close Heartbeat
serverTioConfig.setHeartbeatTimeout(0);
serverTioConfig.statOn = false;
boolean db = EnvUtils.getBoolean("db", true);
if (db) {
try {
new MysqlDbConfig().init();
} catch (Exception e) {
e.printStackTrace();
}
}
// start server
try {
new MysqlDbConfig().init();
new EnjoyEngineConfig().engine();
new EhCachePluginConfig().ehCachePlugin();
httpServerStarter.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.alibaba.fastjson2.JSON;
import com.litongjava.db.activerecord.Db;
import com.litongjava.db.activerecord.Record;
import com.litongjava.db.activerecord.Row;
import com.litongjava.tio.http.common.HeaderName;
import com.litongjava.tio.http.common.HeaderValue;
import com.litongjava.tio.http.common.HttpRequest;
Expand All @@ -24,7 +24,7 @@ public HttpResponse cachedQuery(HttpRequest request) {
.limit(RandomUtils.parseQueryCount(queries)) // 限制查询数量
.mapToObj(id -> findByIdWithCache("world", id)) // 使用 mapToObj 将 int 映射为对象
.filter(Objects::nonNull) // 过滤掉 null 值
.map(Record::toMap) // 将每个 Record 对象转换为 Map
.map(Row::toMap) // 将每个 Record 对象转换为 Map
.collect(Collectors.toList()); // 收集到 List

HttpResponse httpResponse = new HttpResponse(request);
Expand All @@ -34,7 +34,7 @@ public HttpResponse cachedQuery(HttpRequest request) {

}

private Record findByIdWithCache(String tableName, int id) {
private Row findByIdWithCache(String tableName, int id) {
String sql = "SELECT id, randomNumber FROM world WHERE id = ?";
return Db.findFirstByCache(tableName, id, sql, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.litongjava.db.activerecord.Db;
import com.litongjava.db.activerecord.Record;
import com.litongjava.ehcache.EhCache;
import com.litongjava.db.activerecord.Row;
import com.litongjava.ehcache.EhCacheKit;
import com.litongjava.tio.http.common.HeaderName;
import com.litongjava.tio.http.common.HeaderValue;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.http.server.model.Fortune;
import com.litongjava.tio.http.server.util.Resps;
import com.litongjava.tio.http.server.utils.BeanConverterUtils;
import com.litongjava.tio.http.server.utils.RandomUtils;

public class DbController {
Expand All @@ -37,7 +36,7 @@ public HttpResponse db(HttpRequest request) {
// int id = 11;
// String sql="SELECT id, randomNumber FROM world WHERE id = ?";

Record recored = Db.findById("world", id);
Row recored = Db.findById("world", id);
if (recored != null) {
httpResponse.setBody(JSON.toJSONBytes(recored.toMap()));
} else {
Expand All @@ -57,7 +56,7 @@ public HttpResponse queries(HttpRequest request) {
.limit(RandomUtils.parseQueryCount(queries)) // 限制查询数量
.mapToObj(id -> Db.findById("world", id)) // 使用 mapToObj 将 int 映射为对象
.filter(Objects::nonNull) // 过滤掉 null 值
.map(Record::toMap) // 将每个 Record 对象转换为 Map
.map(Row::toMap) // 将每个 Record 对象转换为 Map
.collect(Collectors.toList()); // 收集到 List

HttpResponse httpResponse = new HttpResponse(request);
Expand All @@ -66,11 +65,11 @@ public HttpResponse queries(HttpRequest request) {
return httpResponse;
}

//@GetMapping("/updates")
//@GetMapping("/updates")
public HttpResponse updates(HttpRequest request) {
String queries = request.getParam("queries");

EhCache.removeAll("world");
EhCacheKit.removeAll("world");

List<Map<String, Object>> updatedRecords = RandomUtils.randomWorldNumbers()// random numbers
// limit
Expand All @@ -90,7 +89,7 @@ public HttpResponse updates(HttpRequest request) {
return record;
})
// tomap
.map(Record::toMap)
.map(Row::toMap)
// to List
.collect(Collectors.toList());

Expand All @@ -101,11 +100,11 @@ public HttpResponse updates(HttpRequest request) {
}

public HttpResponse fortunes(HttpRequest request) throws IllegalAccessException, InstantiationException {
List<Record> records = Db.find("SELECT * FROM fortune");
List<Row> records = Db.find("SELECT * FROM fortune");

List<Fortune> fortunes = new ArrayList<>(records.size());
for (Record record : records) {
fortunes.add(BeanConverterUtils.toBean(record.toMap(), Fortune.class));
for (Row record : records) {
fortunes.add(record.toBean(Fortune.class));
}
// 添加额外的 Fortune
fortunes.add(new Fortune(0L, "Additional fortune added at request time."));
Expand Down
Loading
Loading