优化HttpServer
This commit is contained in:
@@ -1 +1,40 @@
|
|||||||
文档完善中……
|
# 使用native-image
|
||||||
|
   Redkale支持GraalVM的native-image, 由于Redkale使用了大量的asm动态生成代码,而native-image不支持动态字节码,因此需要使用```redkale-maven-plugin```执行预编译,提前生成动态字节码进行打包。
|
||||||
|
|
||||||
|
## 安装GraalVM
|
||||||
|
```
|
||||||
|
下载地址: https://www.graalvm.org/downloads/
|
||||||
|
```
|
||||||
|
|
||||||
|
## 安装native-image
|
||||||
|
```
|
||||||
|
install native-image
|
||||||
|
```
|
||||||
|
|
||||||
|
## 配置pom
|
||||||
|
```xml
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.redkale.maven.plugins</groupId>
|
||||||
|
<artifactId>redkale-maven-plugin</artifactId>
|
||||||
|
<version>1.2.0</version>
|
||||||
|
<configuration>
|
||||||
|
<nativeimageArgs>
|
||||||
|
<arg>--no-fallback</arg>
|
||||||
|
</nativeimageArgs>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>redkale-compile</id>
|
||||||
|
<phase>process-classes</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
```
|
||||||
|
|
||||||
|
## native-image编译
|
||||||
|
```
|
||||||
|
native-image -H:+ReportExceptionStackTraces --report-unsupported-elements-at-runtime -jar xxx.jar
|
||||||
|
```
|
||||||
@@ -12,6 +12,7 @@ import java.util.Set;
|
|||||||
import java.util.concurrent.atomic.LongAdder;
|
import java.util.concurrent.atomic.LongAdder;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -81,7 +82,7 @@ class AsyncNioTcpProtocolServer extends ProtocolServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(Application application, Server server) throws IOException {
|
public void accept(@Nullable Application application, Server server) throws IOException {
|
||||||
this.serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);
|
this.serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);
|
||||||
|
|
||||||
LongAdder createBufferCounter = new LongAdder();
|
LongAdder createBufferCounter = new LongAdder();
|
||||||
@@ -103,7 +104,7 @@ class AsyncNioTcpProtocolServer extends ProtocolServer {
|
|||||||
ObjectPool<Response> pool = localResponsePool.get();
|
ObjectPool<Response> pool = localResponsePool.get();
|
||||||
return pool == null ? safeResponsePool.get() : pool.get();
|
return pool == null ? safeResponsePool.get() : pool.get();
|
||||||
};
|
};
|
||||||
this.responseConsumer = (v) -> {
|
this.responseConsumer = v -> {
|
||||||
WorkThread thread = v.channel != null ? v.channel.getWriteIOThread() : v.thread;
|
WorkThread thread = v.channel != null ? v.channel.getWriteIOThread() : v.thread;
|
||||||
if (thread != null && !thread.inCurrThread()) {
|
if (thread != null && !thread.inCurrThread()) {
|
||||||
thread.execute(() -> {
|
thread.execute(() -> {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import java.util.concurrent.atomic.LongAdder;
|
|||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -84,7 +85,7 @@ class AsyncNioUdpProtocolServer extends ProtocolServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(Application application, Server server) throws IOException {
|
public void accept(@Nullable Application application, Server server) throws IOException {
|
||||||
|
|
||||||
LongAdder createBufferCounter = new LongAdder();
|
LongAdder createBufferCounter = new LongAdder();
|
||||||
LongAdder cycleBufferCounter = new LongAdder();
|
LongAdder cycleBufferCounter = new LongAdder();
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public abstract class ProtocolServer {
|
|||||||
//最大连接数,小于1表示无限制
|
//最大连接数,小于1表示无限制
|
||||||
protected int maxConns;
|
protected int maxConns;
|
||||||
|
|
||||||
@Resource
|
@Resource(required = false) //独立创建HttpServer时没有Application
|
||||||
protected Application application;
|
protected Application application;
|
||||||
|
|
||||||
public abstract void open(AnyValue config) throws IOException;
|
public abstract void open(AnyValue config) throws IOException;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.net.http;
|
package org.redkale.net.http;
|
||||||
|
|
||||||
import org.redkale.mq.spi.MessageAgent;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.net.HttpCookie;
|
import java.net.HttpCookie;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
@@ -15,11 +15,12 @@ import java.util.*;
|
|||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.LongAdder;
|
import java.util.concurrent.atomic.LongAdder;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import org.redkale.mq.*;
|
import org.redkale.mq.spi.MessageAgent;
|
||||||
import org.redkale.net.Server;
|
import org.redkale.net.Server;
|
||||||
import org.redkale.net.http.HttpContext.HttpContextConfig;
|
import org.redkale.net.http.HttpContext.HttpContextConfig;
|
||||||
import org.redkale.net.http.HttpResponse.HttpResponseConfig;
|
import org.redkale.net.http.HttpResponse.HttpResponseConfig;
|
||||||
@@ -188,6 +189,37 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加HttpServlet
|
||||||
|
*
|
||||||
|
* @param mapping 匹配规则
|
||||||
|
* @param servlet HttpServlet
|
||||||
|
*
|
||||||
|
* @return HttpServer
|
||||||
|
*/
|
||||||
|
public HttpServer addHttpServlet(String mapping, HttpServlet servlet) {
|
||||||
|
this.dispatcher.addServlet(servlet, null, null, mapping);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加HttpServlet
|
||||||
|
*
|
||||||
|
* @param mapping 匹配规则
|
||||||
|
* @param consumer HttpServlet
|
||||||
|
*
|
||||||
|
* @return HttpServer
|
||||||
|
*/
|
||||||
|
public HttpServer addHttpServlet(String mapping, BiConsumer<HttpRequest, HttpResponse> consumer) {
|
||||||
|
this.dispatcher.addServlet(new HttpServlet() {
|
||||||
|
@Override
|
||||||
|
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
||||||
|
consumer.accept(request, response);
|
||||||
|
}
|
||||||
|
}, null, null, mapping);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加HttpServlet
|
* 添加HttpServlet
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import java.time.Duration;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -122,6 +123,14 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onServersPreStart() {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onServersPostStart() {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void schedule(Object service) {
|
public void schedule(Object service) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@@ -336,7 +345,7 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract class ScheduledTask implements Runnable {
|
protected abstract class ScheduledTask {
|
||||||
|
|
||||||
protected final WeakReference ref;
|
protected final WeakReference ref;
|
||||||
|
|
||||||
@@ -350,6 +359,11 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
|
|
||||||
protected final ScheduleEvent event;
|
protected final ScheduleEvent event;
|
||||||
|
|
||||||
|
protected final Map<String, Object> eventMap;
|
||||||
|
|
||||||
|
//任务是否正运行中
|
||||||
|
protected final AtomicBoolean doing = new AtomicBoolean();
|
||||||
|
|
||||||
protected ScheduledTask(WeakReference ref, String name, Method method) {
|
protected ScheduledTask(WeakReference ref, String name, Method method) {
|
||||||
Objects.requireNonNull(ref);
|
Objects.requireNonNull(ref);
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
@@ -357,7 +371,8 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
this.ref = ref;
|
this.ref = ref;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.event = method.getParameterCount() == 0 ? null : new ScheduleEvent();
|
this.eventMap = method.getParameterCount() == 0 ? null : new HashMap<>();
|
||||||
|
this.event = eventMap == null ? null : new ScheduleEvent(eventMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
@@ -374,6 +389,14 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
this.started.set(false);
|
this.started.set(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean doing() {
|
||||||
|
return doing.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> eventMap() {
|
||||||
|
return eventMap;
|
||||||
|
}
|
||||||
|
|
||||||
public Method method() {
|
public Method method() {
|
||||||
return method;
|
return method;
|
||||||
}
|
}
|
||||||
@@ -383,7 +406,7 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class FixedTask extends ScheduledTask {
|
protected class FixedTask extends ScheduledTask implements Runnable {
|
||||||
|
|
||||||
private final Function<ScheduleEvent, Object> delegate;
|
private final Function<ScheduleEvent, Object> delegate;
|
||||||
|
|
||||||
@@ -406,10 +429,13 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
doing.set(true);
|
||||||
try {
|
try {
|
||||||
delegate.apply(event);
|
delegate.apply(event);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.log(Level.SEVERE, "schedule task error", t);
|
logger.log(Level.SEVERE, "schedule task error", t);
|
||||||
|
} finally {
|
||||||
|
doing.set(false);
|
||||||
}
|
}
|
||||||
if (ref.get() == null) {
|
if (ref.get() == null) {
|
||||||
stop();
|
stop();
|
||||||
@@ -430,7 +456,7 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class CronTask extends ScheduledTask {
|
protected class CronTask extends ScheduledTask implements Runnable {
|
||||||
|
|
||||||
private final Function<ScheduleEvent, Object> delegate;
|
private final Function<ScheduleEvent, Object> delegate;
|
||||||
|
|
||||||
@@ -448,10 +474,13 @@ public class ScheduleManagerService implements ScheduleManager, Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
doing.set(true);
|
||||||
try {
|
try {
|
||||||
delegate.apply(event);
|
delegate.apply(event);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.log(Level.SEVERE, "schedule task error", t);
|
logger.log(Level.SEVERE, "schedule task error", t);
|
||||||
|
} finally {
|
||||||
|
doing.set(false);
|
||||||
}
|
}
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,26 @@ public class ScheduleModuleEngine extends ModuleEngine {
|
|||||||
this.scheduleManager.unschedule(service);
|
this.scheduleManager.unschedule(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务全部启动前被调用
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onServersPreStart() {
|
||||||
|
if (this.scheduleManager instanceof ScheduleManagerService) {
|
||||||
|
((ScheduleManagerService) this.scheduleManager).onServersPreStart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务全部启动后被调用
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onServersPostStart() {
|
||||||
|
if (this.scheduleManager instanceof ScheduleManagerService) {
|
||||||
|
((ScheduleManagerService) this.scheduleManager).onServersPostStart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 进入Application.shutdown方法被调用
|
* 进入Application.shutdown方法被调用
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user