增加NonBlocking

This commit is contained in:
redkale
2023-02-03 21:47:39 +08:00
parent de39ac1981
commit 73c9a3cdb4
4 changed files with 34 additions and 60 deletions

View File

@@ -1,23 +0,0 @@
/*
*
*/
package org.redkale.annotation;
import java.lang.annotation.*;
/**
* 异步模式标记。
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Async {
boolean value() default true;
}

View File

@@ -0,0 +1,28 @@
/*
*
*/
package org.redkale.annotation;
import java.lang.annotation.*;
/**
* 非阻塞模式标记, 标记在Service类和方法、Filter类、HttpServlet类上 <br>
* 一般情况下,没有显注此注解的方法视为阻塞时, 以下两种情况除外: <br>
* 1、返回类型是CompletableFuture <br>
* 2、返回类型是void且参数存在CompletionHandler类型 <br>
* 阻塞模式的方法会在work线程池中运行 非阻塞在IO线程中运行。
*
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NonBlocking {
boolean value() default true;
}

View File

@@ -12,7 +12,7 @@ import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.stream.Stream;
import org.redkale.annotation.Async;
import org.redkale.annotation.NonBlocking;
import org.redkale.boot.Application;
import org.redkale.util.*;
@@ -210,15 +210,15 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
filtersLock.lock();
try {
this.filters.add(filter);
this.allFilterAsync = this.allFilterAsync && isAsync(filter);
this.allFilterAsync = this.allFilterAsync && isNonBlocking(filter);
Collections.sort(this.filters);
} finally {
filtersLock.unlock();
}
}
private boolean isAsync(Filter filter) {
Async a = filter.getClass().getAnnotation(Async.class);
private boolean isNonBlocking(Filter filter) {
NonBlocking a = filter.getClass().getAnnotation(NonBlocking.class);
return a != null && a.value();
}
@@ -277,7 +277,7 @@ public abstract class DispatcherServlet<K extends Serializable, C extends Contex
this.filters.remove(filter);
boolean async = true;
for (Filter f : filters) {
async = async && isAsync(filter);
async = async && isNonBlocking(filter);
if (!async) {
break;
}

View File

@@ -13,7 +13,7 @@ import org.redkale.annotation.ResourceType;
import org.redkale.net.*;
import org.redkale.net.sncp.*;
import org.redkale.service.*;
import org.redkale.util.*;
import org.redkale.util.ResourceFactory;
/**
*
@@ -51,37 +51,6 @@ public class SncpTestServiceImpl implements SncpTestIService {
return value + 1;
}
public static class CallAttribute implements Attribute<SncpTestBean, Long> {
@Override
public Class<? extends Long> type() {
return long.class;
}
@Override
public Class<SncpTestBean> declaringClass() {
return SncpTestBean.class;
}
@Override
public String field() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Long get(SncpTestBean obj) {
System.out.println("返回ID: " + obj.getId());
return obj.getId();
}
@Override
public void set(SncpTestBean obj, Long value) {
System.out.println("设置ID: " + value);
obj.setId(value);
}
}
@Override
public SncpTestBean insert(SncpTestBean bean) {
bean.setId(System.currentTimeMillis());