删掉AsyncHandler, 采用CompletionHandler代替
This commit is contained in:
@@ -99,7 +99,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
this.register(Pattern.class, PatternSimpledCoder.instance);
|
this.register(Pattern.class, PatternSimpledCoder.instance);
|
||||||
this.register(File.class, FileSimpledCoder.instance);
|
this.register(File.class, FileSimpledCoder.instance);
|
||||||
this.register(CompletionHandler.class, CompletionHandlerSimpledCoder.instance);
|
this.register(CompletionHandler.class, CompletionHandlerSimpledCoder.instance);
|
||||||
this.register(AsyncHandler.class, AsyncHandlerSimpledCoder.instance);
|
|
||||||
this.register(URL.class, URLSimpledCoder.instance);
|
this.register(URL.class, URLSimpledCoder.instance);
|
||||||
this.register(URI.class, URISimpledCoder.instance);
|
this.register(URI.class, URISimpledCoder.instance);
|
||||||
//---------------------------------------------------------
|
//---------------------------------------------------------
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
package org.redkale.convert.ext;
|
|
||||||
|
|
||||||
import org.redkale.convert.*;
|
|
||||||
import org.redkale.util.AsyncHandler;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* AsyncHandlerSimpledCoder 的SimpledCoder实现, 只输出null
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <R> Reader输入的子类型
|
|
||||||
* @param <W> Writer输出的子类型
|
|
||||||
*/
|
|
||||||
public final class AsyncHandlerSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, AsyncHandler> {
|
|
||||||
|
|
||||||
public static final AsyncHandlerSimpledCoder instance = new AsyncHandlerSimpledCoder();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convertTo(W out, AsyncHandler value) {
|
|
||||||
out.writeObjectNull(AsyncHandler.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AsyncHandler convertFrom(R in) {
|
|
||||||
in.readObjectB(AsyncHandler.class);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,7 @@ package org.redkale.net.http;
|
|||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.nio.charset.*;
|
import java.nio.charset.*;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
@@ -54,7 +55,7 @@ public class HttpContext extends Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected <H extends AsyncHandler> Creator<H> loadAsyncHandlerCreator(Class<H> handlerClass) {
|
protected <H extends CompletionHandler> Creator<H> loadAsyncHandlerCreator(Class<H> handlerClass) {
|
||||||
Creator<H> creator = asyncHandlerCreators.get(handlerClass);
|
Creator<H> creator = asyncHandlerCreators.get(handlerClass);
|
||||||
if (creator == null) {
|
if (creator == null) {
|
||||||
creator = createAsyncHandlerCreator(handlerClass);
|
creator = createAsyncHandlerCreator(handlerClass);
|
||||||
@@ -64,14 +65,14 @@ public class HttpContext extends Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <H extends AsyncHandler> Creator<H> createAsyncHandlerCreator(Class<H> handlerClass) {
|
private <H extends CompletionHandler> Creator<H> createAsyncHandlerCreator(Class<H> handlerClass) {
|
||||||
//生成规则与SncpAsyncHandler.Factory 很类似
|
//生成规则与SncpAsyncHandler.Factory 很类似
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
final boolean handlerinterface = handlerClass.isInterface();
|
final boolean handlerinterface = handlerClass.isInterface();
|
||||||
final String handlerClassName = handlerClass.getName().replace('.', '/');
|
final String handlerClassName = handlerClass.getName().replace('.', '/');
|
||||||
final String handlerName = AsyncHandler.class.getName().replace('.', '/');
|
final String handlerName = CompletionHandler.class.getName().replace('.', '/');
|
||||||
final String handlerDesc = Type.getDescriptor(AsyncHandler.class);
|
final String handlerDesc = Type.getDescriptor(CompletionHandler.class);
|
||||||
final String newDynName = handlerClass.getName().replace('.', '/') + "_Dync" + AsyncHandler.class.getSimpleName() + "_" + (System.currentTimeMillis() % 10000);
|
final String newDynName = handlerClass.getName().replace('.', '/') + "_DyncAsyncHandler_" + (System.currentTimeMillis() % 10000);
|
||||||
|
|
||||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
|
||||||
FieldVisitor fv;
|
FieldVisitor fv;
|
||||||
@@ -157,7 +158,7 @@ public class HttpContext extends Context {
|
|||||||
}
|
}
|
||||||
cw.visitEnd();
|
cw.visitEnd();
|
||||||
byte[] bytes = cw.toByteArray();
|
byte[] bytes = cw.toByteArray();
|
||||||
Class<AsyncHandler> newHandlerClazz = (Class<AsyncHandler>) new ClassLoader(handlerClass.getClassLoader()) {
|
Class<CompletionHandler> newHandlerClazz = (Class<CompletionHandler>) new ClassLoader(handlerClass.getClassLoader()) {
|
||||||
public final Class<?> loadClass(String name, byte[] b) {
|
public final Class<?> loadClass(String name, byte[] b) {
|
||||||
return defineClass(name, b, 0, b.length);
|
return defineClass(name, b, 0, b.length);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -221,12 +221,12 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建AsyncHandler实例
|
* 创建CompletionHandler实例
|
||||||
*
|
*
|
||||||
* @return AsyncHandler
|
* @return CompletionHandler
|
||||||
*/
|
*/
|
||||||
public AsyncHandler createAsyncHandler() {
|
public CompletionHandler createAsyncHandler() {
|
||||||
return AsyncHandler.create((v, a) -> {
|
return Utility.createAsyncHandler((v, a) -> {
|
||||||
if (v instanceof org.redkale.service.RetResult) {
|
if (v instanceof org.redkale.service.RetResult) {
|
||||||
finishJson((org.redkale.service.RetResult) v);
|
finishJson((org.redkale.service.RetResult) v);
|
||||||
} else if (v instanceof CharSequence) {
|
} else if (v instanceof CharSequence) {
|
||||||
@@ -241,18 +241,18 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建AsyncHandler子类的实例 <br>
|
* 创建CompletionHandler子类的实例 <br>
|
||||||
*
|
*
|
||||||
* 传入的AsyncHandler子类必须是public,且保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数。
|
* 传入的CompletionHandler子类必须是public,且保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数。
|
||||||
*
|
*
|
||||||
* @param <H> 泛型
|
* @param <H> 泛型
|
||||||
* @param handlerClass AsyncHandler子类
|
* @param handlerClass CompletionHandler子类
|
||||||
*
|
*
|
||||||
* @return AsyncHandler AsyncHandler
|
* @return CompletionHandler
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <H extends AsyncHandler> H createAsyncHandler(Class<H> handlerClass) {
|
public <H extends CompletionHandler> H createAsyncHandler(Class<H> handlerClass) {
|
||||||
if (handlerClass == null || handlerClass == AsyncHandler.class) return (H) createAsyncHandler();
|
if (handlerClass == null || handlerClass == CompletionHandler.class) return (H) createAsyncHandler();
|
||||||
return context.loadAsyncHandlerCreator(handlerClass).create(createAsyncHandler());
|
return context.loadAsyncHandlerCreator(handlerClass).create(createAsyncHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -650,7 +650,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
* @param attachment 异步回调参数
|
* @param attachment 异步回调参数
|
||||||
* @param handler 异步回调函数
|
* @param handler 异步回调函数
|
||||||
*/
|
*/
|
||||||
public <A> void sendBody(ByteBuffer buffer, A attachment, AsyncHandler<Integer, A> handler) {
|
public <A> void sendBody(ByteBuffer buffer, A attachment, CompletionHandler<Integer, A> handler) {
|
||||||
if (!this.headsended) {
|
if (!this.headsended) {
|
||||||
if (this.contentLength < 0) this.contentLength = buffer == null ? 0 : buffer.remaining();
|
if (this.contentLength < 0) this.contentLength = buffer == null ? 0 : buffer.remaining();
|
||||||
ByteBuffer headbuf = createHeader();
|
ByteBuffer headbuf = createHeader();
|
||||||
@@ -673,7 +673,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
* @param attachment 异步回调参数
|
* @param attachment 异步回调参数
|
||||||
* @param handler 异步回调函数
|
* @param handler 异步回调函数
|
||||||
*/
|
*/
|
||||||
public <A> void sendBody(ByteBuffer[] buffers, A attachment, AsyncHandler<Integer, A> handler) {
|
public <A> void sendBody(ByteBuffer[] buffers, A attachment, CompletionHandler<Integer, A> handler) {
|
||||||
if (!this.headsended) {
|
if (!this.headsended) {
|
||||||
if (this.contentLength < 0) {
|
if (this.contentLength < 0) {
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -1024,7 +1024,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
this.bufferHandler = bufferHandler;
|
this.bufferHandler = bufferHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final class TransferFileHandler implements AsyncHandler<Integer, ByteBuffer> {
|
protected final class TransferFileHandler implements CompletionHandler<Integer, ByteBuffer> {
|
||||||
|
|
||||||
private final File file;
|
private final File file;
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.lang.annotation.*;
|
|||||||
import static java.lang.annotation.ElementType.*;
|
import static java.lang.annotation.ElementType.*;
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -142,13 +143,20 @@ public final class Rest {
|
|||||||
return childFactory.getConvert();
|
return childFactory.getConvert();
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getWebModuleName(Class<? extends Service> serviceType) {
|
static String getWebModuleNameLowerCase(Class<? extends Service> serviceType) {
|
||||||
final RestService controller = serviceType.getAnnotation(RestService.class);
|
final RestService controller = serviceType.getAnnotation(RestService.class);
|
||||||
if (controller == null) return serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase();
|
if (controller == null) return serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase();
|
||||||
if (controller.ignore()) return null;
|
if (controller.ignore()) return null;
|
||||||
return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase();
|
return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String getWebModuleName(Class<? extends Service> serviceType) {
|
||||||
|
final RestService controller = serviceType.getAnnotation(RestService.class);
|
||||||
|
if (controller == null) return serviceType.getSimpleName().replaceAll("Service.*$", "");
|
||||||
|
if (controller.ignore()) return null;
|
||||||
|
return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", "");
|
||||||
|
}
|
||||||
|
|
||||||
static boolean isRestDyn(HttpServlet servlet) {
|
static boolean isRestDyn(HttpServlet servlet) {
|
||||||
return servlet.getClass().getAnnotation(RestDyn.class) != null;
|
return servlet.getClass().getAnnotation(RestDyn.class) != null;
|
||||||
}
|
}
|
||||||
@@ -324,7 +332,7 @@ public final class Rest {
|
|||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
pushInt(mv, rws.wsmaxbody());
|
pushInt(mv, rws.wsmaxbody());
|
||||||
mv.visitFieldInsn(PUTFIELD, newDynName, "wsmaxbody", "I");
|
mv.visitFieldInsn(PUTFIELD, newDynName, "wsmaxbody", "I");
|
||||||
|
|
||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
mv.visitInsn(rws.single() ? ICONST_1 : ICONST_0);
|
mv.visitInsn(rws.single() ? ICONST_1 : ICONST_0);
|
||||||
mv.visitFieldInsn(PUTFIELD, newDynName, "single", "Z");
|
mv.visitFieldInsn(PUTFIELD, newDynName, "single", "Z");
|
||||||
@@ -332,7 +340,7 @@ public final class Rest {
|
|||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
mv.visitInsn(rws.anyuser() ? ICONST_1 : ICONST_0);
|
mv.visitInsn(rws.anyuser() ? ICONST_1 : ICONST_0);
|
||||||
mv.visitFieldInsn(PUTFIELD, newDynName, "anyuser", "Z");
|
mv.visitFieldInsn(PUTFIELD, newDynName, "anyuser", "Z");
|
||||||
|
|
||||||
mv.visitInsn(RETURN);
|
mv.visitInsn(RETURN);
|
||||||
mv.visitMaxs(3, 1);
|
mv.visitMaxs(3, 1);
|
||||||
mv.visitEnd();
|
mv.visitEnd();
|
||||||
@@ -607,7 +615,8 @@ public final class Rest {
|
|||||||
String newDynName = serviceTypeInternalName.substring(0, serviceTypeInternalName.lastIndexOf('/') + 1) + "_Dyn" + serviceType.getSimpleName().replaceAll("Service.*$", "") + "RestServlet";
|
String newDynName = serviceTypeInternalName.substring(0, serviceTypeInternalName.lastIndexOf('/') + 1) + "_Dyn" + serviceType.getSimpleName().replaceAll("Service.*$", "") + "RestServlet";
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
final String defmodulename = getWebModuleName(serviceType);
|
final String defmodulename = getWebModuleNameLowerCase(serviceType);
|
||||||
|
final String bigmodulename = getWebModuleName(serviceType);
|
||||||
final String catalog = controller == null ? "" : controller.catalog();
|
final String catalog = controller == null ? "" : controller.catalog();
|
||||||
if (!checkName(catalog)) throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
|
if (!checkName(catalog)) throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
|
||||||
if (!checkName(defmodulename)) throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ cannot begin 0-9");
|
if (!checkName(defmodulename)) throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ cannot begin 0-9");
|
||||||
@@ -710,7 +719,7 @@ public final class Rest {
|
|||||||
if (ignore) continue;
|
if (ignore) continue;
|
||||||
paramtypes.add(method.getGenericParameterTypes());
|
paramtypes.add(method.getGenericParameterTypes());
|
||||||
if (mappings.length == 0) { //没有Mapping,设置一个默认值
|
if (mappings.length == 0) { //没有Mapping,设置一个默认值
|
||||||
MappingEntry entry = new MappingEntry(methodidex, null, defmodulename, method);
|
MappingEntry entry = new MappingEntry(methodidex, null, bigmodulename, method);
|
||||||
if (entrys.contains(entry)) throw new RuntimeException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat");
|
if (entrys.contains(entry)) throw new RuntimeException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat");
|
||||||
entrys.add(entry);
|
entrys.add(entry);
|
||||||
} else {
|
} else {
|
||||||
@@ -723,7 +732,6 @@ public final class Rest {
|
|||||||
methodidex++;
|
methodidex++;
|
||||||
}
|
}
|
||||||
if (entrys.isEmpty()) return null; //没有可HttpMapping的方法
|
if (entrys.isEmpty()) return null; //没有可HttpMapping的方法
|
||||||
|
|
||||||
//将每个Service可转换的方法生成HttpServlet对应的HttpMapping方法
|
//将每个Service可转换的方法生成HttpServlet对应的HttpMapping方法
|
||||||
final Map<String, List<String>> asmParamMap = MethodParamClassVisitor.getMethodParamNames(serviceType);
|
final Map<String, List<String>> asmParamMap = MethodParamClassVisitor.getMethodParamNames(serviceType);
|
||||||
final Map<String, java.lang.reflect.Type> bodyTypes = new HashMap<>();
|
final Map<String, java.lang.reflect.Type> bodyTypes = new HashMap<>();
|
||||||
@@ -870,7 +878,7 @@ public final class Rest {
|
|||||||
if (ptype.isPrimitive() || ptype == String.class) n = "#";
|
if (ptype.isPrimitive() || ptype == String.class) n = "#";
|
||||||
}
|
}
|
||||||
if (annhead == null && anncookie == null && annsid == null && annaddr == null && annbody == null && annfile == null
|
if (annhead == null && anncookie == null && annsid == null && annaddr == null && annbody == null && annfile == null
|
||||||
&& !ptype.isPrimitive() && ptype != String.class && ptype != Flipper.class && !AsyncHandler.class.isAssignableFrom(ptype)
|
&& !ptype.isPrimitive() && ptype != String.class && ptype != Flipper.class && !CompletionHandler.class.isAssignableFrom(ptype)
|
||||||
&& !ptype.getName().startsWith("java") && n.charAt(0) != '#' && !"&".equals(n)) { //判断Json对象是否包含@RestUploadFile
|
&& !ptype.getName().startsWith("java") && n.charAt(0) != '#' && !"&".equals(n)) { //判断Json对象是否包含@RestUploadFile
|
||||||
Class loop = ptype;
|
Class loop = ptype;
|
||||||
do {
|
do {
|
||||||
@@ -1007,17 +1015,17 @@ public final class Rest {
|
|||||||
|
|
||||||
paramMap.put("name", pname);
|
paramMap.put("name", pname);
|
||||||
paramMap.put("type", ptype.getName());
|
paramMap.put("type", ptype.getName());
|
||||||
if (AsyncHandler.class.isAssignableFrom(ptype)) { //HttpResponse.createAsyncHandler() or HttpResponse.createAsyncHandler(Class)
|
if (CompletionHandler.class.isAssignableFrom(ptype)) { //HttpResponse.createAsyncHandler() or HttpResponse.createAsyncHandler(Class)
|
||||||
if (ptype == AsyncHandler.class) {
|
if (ptype == CompletionHandler.class) {
|
||||||
mv.visitVarInsn(ALOAD, 2);
|
mv.visitVarInsn(ALOAD, 2);
|
||||||
mv.visitMethodInsn(INVOKEVIRTUAL, respInternalName, "createAsyncHandler", "()Lorg/redkale/util/AsyncHandler;", false);
|
mv.visitMethodInsn(INVOKEVIRTUAL, respInternalName, "createAsyncHandler", "()Ljava/nio/channels/CompletionHandler;", false);
|
||||||
mv.visitVarInsn(ASTORE, maxLocals);
|
mv.visitVarInsn(ASTORE, maxLocals);
|
||||||
varInsns.add(new int[]{ALOAD, maxLocals});
|
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||||
} else {
|
} else {
|
||||||
mv.visitVarInsn(ALOAD, 3);
|
mv.visitVarInsn(ALOAD, 3);
|
||||||
mv.visitVarInsn(ALOAD, 2);
|
mv.visitVarInsn(ALOAD, 2);
|
||||||
mv.visitLdcInsn(Type.getType(Type.getDescriptor(ptype)));
|
mv.visitLdcInsn(Type.getType(Type.getDescriptor(ptype)));
|
||||||
mv.visitMethodInsn(INVOKEVIRTUAL, respInternalName, "createAsyncHandler", "(Ljava/lang/Class;)Lorg/redkale/util/AsyncHandler;", false);
|
mv.visitMethodInsn(INVOKEVIRTUAL, respInternalName, "createAsyncHandler", "(Ljava/lang/Class;)Ljava/nio/channels/CompletionHandler;", false);
|
||||||
mv.visitTypeInsn(CHECKCAST, ptype.getName().replace('.', '/'));
|
mv.visitTypeInsn(CHECKCAST, ptype.getName().replace('.', '/'));
|
||||||
mv.visitVarInsn(ASTORE, maxLocals);
|
mv.visitVarInsn(ASTORE, maxLocals);
|
||||||
varInsns.add(new int[]{ALOAD, maxLocals});
|
varInsns.add(new int[]{ALOAD, maxLocals});
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.io.*;
|
|||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -192,7 +193,7 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
|
|||||||
response.setHeader("Connection", "Upgrade");
|
response.setHeader("Connection", "Upgrade");
|
||||||
response.addHeader("Upgrade", "websocket");
|
response.addHeader("Upgrade", "websocket");
|
||||||
response.addHeader("Sec-WebSocket-Accept", Base64.getEncoder().encodeToString(bytes));
|
response.addHeader("Sec-WebSocket-Accept", Base64.getEncoder().encodeToString(bytes));
|
||||||
response.sendBody((ByteBuffer) null, null, new AsyncHandler<Integer, Void>() {
|
response.sendBody((ByteBuffer) null, null, new CompletionHandler<Integer, Void>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void completed(Integer result, Void attachment) {
|
public void completed(Integer result, Void attachment) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.lang.annotation.Annotation;
|
|||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -138,12 +139,12 @@ public abstract class Sncp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void checkAsyncModifier(Class param, Method method) {
|
static void checkAsyncModifier(Class param, Method method) {
|
||||||
if (param == AsyncHandler.class) return;
|
if (param == CompletionHandler.class) return;
|
||||||
if (Modifier.isFinal(param.getModifiers())) {
|
if (Modifier.isFinal(param.getModifiers())) {
|
||||||
throw new RuntimeException("AsyncHandler Type Parameter on {" + method + "} cannot final modifier");
|
throw new RuntimeException("CompletionHandler Type Parameter on {" + method + "} cannot final modifier");
|
||||||
}
|
}
|
||||||
if (!Modifier.isPublic(param.getModifiers())) {
|
if (!Modifier.isPublic(param.getModifiers())) {
|
||||||
throw new RuntimeException("AsyncHandler Type Parameter on {" + method + "} must be public modifier");
|
throw new RuntimeException("CompletionHandler Type Parameter on {" + method + "} must be public modifier");
|
||||||
}
|
}
|
||||||
if (param.isInterface()) return;
|
if (param.isInterface()) return;
|
||||||
boolean constructorflag = false;
|
boolean constructorflag = false;
|
||||||
@@ -388,8 +389,8 @@ public abstract class Sncp {
|
|||||||
int varindex = 0;
|
int varindex = 0;
|
||||||
boolean handlerFuncFlag = false;
|
boolean handlerFuncFlag = false;
|
||||||
for (Class pt : paramtypes) {
|
for (Class pt : paramtypes) {
|
||||||
if (AsyncHandler.class.isAssignableFrom(pt)) {
|
if (CompletionHandler.class.isAssignableFrom(pt)) {
|
||||||
if (handlerFuncFlag) throw new RuntimeException(method + " have more than one AsyncHandler type parameter");
|
if (handlerFuncFlag) throw new RuntimeException(method + " have more than one CompletionHandler type parameter");
|
||||||
checkAsyncModifier(pt, method);
|
checkAsyncModifier(pt, method);
|
||||||
handlerFuncFlag = true;
|
handlerFuncFlag = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.net.sncp;
|
package org.redkale.net.sncp;
|
||||||
|
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import jdk.internal.org.objectweb.asm.*;
|
import jdk.internal.org.objectweb.asm.*;
|
||||||
@@ -26,7 +27,7 @@ import org.redkale.util.*;
|
|||||||
* @param <V> 结果对象的泛型
|
* @param <V> 结果对象的泛型
|
||||||
* @param <A> 附件对象的泛型
|
* @param <A> 附件对象的泛型
|
||||||
*/
|
*/
|
||||||
public interface SncpAsyncHandler<V, A> extends AsyncHandler<V, A> {
|
public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
|
||||||
|
|
||||||
public Object[] sncp_getParams();
|
public Object[] sncp_getParams();
|
||||||
|
|
||||||
@@ -42,9 +43,9 @@ public interface SncpAsyncHandler<V, A> extends AsyncHandler<V, A> {
|
|||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
*
|
*
|
||||||
* 考虑点:
|
* 考虑点:
|
||||||
* 1、AsyncHandler子类是接口,且还有其他多个方法
|
* 1、CompletionHandler子类是接口,且还有其他多个方法
|
||||||
* 2、AsyncHandler子类是类, 需要继承,且必须有空参数构造函数
|
* 2、CompletionHandler子类是类, 需要继承,且必须有空参数构造函数
|
||||||
* 3、AsyncHandler子类无论是接口还是类,都可能存在其他泛型
|
* 3、CompletionHandler子类无论是接口还是类,都可能存在其他泛型
|
||||||
*
|
*
|
||||||
* public class XXXAsyncHandler_DyncSncpAsyncHandler_4323 extends XXXAsyncHandler implements SncpAsyncHandler {
|
* public class XXXAsyncHandler_DyncSncpAsyncHandler_4323 extends XXXAsyncHandler implements SncpAsyncHandler {
|
||||||
*
|
*
|
||||||
@@ -91,11 +92,11 @@ public interface SncpAsyncHandler<V, A> extends AsyncHandler<V, A> {
|
|||||||
*
|
*
|
||||||
* </pre></blockquote>
|
* </pre></blockquote>
|
||||||
*
|
*
|
||||||
* @param handlerClass AsyncHandler类型或子类
|
* @param handlerClass CompletionHandler类型或子类
|
||||||
*
|
*
|
||||||
* @return Creator
|
* @return Creator
|
||||||
*/
|
*/
|
||||||
public static Creator<SncpAsyncHandler> createCreator(Class<? extends AsyncHandler> handlerClass) {
|
public static Creator<SncpAsyncHandler> createCreator(Class<? extends CompletionHandler> handlerClass) {
|
||||||
//-------------------------------------------------------------
|
//-------------------------------------------------------------
|
||||||
final boolean handlerinterface = handlerClass.isInterface();
|
final boolean handlerinterface = handlerClass.isInterface();
|
||||||
final String handlerClassName = handlerClass.getName().replace('.', '/');
|
final String handlerClassName = handlerClass.getName().replace('.', '/');
|
||||||
|
|||||||
@@ -293,7 +293,7 @@ public final class SncpClient {
|
|||||||
//只给远程模式调用的
|
//只给远程模式调用的
|
||||||
public <T> T remote(final int index, final Object... params) {
|
public <T> T remote(final int index, final Object... params) {
|
||||||
final SncpAction action = actions[index];
|
final SncpAction action = actions[index];
|
||||||
final AsyncHandler handlerFunc = action.handlerFuncParamIndex >= 0 ? (AsyncHandler) params[action.handlerFuncParamIndex] : null;
|
final CompletionHandler handlerFunc = action.handlerFuncParamIndex >= 0 ? (CompletionHandler) params[action.handlerFuncParamIndex] : null;
|
||||||
if (action.handlerFuncParamIndex >= 0) params[action.handlerFuncParamIndex] = null;
|
if (action.handlerFuncParamIndex >= 0) params[action.handlerFuncParamIndex] = null;
|
||||||
final BsonReader reader = bsonConvert.pollBsonReader();
|
final BsonReader reader = bsonConvert.pollBsonReader();
|
||||||
CompletableFuture<byte[]> future = remote0(handlerFunc, remoteGroupTransport, null, action, params);
|
CompletableFuture<byte[]> future = remote0(handlerFunc, remoteGroupTransport, null, action, params);
|
||||||
@@ -339,7 +339,7 @@ public final class SncpClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<byte[]> remote0(final AsyncHandler handler, final Transport transport, final SocketAddress addr0, final SncpAction action, final Object... params) {
|
private CompletableFuture<byte[]> remote0(final CompletionHandler handler, final Transport transport, final SocketAddress addr0, final SncpAction action, final Object... params) {
|
||||||
final Type[] myparamtypes = action.paramTypes;
|
final Type[] myparamtypes = action.paramTypes;
|
||||||
final Class[] myparamclass = action.paramClass;
|
final Class[] myparamclass = action.paramClass;
|
||||||
if (action.addressSourceParamIndex >= 0) params[action.addressSourceParamIndex] = this.clientAddress;
|
if (action.addressSourceParamIndex >= 0) params[action.addressSourceParamIndex] = this.clientAddress;
|
||||||
@@ -347,7 +347,7 @@ public final class SncpClient {
|
|||||||
final BsonWriter writer = bsonConvert.pollBsonWriter(transport.getBufferSupplier()); // 将head写入
|
final BsonWriter writer = bsonConvert.pollBsonWriter(transport.getBufferSupplier()); // 将head写入
|
||||||
writer.writeTo(DEFAULT_HEADER);
|
writer.writeTo(DEFAULT_HEADER);
|
||||||
for (int i = 0; i < params.length; i++) { //params 可能包含: 3 个 boolean
|
for (int i = 0; i < params.length; i++) { //params 可能包含: 3 个 boolean
|
||||||
bsonConvert.convertTo(writer, AsyncHandler.class.isAssignableFrom(myparamclass[i]) ? AsyncHandler.class : myparamtypes[i], params[i]);
|
bsonConvert.convertTo(writer, CompletionHandler.class.isAssignableFrom(myparamclass[i]) ? CompletionHandler.class : myparamtypes[i], params[i]);
|
||||||
}
|
}
|
||||||
final int reqBodyLength = writer.count() - HEADER_SIZE; //body总长度
|
final int reqBodyLength = writer.count() - HEADER_SIZE; //body总长度
|
||||||
final long seqid = System.nanoTime();
|
final long seqid = System.nanoTime();
|
||||||
@@ -571,12 +571,12 @@ public final class SncpClient {
|
|||||||
if (anns.length > 0) {
|
if (anns.length > 0) {
|
||||||
Class<?>[] params = method.getParameterTypes();
|
Class<?>[] params = method.getParameterTypes();
|
||||||
for (int i = 0; i < params.length; i++) {
|
for (int i = 0; i < params.length; i++) {
|
||||||
if (AsyncHandler.class.isAssignableFrom(params[i])) {
|
if (CompletionHandler.class.isAssignableFrom(params[i])) {
|
||||||
if (boolReturnTypeFuture) {
|
if (boolReturnTypeFuture) {
|
||||||
throw new RuntimeException(method + " have both AsyncHandler and CompletableFuture");
|
throw new RuntimeException(method + " have both CompletionHandler and CompletableFuture");
|
||||||
}
|
}
|
||||||
if (handlerFuncIndex >= 0) {
|
if (handlerFuncIndex >= 0) {
|
||||||
throw new RuntimeException(method + " have more than one AsyncHandler type parameter");
|
throw new RuntimeException(method + " have more than one CompletionHandler type parameter");
|
||||||
}
|
}
|
||||||
Sncp.checkAsyncModifier(params[i], method);
|
Sncp.checkAsyncModifier(params[i], method);
|
||||||
handlerFuncIndex = i;
|
handlerFuncIndex = i;
|
||||||
@@ -617,7 +617,7 @@ public final class SncpClient {
|
|||||||
this.handlerAttachParamIndex = handlerAttachIndex;
|
this.handlerAttachParamIndex = handlerAttachIndex;
|
||||||
this.paramAttrs = hasattr ? atts : null;
|
this.paramAttrs = hasattr ? atts : null;
|
||||||
if (this.handlerFuncParamIndex >= 0 && method.getReturnType() != void.class) {
|
if (this.handlerFuncParamIndex >= 0 && method.getReturnType() != void.class) {
|
||||||
throw new RuntimeException(method + " have AsyncHandler type parameter but return type is not void");
|
throw new RuntimeException(method + " have CompletionHandler type parameter but return type is not void");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.io.*;
|
|||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
@@ -118,7 +119,7 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
SncpAsyncHandler handler = null;
|
SncpAsyncHandler handler = null;
|
||||||
try {
|
try {
|
||||||
if (action.handlerFuncParamIndex >= 0) {
|
if (action.handlerFuncParamIndex >= 0) {
|
||||||
if (action.handlerFuncParamClass == AsyncHandler.class) {
|
if (action.handlerFuncParamClass == CompletionHandler.class) {
|
||||||
handler = new DefaultSncpAsyncHandler(action, in, out, request, response);
|
handler = new DefaultSncpAsyncHandler(action, in, out, request, response);
|
||||||
} else {
|
} else {
|
||||||
Creator<SncpAsyncHandler> creator = action.handlerCreator;
|
Creator<SncpAsyncHandler> creator = action.handlerCreator;
|
||||||
@@ -178,15 +179,15 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
|
|
||||||
protected java.lang.reflect.Type[] paramTypes; //index=0表示返回参数的type, void的返回参数类型为null
|
protected java.lang.reflect.Type[] paramTypes; //index=0表示返回参数的type, void的返回参数类型为null
|
||||||
|
|
||||||
protected int handlerFuncParamIndex = -1; //handlerFuncParamIndex>=0表示存在AsyncHandler参数
|
protected int handlerFuncParamIndex = -1; //handlerFuncParamIndex>=0表示存在CompletionHandler参数
|
||||||
|
|
||||||
protected boolean boolReturnTypeFuture = false; // 返回结果类型是否为 CompletableFuture
|
protected boolean boolReturnTypeFuture = false; // 返回结果类型是否为 CompletableFuture
|
||||||
|
|
||||||
protected Class handlerFuncParamClass; //AsyncHandler参数的类型
|
protected Class handlerFuncParamClass; //CompletionHandler参数的类型
|
||||||
|
|
||||||
public abstract void action(final BsonReader in, final BsonWriter out, final SncpAsyncHandler handler) throws Throwable;
|
public abstract void action(final BsonReader in, final BsonWriter out, final SncpAsyncHandler handler) throws Throwable;
|
||||||
|
|
||||||
//只有同步方法才调用 (没有AsyncHandler、CompletableFuture)
|
//只有同步方法才调用 (没有CompletionHandler、CompletableFuture)
|
||||||
public final void _callParameter(final BsonWriter out, final Object... params) {
|
public final void _callParameter(final BsonWriter out, final Object... params) {
|
||||||
if (paramAttrs != null) {
|
if (paramAttrs != null) {
|
||||||
for (int i = 1; i < paramAttrs.length; i++) {
|
for (int i = 1; i < paramAttrs.length; i++) {
|
||||||
@@ -207,10 +208,10 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
* return false;
|
* return false;
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* public void insert(AsyncHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
|
* public void insert(CompletionHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* public void update(long show, short v2, AsyncHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
|
* public void update(long show, short v2, CompletionHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* public CompletableFuture<String> changeName(TestBean bean, String name, int id) {
|
* public CompletableFuture<String> changeName(TestBean bean, String name, int id) {
|
||||||
@@ -241,7 +242,7 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
* @Override
|
* @Override
|
||||||
* public void action(BsonReader in, BsonWriter out, SncpAsyncHandler handler) throws Throwable {
|
* public void action(BsonReader in, BsonWriter out, SncpAsyncHandler handler) throws Throwable {
|
||||||
* SncpAsyncHandler arg0 = handler;
|
* SncpAsyncHandler arg0 = handler;
|
||||||
* convert.convertFrom(AsyncHandler.class, in);
|
* convert.convertFrom(CompletionHandler.class, in);
|
||||||
* TestBean arg1 = convert.convertFrom(paramTypes[2], in);
|
* TestBean arg1 = convert.convertFrom(paramTypes[2], in);
|
||||||
* String arg2 = convert.convertFrom(paramTypes[3], in);
|
* String arg2 = convert.convertFrom(paramTypes[3], in);
|
||||||
* int arg3 = convert.convertFrom(paramTypes[4], in);
|
* int arg3 = convert.convertFrom(paramTypes[4], in);
|
||||||
@@ -259,7 +260,7 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
* long a1 = convert.convertFrom(paramTypes[1], in);
|
* long a1 = convert.convertFrom(paramTypes[1], in);
|
||||||
* short a2 = convert.convertFrom(paramTypes[2], in);
|
* short a2 = convert.convertFrom(paramTypes[2], in);
|
||||||
* SncpAsyncHandler a3 = handler;
|
* SncpAsyncHandler a3 = handler;
|
||||||
* convert.convertFrom(AsyncHandler.class, in);
|
* convert.convertFrom(CompletionHandler.class, in);
|
||||||
* TestBean arg1 = convert.convertFrom(paramTypes[4], in);
|
* TestBean arg1 = convert.convertFrom(paramTypes[4], in);
|
||||||
* String arg2 = convert.convertFrom(paramTypes[5], in);
|
* String arg2 = convert.convertFrom(paramTypes[5], in);
|
||||||
* int arg3 = convert.convertFrom(paramTypes[6], in);
|
* int arg3 = convert.convertFrom(paramTypes[6], in);
|
||||||
@@ -353,12 +354,12 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
final Class[] paramClasses = method.getParameterTypes();
|
final Class[] paramClasses = method.getParameterTypes();
|
||||||
int[][] codes = new int[paramClasses.length][2];
|
int[][] codes = new int[paramClasses.length][2];
|
||||||
for (int i = 0; i < paramClasses.length; i++) { //反序列化方法的每个参数
|
for (int i = 0; i < paramClasses.length; i++) { //反序列化方法的每个参数
|
||||||
if (AsyncHandler.class.isAssignableFrom(paramClasses[i])) {
|
if (CompletionHandler.class.isAssignableFrom(paramClasses[i])) {
|
||||||
if (boolReturnTypeFuture) {
|
if (boolReturnTypeFuture) {
|
||||||
throw new RuntimeException(method + " have both AsyncHandler and CompletableFuture");
|
throw new RuntimeException(method + " have both CompletionHandler and CompletableFuture");
|
||||||
}
|
}
|
||||||
if (handlerFuncIndex >= 0) {
|
if (handlerFuncIndex >= 0) {
|
||||||
throw new RuntimeException(method + " have more than one AsyncHandler type parameter");
|
throw new RuntimeException(method + " have more than one CompletionHandler type parameter");
|
||||||
}
|
}
|
||||||
Sncp.checkAsyncModifier(paramClasses[i], method);
|
Sncp.checkAsyncModifier(paramClasses[i], method);
|
||||||
handlerFuncIndex = i;
|
handlerFuncIndex = i;
|
||||||
@@ -372,7 +373,7 @@ public final class SncpDynServlet extends SncpServlet {
|
|||||||
intconst++;
|
intconst++;
|
||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
mv.visitFieldInsn(GETFIELD, newDynName, "convert", Type.getDescriptor(BsonConvert.class));
|
mv.visitFieldInsn(GETFIELD, newDynName, "convert", Type.getDescriptor(BsonConvert.class));
|
||||||
mv.visitLdcInsn(Type.getType(Type.getDescriptor(AsyncHandler.class)));
|
mv.visitLdcInsn(Type.getType(Type.getDescriptor(CompletionHandler.class)));
|
||||||
mv.visitVarInsn(ALOAD, 1);
|
mv.visitVarInsn(ALOAD, 1);
|
||||||
mv.visitMethodInsn(INVOKEVIRTUAL, convertName, "convertFrom", convertFromDesc, false);
|
mv.visitMethodInsn(INVOKEVIRTUAL, convertName, "convertFrom", convertFromDesc, false);
|
||||||
mv.visitInsn(POP);
|
mv.visitInsn(POP);
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ import org.redkale.util.*;
|
|||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
* 异步方法:
|
* 异步方法:
|
||||||
* Service编写异步方法:
|
* Service编写异步方法:
|
||||||
* 1、异步方法有且仅有一个类型为AsyncHandler的参数, 返回类型必须是void。若参数类型为AsyncHandler子类,必须保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数。
|
* 1、异步方法有且仅有一个类型为CompletionHandler的参数, 返回类型必须是void。若参数类型为CompletionHandler子类,必须保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数。
|
||||||
* 2、异步方法返回类型是CompletableFuture。
|
* 2、异步方法返回类型是CompletableFuture。
|
||||||
* 例如:
|
* 例如:
|
||||||
* public void insertRecord(AsyncHandler<Integer, Record> handler, String name, @RpcAttachment Record record);
|
* public void insertRecord(CompletionHandler<Integer, Record> handler, String name, @RpcAttachment Record record);
|
||||||
*
|
*
|
||||||
* </pre></blockquote>
|
* </pre></blockquote>
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
package org.redkale.util;
|
|
||||||
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
import java.nio.channels.CompletionHandler;
|
|
||||||
import java.util.function.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步回调函数
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
* @param <V> 结果对象的泛型
|
|
||||||
* @param <A> 附件对象的泛型
|
|
||||||
*/
|
|
||||||
public interface AsyncHandler<V, A> extends CompletionHandler<V, A> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建 AsyncHandler 对象
|
|
||||||
*
|
|
||||||
* @param <V> 结果对象的泛型
|
|
||||||
* @param <A> 附件对象的泛型
|
|
||||||
* @param success 成功的回调函数
|
|
||||||
* @param fail 失败的回调函数
|
|
||||||
*
|
|
||||||
* @return AsyncHandler
|
|
||||||
*/
|
|
||||||
public static <V, A> AsyncHandler<V, A> create(final BiConsumer<V, A> success, final BiConsumer<Throwable, A> fail) {
|
|
||||||
return new AsyncHandler<V, A>() {
|
|
||||||
@Override
|
|
||||||
public void completed(V result, A attachment) {
|
|
||||||
if (success != null) success.accept(result, attachment);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void failed(Throwable exc, A attachment) {
|
|
||||||
if (fail != null) fail.accept(exc, attachment);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建没有返回结果的 AsyncHandler 对象
|
|
||||||
*
|
|
||||||
* @param <A> 附件对象的泛型
|
|
||||||
* @param success 成功的回调函数
|
|
||||||
* @param fail 失败的回调函数
|
|
||||||
*
|
|
||||||
* @return AsyncHandler
|
|
||||||
*/
|
|
||||||
public static <A> AsyncHandler<Void, A> create(final Consumer<A> success, final BiConsumer<Throwable, A> fail) {
|
|
||||||
return new AsyncHandler<Void, A>() {
|
|
||||||
@Override
|
|
||||||
public void completed(Void result, A attachment) {
|
|
||||||
if (success != null) success.accept(attachment);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void failed(Throwable exc, A attachment) {
|
|
||||||
if (fail != null) fail.accept(exc, attachment);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建没有附件对象的 AsyncNoResultHandler 对象
|
|
||||||
*
|
|
||||||
* @param <V> 结果对象的泛型
|
|
||||||
* @param success 成功的回调函数
|
|
||||||
* @param fail 失败的回调函数
|
|
||||||
*
|
|
||||||
* @return AsyncHandler
|
|
||||||
*/
|
|
||||||
public static <V> AsyncHandler<V, Void> create(final Consumer<V> success, final Consumer<Throwable> fail) {
|
|
||||||
return new AsyncHandler<V, Void>() {
|
|
||||||
@Override
|
|
||||||
public void completed(V result, Void attachment) {
|
|
||||||
if (success != null) success.accept(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void failed(Throwable exc, Void attachment) {
|
|
||||||
if (fail != null) fail.accept(exc);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -8,10 +8,11 @@ import java.io.*;
|
|||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.nio.charset.*;
|
import java.nio.charset.*;
|
||||||
import java.time.*;
|
import java.time.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.*;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
import javax.net.ssl.*;
|
import javax.net.ssl.*;
|
||||||
|
|
||||||
@@ -458,6 +459,76 @@ public final class Utility {
|
|||||||
return back;
|
return back;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 CompletionHandler 对象
|
||||||
|
*
|
||||||
|
* @param <V> 结果对象的泛型
|
||||||
|
* @param <A> 附件对象的泛型
|
||||||
|
* @param success 成功的回调函数
|
||||||
|
* @param fail 失败的回调函数
|
||||||
|
*
|
||||||
|
* @return CompletionHandler
|
||||||
|
*/
|
||||||
|
public static <V, A> CompletionHandler<V, A> createAsyncHandler(final BiConsumer<V, A> success, final BiConsumer<Throwable, A> fail) {
|
||||||
|
return new CompletionHandler<V, A>() {
|
||||||
|
@Override
|
||||||
|
public void completed(V result, A attachment) {
|
||||||
|
if (success != null) success.accept(result, attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed(Throwable exc, A attachment) {
|
||||||
|
if (fail != null) fail.accept(exc, attachment);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建没有返回结果的 CompletionHandler 对象
|
||||||
|
*
|
||||||
|
* @param <A> 附件对象的泛型
|
||||||
|
* @param success 成功的回调函数
|
||||||
|
* @param fail 失败的回调函数
|
||||||
|
*
|
||||||
|
* @return CompletionHandler
|
||||||
|
*/
|
||||||
|
public static <A> CompletionHandler<Void, A> createAsyncHandler(final Consumer<A> success, final BiConsumer<Throwable, A> fail) {
|
||||||
|
return new CompletionHandler<Void, A>() {
|
||||||
|
@Override
|
||||||
|
public void completed(Void result, A attachment) {
|
||||||
|
if (success != null) success.accept(attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed(Throwable exc, A attachment) {
|
||||||
|
if (fail != null) fail.accept(exc, attachment);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建没有附件对象的 CompletionHandler 对象
|
||||||
|
*
|
||||||
|
* @param <V> 结果对象的泛型
|
||||||
|
* @param success 成功的回调函数
|
||||||
|
* @param fail 失败的回调函数
|
||||||
|
*
|
||||||
|
* @return CompletionHandler
|
||||||
|
*/
|
||||||
|
public static <V> CompletionHandler<V, Void> createAsyncHandler(final Consumer<V> success, final Consumer<Throwable> fail) {
|
||||||
|
return new CompletionHandler<V, Void>() {
|
||||||
|
@Override
|
||||||
|
public void completed(V result, Void attachment) {
|
||||||
|
if (success != null) success.accept(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed(Throwable exc, Void attachment) {
|
||||||
|
if (fail != null) fail.accept(exc);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取格式为yyyy-MM-dd HH:mm:ss的当前时间
|
* 获取格式为yyyy-MM-dd HH:mm:ss的当前时间
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ import java.io.*;
|
|||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
import org.redkale.net.http.*;
|
import org.redkale.net.http.*;
|
||||||
import org.redkale.util.AsyncHandler;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -28,11 +28,11 @@ public interface HttpResponseDesc {
|
|||||||
//增加Cookie值
|
//增加Cookie值
|
||||||
public HttpResponse addCookie(Collection<HttpCookie> cookies);
|
public HttpResponse addCookie(Collection<HttpCookie> cookies);
|
||||||
|
|
||||||
//创建AsyncHandler实例,将非字符串对象以JSON格式输出,字符串以文本输出
|
//创建CompletionHandler实例,将非字符串对象以JSON格式输出,字符串以文本输出
|
||||||
public AsyncHandler createAsyncHandler();
|
public CompletionHandler createAsyncHandler();
|
||||||
|
|
||||||
//传入的AsyncHandler子类必须是public,且保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数
|
//传入的CompletionHandler子类必须是public,且保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数
|
||||||
public <H extends AsyncHandler> H createAsyncHandler(Class<H> handlerClass);
|
public <H extends CompletionHandler> H createAsyncHandler(Class<H> handlerClass);
|
||||||
|
|
||||||
//设置状态码
|
//设置状态码
|
||||||
public void setStatus(int status);
|
public void setStatus(int status);
|
||||||
@@ -66,10 +66,10 @@ public interface HttpResponseDesc {
|
|||||||
public HttpResponse skipHeader();
|
public HttpResponse skipHeader();
|
||||||
|
|
||||||
//异步输出指定内容
|
//异步输出指定内容
|
||||||
public <A> void sendBody(ByteBuffer buffer, A attachment, AsyncHandler<Integer, A> handler);
|
public <A> void sendBody(ByteBuffer buffer, A attachment, CompletionHandler<Integer, A> handler);
|
||||||
|
|
||||||
//异步输出指定内容
|
//异步输出指定内容
|
||||||
public <A> void sendBody(ByteBuffer[] buffers, A attachment, AsyncHandler<Integer, A> handler);
|
public <A> void sendBody(ByteBuffer[] buffers, A attachment, CompletionHandler<Integer, A> handler);
|
||||||
|
|
||||||
//关闭HTTP连接,如果是keep-alive则不强制关闭
|
//关闭HTTP连接,如果是keep-alive则不强制关闭
|
||||||
public void finish();
|
public void finish();
|
||||||
|
|||||||
@@ -5,13 +5,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.rest;
|
package org.redkale.test.rest;
|
||||||
|
|
||||||
import org.redkale.util.AsyncHandler;
|
import java.nio.channels.CompletionHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
public class HelloAsyncHandler implements AsyncHandler {
|
public class HelloAsyncHandler implements CompletionHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void completed(Object result, Object attachment) {
|
public void completed(Object result, Object attachment) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.redkale.test.rest;
|
package org.redkale.test.rest;
|
||||||
|
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -96,7 +97,7 @@ public class HelloService implements Service {
|
|||||||
|
|
||||||
//异步查询单个
|
//异步查询单个
|
||||||
@RestMapping(name = "asyncfind2")
|
@RestMapping(name = "asyncfind2")
|
||||||
public void asyncFindHello(AsyncHandler hander, @RestParam(name = "#") int id) { //通过 /pipes/hello/find/1234、/pipes/hello/jsfind/1234 查询对象
|
public void asyncFindHello(CompletionHandler hander, @RestParam(name = "#") int id) { //通过 /pipes/hello/find/1234、/pipes/hello/jsfind/1234 查询对象
|
||||||
if (source != null) source.findAsync(HelloEntity.class, id);
|
if (source != null) source.findAsync(HelloEntity.class, id);
|
||||||
System.out.println("-----------进入asyncfind2--------" + hander);
|
System.out.println("-----------进入asyncfind2--------" + hander);
|
||||||
hander.completed(new HelloEntity(id), id);
|
hander.completed(new HelloEntity(id), id);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ package org.redkale.test.service;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.AsynchronousChannelGroup;
|
import java.nio.channels.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
@@ -165,8 +165,8 @@ public class ABMainService implements Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RestMapping(name = "asyncabtime")
|
@RestMapping(name = "asyncabtime")
|
||||||
public void abCurrentTime(final AsyncHandler<String, Void> handler, @RestParam(name = "#") final String name) {
|
public void abCurrentTime(final CompletionHandler<String, Void> handler, @RestParam(name = "#") final String name) {
|
||||||
bcService.bcCurrentTime(AsyncHandler.create((v, a) -> {
|
bcService.bcCurrentTime(Utility.createAsyncHandler((v, a) -> {
|
||||||
System.out.println("执行了 ABMainService.abCurrentTime----异步方法");
|
System.out.println("执行了 ABMainService.abCurrentTime----异步方法");
|
||||||
String rs = "异步abCurrentTime: " + v;
|
String rs = "异步abCurrentTime: " + v;
|
||||||
if (handler != null) handler.completed(rs, a);
|
if (handler != null) handler.completed(rs, a);
|
||||||
|
|||||||
@@ -5,9 +5,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.service;
|
package org.redkale.test.service;
|
||||||
|
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import org.redkale.service.*;
|
import org.redkale.service.*;
|
||||||
import org.redkale.util.AsyncHandler;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -24,8 +25,8 @@ public class BCService implements Service {
|
|||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void bcCurrentTime(final AsyncHandler<String, Void> handler, final String name) {
|
public void bcCurrentTime(final CompletionHandler<String, Void> handler, final String name) {
|
||||||
cService.ccCurrentTime(AsyncHandler.create((v, a) -> {
|
cService.ccCurrentTime(Utility.createAsyncHandler((v, a) -> {
|
||||||
System.out.println("执行了 BCService.bcCurrentTime----异步方法");
|
System.out.println("执行了 BCService.bcCurrentTime----异步方法");
|
||||||
String rs = "异步bcCurrentTime: " + (v == null ? null : v.getResult());
|
String rs = "异步bcCurrentTime: " + (v == null ? null : v.getResult());
|
||||||
if (handler != null) handler.completed(rs, null);
|
if (handler != null) handler.completed(rs, null);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.service;
|
package org.redkale.test.service;
|
||||||
|
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import org.redkale.service.*;
|
import org.redkale.service.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ public class CService implements Service {
|
|||||||
return new RetResult(rs);
|
return new RetResult(rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ccCurrentTime(final AsyncHandler<RetResult<String>, Void> handler, final String name) {
|
public void ccCurrentTime(final CompletionHandler<RetResult<String>, Void> handler, final String name) {
|
||||||
String rs = "异步ccCurrentTime: " + name + ": " + Utility.formatTime(System.currentTimeMillis());
|
String rs = "异步ccCurrentTime: " + name + ": " + Utility.formatTime(System.currentTimeMillis());
|
||||||
System.out.println("执行了 CService.ccCurrentTime----异步方法");
|
System.out.println("执行了 CService.ccCurrentTime----异步方法");
|
||||||
if (handler != null) handler.completed(new RetResult(rs), null);
|
if (handler != null) handler.completed(new RetResult(rs), null);
|
||||||
|
|||||||
@@ -5,13 +5,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.service;
|
package org.redkale.test.service;
|
||||||
|
|
||||||
import org.redkale.util.AsyncHandler;
|
import java.nio.channels.CompletionHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
public abstract class MyAsyncInnerHandler<V, A> implements AsyncHandler<V, A> {
|
public abstract class MyAsyncInnerHandler<V, A> implements CompletionHandler<V, A> {
|
||||||
|
|
||||||
protected abstract int id2();
|
protected abstract int id2();
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.service;
|
package org.redkale.test.service;
|
||||||
|
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import org.redkale.net.sncp.*;
|
import org.redkale.net.sncp.*;
|
||||||
import org.redkale.service.Service;
|
import org.redkale.service.Service;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
@@ -19,7 +20,7 @@ public class TestService implements Service {
|
|||||||
// return false;
|
// return false;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public void change(AsyncHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
|
public void change(CompletionHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package org.redkale.test.sncp;
|
|||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import org.redkale.net.TransportFactory;
|
import org.redkale.net.TransportFactory;
|
||||||
import org.redkale.net.sncp.*;
|
import org.redkale.net.sncp.*;
|
||||||
@@ -97,7 +98,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
|
|||||||
return "result: " + bean;
|
return "result: " + bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void queryResult(AsyncHandler<String, SncpTestBean> handler, @RpcAttachment SncpTestBean bean) {
|
public void queryResult(CompletionHandler<String, SncpTestBean> handler, @RpcAttachment SncpTestBean bean) {
|
||||||
System.out.println(Thread.currentThread().getName() + " handler 运行了queryResult方法");
|
System.out.println(Thread.currentThread().getName() + " handler 运行了queryResult方法");
|
||||||
if (handler != null) handler.completed("result: " + bean, bean);
|
if (handler != null) handler.completed("result: " + bean, bean);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user