Filter.isNonBlocking
This commit is contained in:
@@ -6,7 +6,7 @@ package org.redkale.annotation;
|
|||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 非阻塞模式标记, 标记在Service类和方法、Filter类、HttpServlet类上 <br>
|
* 非阻塞模式标记, 标记在Service类和方法、HttpServlet类上 <br>
|
||||||
* 一般情况下,没有显注此注解的方法视为阻塞时, 以下两种情况除外: <br>
|
* 一般情况下,没有显注此注解的方法视为阻塞时, 以下两种情况除外: <br>
|
||||||
* 1、返回类型是CompletableFuture <br>
|
* 1、返回类型是CompletableFuture <br>
|
||||||
* 2、返回类型是void且参数存在CompletionHandler类型 <br>
|
* 2、返回类型是void且参数存在CompletionHandler类型 <br>
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import org.redkale.annotation.NonBlocking;
|
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -45,8 +44,6 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
|||||||
|
|
||||||
private Map<K, S> mappings = new HashMap<>();
|
private Map<K, S> mappings = new HashMap<>();
|
||||||
|
|
||||||
private volatile boolean allFilterAsync = true;
|
|
||||||
|
|
||||||
private final List<Filter<C, R, P>> filters = new ArrayList<>();
|
private final List<Filter<C, R, P>> filters = new ArrayList<>();
|
||||||
|
|
||||||
protected final ReentrantLock filtersLock = new ReentrantLock();
|
protected final ReentrantLock filtersLock = new ReentrantLock();
|
||||||
@@ -210,18 +207,12 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
|||||||
filtersLock.lock();
|
filtersLock.lock();
|
||||||
try {
|
try {
|
||||||
this.filters.add(filter);
|
this.filters.add(filter);
|
||||||
this.allFilterAsync = this.allFilterAsync && isNonBlocking(filter);
|
|
||||||
Collections.sort(this.filters);
|
Collections.sort(this.filters);
|
||||||
} finally {
|
} finally {
|
||||||
filtersLock.unlock();
|
filtersLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNonBlocking(Filter filter) {
|
|
||||||
NonBlocking a = filter.getClass().getAnnotation(NonBlocking.class);
|
|
||||||
return a != null && a.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T extends Filter<C, R, P>> T removeFilter(Class<T> filterClass) {
|
public <T extends Filter<C, R, P>> T removeFilter(Class<T> filterClass) {
|
||||||
return removeFilter(f -> filterClass.equals(f.getClass()));
|
return removeFilter(f -> filterClass.equals(f.getClass()));
|
||||||
}
|
}
|
||||||
@@ -275,14 +266,6 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
|||||||
}
|
}
|
||||||
filter._next = null;
|
filter._next = null;
|
||||||
this.filters.remove(filter);
|
this.filters.remove(filter);
|
||||||
boolean async = true;
|
|
||||||
for (Filter f : filters) {
|
|
||||||
async = async && isNonBlocking(filter);
|
|
||||||
if (!async) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.allFilterAsync = async;
|
|
||||||
}
|
}
|
||||||
return (T) filter;
|
return (T) filter;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -304,6 +287,7 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
|
|||||||
request.prepare();
|
request.prepare();
|
||||||
response.filter = this.headFilter;
|
response.filter = this.headFilter;
|
||||||
response.servlet = this;
|
response.servlet = this;
|
||||||
|
response.inNonBlocking = true;
|
||||||
response.nextEvent();
|
response.nextEvent();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
response.context.logger.log(Level.WARNING, "prepare servlet abort, force to close channel ", t);
|
response.context.logger.log(Level.WARNING, "prepare servlet abort, force to close channel ", t);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ package org.redkale.net;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.redkale.annotation.Priority;
|
import org.redkale.annotation.Priority;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.AnyValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 协议拦截器类, 类似JavaEE中的javax.servlet.Filter <br>
|
* 协议拦截器类, 类似JavaEE中的javax.servlet.Filter <br>
|
||||||
@@ -33,12 +33,16 @@ public abstract class Filter<C extends Context, R extends Request<C>, P extends
|
|||||||
|
|
||||||
public abstract void doFilter(R request, P response) throws IOException;
|
public abstract void doFilter(R request, P response) throws IOException;
|
||||||
|
|
||||||
|
public abstract boolean isNonBlocking();
|
||||||
|
|
||||||
public void destroy(C context, AnyValue config) {
|
public void destroy(C context, AnyValue config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Object o) {
|
public int compareTo(Object o) {
|
||||||
if (!(o instanceof Filter)) return 1;
|
if (!(o instanceof Filter)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Priority p1 = this.getClass().getAnnotation(Priority.class);
|
Priority p1 = this.getClass().getAnnotation(Priority.class);
|
||||||
Priority p2 = o.getClass().getAnnotation(Priority.class);
|
Priority p2 = o.getClass().getAnnotation(Priority.class);
|
||||||
return (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value());
|
return (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value());
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
|
|
||||||
private volatile boolean inited = true;
|
private volatile boolean inited = true;
|
||||||
|
|
||||||
|
protected boolean inNonBlocking = true;
|
||||||
|
|
||||||
protected Object output; //输出的结果对象
|
protected Object output; //输出的结果对象
|
||||||
|
|
||||||
protected BiConsumer<R, Response<C, R>> recycleListener;
|
protected BiConsumer<R, Response<C, R>> recycleListener;
|
||||||
@@ -127,6 +129,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
|
|
||||||
protected void prepare() {
|
protected void prepare() {
|
||||||
inited = true;
|
inited = true;
|
||||||
|
inNonBlocking = true;
|
||||||
request.prepare();
|
request.prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,6 +154,10 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean inNonBlocking() {
|
||||||
|
return inNonBlocking;
|
||||||
|
}
|
||||||
|
|
||||||
protected void refuseAlive() {
|
protected void refuseAlive() {
|
||||||
this.request.keepAlive = false;
|
this.request.keepAlive = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user