HttpServlet增加postStart方法

This commit is contained in:
Redkale
2019-09-04 10:00:29 +08:00
parent 2e27814809
commit 264dfbef2e
6 changed files with 34 additions and 0 deletions

View File

@@ -283,11 +283,15 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
serverChannel.bind(address, backlog);
serverChannel.accept(this);
final String threadName = "[" + Thread.currentThread().getName() + "] ";
postStart();
logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address
+ ", threads: " + threads + ", maxbody: " + formatLenth(context.maxbody) + ", bufferCapacity: " + formatLenth(bufferCapacity) + ", bufferPoolSize: " + bufferPoolSize + ", responsePoolSize: " + responsePoolSize
+ ", started in " + (System.currentTimeMillis() - context.getServerStartTime()) + " ms");
}
protected void postStart() {
}
public void changeAddress(final InetSocketAddress addr) throws IOException {
long s = System.currentTimeMillis();
Objects.requireNonNull(addr);

View File

@@ -23,11 +23,13 @@ public abstract class Servlet<C extends Context, R extends Request<C>, P extends
AnyValue _conf; //当前Servlet的配置
//Server执行start时运行此方法
public void init(C context, AnyValue config) {
}
public abstract void execute(R request, P response) throws IOException;
//Server执行shutdown后运行此方法
public void destroy(C context, AnyValue config) {
}

View File

@@ -6,6 +6,7 @@
package org.redkale.net.http;
import org.redkale.net.Filter;
import org.redkale.util.AnyValue;
/**
* HTTP 过滤器 <br>
@@ -17,4 +18,7 @@ import org.redkale.net.Filter;
*/
public abstract class HttpFilter extends Filter<HttpContext, HttpRequest, HttpResponse> {
//Server执行start后运行此方法
public void postStart(HttpContext context, AnyValue config) {
}
}

View File

@@ -402,6 +402,21 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
return this.resourceHttpServlet;
}
public void postStart(HttpContext context, AnyValue config) {
List filters = getFilters();
synchronized (filters) {
if (!filters.isEmpty()) {
for (Object filter : filters) {
((HttpFilter) filter).postStart(context, config);
}
}
}
this.resourceHttpServlet.postStart(context, config);
getServlets().forEach(s -> {
s.postStart(context, getServletConf(s));
});
}
@Override
public void destroy(HttpContext context, AnyValue config) {
super.destroy(context, config); //必须要执行

View File

@@ -56,6 +56,11 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
super.init(config);
}
@Override
protected void postStart() {
((HttpPrepareServlet) this.prepare).postStart(this.context, config);
}
@Override
public void destroy(final AnyValue config) throws Exception {
super.destroy(config);

View File

@@ -105,6 +105,10 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
void postDestroy(HttpContext context, AnyValue config) {
}
//Server执行start后运行此方法
public void postStart(HttpContext context, AnyValue config) {
}
/**
* <p>
* 预执行方法在execute方法之前运行设置当前用户信息或者加入常规统计和基础检测例如 : <br>