PrepareServlet.addServlet方法改为线程安全
This commit is contained in:
@@ -15,7 +15,7 @@ import org.redkale.util.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根Servlet, 一个Server只能存在一个根Servlet
|
* 根Servlet, 一个Server只能存在一个根Servlet
|
||||||
*
|
*
|
||||||
* 用于分发Request请求
|
* 用于分发Request请求
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
@@ -34,9 +34,33 @@ public abstract class PrepareServlet<K extends Serializable, C extends Context,
|
|||||||
|
|
||||||
protected final AtomicLong illRequestCounter = new AtomicLong(); //错误请求次数
|
protected final AtomicLong illRequestCounter = new AtomicLong(); //错误请求次数
|
||||||
|
|
||||||
protected final Set<S> servlets = new HashSet<>();
|
private final Object lock1 = new Object();
|
||||||
|
|
||||||
protected final Map<K, S> mappings = new HashMap<>();
|
private Set<S> servlets = new HashSet<>();
|
||||||
|
|
||||||
|
private final Object lock2 = new Object();
|
||||||
|
|
||||||
|
private Map<K, S> mappings = new HashMap<>();
|
||||||
|
|
||||||
|
protected void putServlet(S servlet) {
|
||||||
|
synchronized (lock1) {
|
||||||
|
Set<S> newservlets = new HashSet<>(servlets);
|
||||||
|
newservlets.add(servlet);
|
||||||
|
this.servlets = newservlets;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void putMapping(K key, S value) {
|
||||||
|
synchronized (lock2) {
|
||||||
|
Map<K, S> newmappings = new HashMap<>(mappings);
|
||||||
|
newmappings.put(key, value);
|
||||||
|
this.mappings = newmappings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected S mappingServlet(K key) {
|
||||||
|
return mappings.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void addServlet(S servlet, Object attachment, AnyValue conf, K... mappings);
|
public abstract void addServlet(S servlet, Object attachment, AnyValue conf, K... mappings);
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(HttpContext context, AnyValue config) {
|
public void init(HttpContext context, AnyValue config) {
|
||||||
this.servlets.forEach(s -> {
|
Collection<HttpServlet> servlets = getServlets();
|
||||||
|
servlets.forEach(s -> {
|
||||||
if (s instanceof WebSocketServlet) {
|
if (s instanceof WebSocketServlet) {
|
||||||
((WebSocketServlet) s).preInit(context, getServletConf(s));
|
((WebSocketServlet) s).preInit(context, getServletConf(s));
|
||||||
} else if (s instanceof HttpBaseServlet) {
|
} else if (s instanceof HttpBaseServlet) {
|
||||||
@@ -47,7 +48,7 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
});
|
});
|
||||||
final WatchFactory watch = context.getWatchFactory();
|
final WatchFactory watch = context.getWatchFactory();
|
||||||
if (watch != null) {
|
if (watch != null) {
|
||||||
this.servlets.forEach(s -> {
|
servlets.forEach(s -> {
|
||||||
watch.inject(s);
|
watch.inject(s);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
||||||
try {
|
try {
|
||||||
final String uri = request.getRequestURI();
|
final String uri = request.getRequestURI();
|
||||||
Servlet<HttpContext, HttpRequest, HttpResponse> servlet = this.mappings.isEmpty() ? null : this.mappings.get(uri);
|
Servlet<HttpContext, HttpRequest, HttpResponse> servlet = mappingServlet(uri);
|
||||||
if (servlet == null && this.regArray != null) {
|
if (servlet == null && this.regArray != null) {
|
||||||
for (SimpleEntry<Predicate<String>, HttpServlet> en : regArray) {
|
for (SimpleEntry<Predicate<String>, HttpServlet> en : regArray) {
|
||||||
if (en.getKey().test(uri)) {
|
if (en.getKey().test(uri)) {
|
||||||
@@ -138,7 +139,7 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
regArray[regArray.length - 1] = new SimpleEntry<>(Pattern.compile(mapping).asPredicate(), servlet);
|
regArray[regArray.length - 1] = new SimpleEntry<>(Pattern.compile(mapping).asPredicate(), servlet);
|
||||||
}
|
}
|
||||||
} else if (mapping != null && !mapping.isEmpty()) {
|
} else if (mapping != null && !mapping.isEmpty()) {
|
||||||
super.mappings.put(mapping, servlet);
|
putMapping(mapping, servlet);
|
||||||
}
|
}
|
||||||
if (this.allMapStrings.containsKey(mapping)) {
|
if (this.allMapStrings.containsKey(mapping)) {
|
||||||
Class old = this.allMapStrings.get(mapping);
|
Class old = this.allMapStrings.get(mapping);
|
||||||
@@ -148,7 +149,7 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
}
|
}
|
||||||
setServletConf(servlet, conf);
|
setServletConf(servlet, conf);
|
||||||
servlet._prefix = prefix.toString();
|
servlet._prefix = prefix.toString();
|
||||||
this.servlets.add(servlet);
|
putServlet(servlet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +176,7 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
@Override
|
@Override
|
||||||
public void destroy(HttpContext context, AnyValue config) {
|
public void destroy(HttpContext context, AnyValue config) {
|
||||||
this.resourceHttpServlet.destroy(context, config);
|
this.resourceHttpServlet.destroy(context, config);
|
||||||
this.servlets.forEach(s -> {
|
getServlets().forEach(s -> {
|
||||||
s.destroy(context, getServletConf(s));
|
s.destroy(context, getServletConf(s));
|
||||||
if (s instanceof WebSocketServlet) {
|
if (s instanceof WebSocketServlet) {
|
||||||
((WebSocketServlet) s).postDestroy(context, getServletConf(s));
|
((WebSocketServlet) s).postDestroy(context, getServletConf(s));
|
||||||
|
|||||||
@@ -30,26 +30,22 @@ public class SncpPrepareServlet extends PrepareServlet<DLong, SncpContext, SncpR
|
|||||||
|
|
||||||
public void addServlet(SncpServlet servlet, AnyValue conf) {
|
public void addServlet(SncpServlet servlet, AnyValue conf) {
|
||||||
setServletConf(servlet, conf);
|
setServletConf(servlet, conf);
|
||||||
synchronized (mappings) {
|
putMapping(servlet.getServiceid(), servlet);
|
||||||
mappings.put(servlet.getServiceid(), servlet);
|
putServlet(servlet);
|
||||||
servlets.add(servlet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SncpServlet> getSncpServlets() {
|
public List<SncpServlet> getSncpServlets() {
|
||||||
ArrayList<SncpServlet> list = new ArrayList<>(servlets.size());
|
return new ArrayList<>(getServlets());
|
||||||
servlets.forEach(x -> list.add((SncpServlet) x));
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(SncpContext context, AnyValue config) {
|
public void init(SncpContext context, AnyValue config) {
|
||||||
servlets.forEach(s -> s.init(context, getServletConf(s)));
|
getServlets().forEach(s -> s.init(context, getServletConf(s)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy(SncpContext context, AnyValue config) {
|
public void destroy(SncpContext context, AnyValue config) {
|
||||||
servlets.forEach(s -> s.destroy(context, getServletConf(s)));
|
getServlets().forEach(s -> s.destroy(context, getServletConf(s)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -58,7 +54,7 @@ public class SncpPrepareServlet extends PrepareServlet<DLong, SncpContext, SncpR
|
|||||||
response.finish(pongBuffer.duplicate());
|
response.finish(pongBuffer.duplicate());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SncpServlet servlet = (SncpServlet) mappings.get(request.getServiceid());
|
SncpServlet servlet = (SncpServlet) mappingServlet(request.getServiceid());
|
||||||
if (servlet == null) {
|
if (servlet == null) {
|
||||||
response.finish(SncpResponse.RETCODE_ILLSERVICEID, null); //无效serviceid
|
response.finish(SncpResponse.RETCODE_ILLSERVICEID, null); //无效serviceid
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user