This commit is contained in:
@@ -14,6 +14,7 @@ import java.nio.file.*;
|
|||||||
import java.text.*;
|
import java.text.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.logging.Level;
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
import org.redkale.net.*;
|
import org.redkale.net.*;
|
||||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||||
@@ -200,6 +201,26 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建AsyncHandler实例,将非字符串对象以JSON格式输出,字符串以文本输出
|
||||||
|
*
|
||||||
|
* @return AsyncHandler
|
||||||
|
*/
|
||||||
|
public AsyncHandler createAsyncHandler() {
|
||||||
|
return AsyncHandler.create((v, a) -> {
|
||||||
|
if (v instanceof org.redkale.service.RetResult) {
|
||||||
|
finishJson((org.redkale.service.RetResult) v);
|
||||||
|
} else if (v instanceof CharSequence) {
|
||||||
|
finish(String.valueOf(v));
|
||||||
|
} else {
|
||||||
|
finishJson(v);
|
||||||
|
}
|
||||||
|
}, (t, a) -> {
|
||||||
|
request.getContext().getLogger().log(Level.WARNING, "Servlet occur, forece to close channel. request = " + request, t);
|
||||||
|
finish(500, null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象以JSON格式输出
|
* 将对象以JSON格式输出
|
||||||
*
|
*
|
||||||
@@ -457,7 +478,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
* @param attachment 异步回调参数
|
* @param attachment 异步回调参数
|
||||||
* @param handler 异步回调函数
|
* @param handler 异步回调函数
|
||||||
*/
|
*/
|
||||||
public <A> void sendBody(ByteBuffer buffer, A attachment, CompletionHandler<Integer, A> handler) {
|
public <A> void sendBody(ByteBuffer buffer, A attachment, AsyncHandler<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();
|
||||||
@@ -796,7 +817,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
this.bufferHandler = bufferHandler;
|
this.bufferHandler = bufferHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final class TransferFileHandler implements CompletionHandler<Integer, ByteBuffer> {
|
protected final class TransferFileHandler implements AsyncHandler<Integer, ByteBuffer> {
|
||||||
|
|
||||||
private final AsynchronousFileChannel filechannel;
|
private final AsynchronousFileChannel filechannel;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ package org.redkale.net.http;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
@@ -128,7 +127,7 @@ public abstract class WebSocketServlet extends HttpServlet {
|
|||||||
response.setHeader("Connection", "Upgrade");
|
response.setHeader("Connection", "Upgrade");
|
||||||
response.addHeader("Upgrade", "websocket");
|
response.addHeader("Upgrade", "websocket");
|
||||||
response.addHeader("Sec-WebSocket-Accept", key);
|
response.addHeader("Sec-WebSocket-Accept", key);
|
||||||
response.sendBody((ByteBuffer) null, null, new CompletionHandler<Integer, Void>() {
|
response.sendBody((ByteBuffer) null, null, new AsyncHandler<Integer, Void>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void completed(Integer result, Void attachment) {
|
public void completed(Integer result, Void attachment) {
|
||||||
|
|||||||
@@ -192,9 +192,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exists(final AsyncHandler<Boolean, K> handler, @RpcAttachment final K key) {
|
public boolean exists(final AsyncHandler<Boolean, K> handler, @RpcAttachment final K key) {
|
||||||
boolean rs = exists(key);
|
boolean rs = exists(key);
|
||||||
if (handler != null) handler.completed(rs, key);
|
if (handler != null) handler.completed(rs, key);
|
||||||
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -208,9 +209,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void get(final AsyncHandler<V, K> handler, @RpcAttachment final K key) {
|
public V get(final AsyncHandler<V, K> handler, @RpcAttachment final K key) {
|
||||||
V rs = get(key);
|
V rs = get(key);
|
||||||
if (handler != null) handler.completed(rs, key);
|
if (handler != null) handler.completed(rs, key);
|
||||||
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -227,9 +229,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getAndRefresh(final AsyncHandler<V, K> handler, @RpcAttachment final K key, final int expireSeconds) {
|
public V getAndRefresh(final AsyncHandler<V, K> handler, @RpcAttachment final K key, final int expireSeconds) {
|
||||||
V rs = getAndRefresh(key, expireSeconds);
|
V rs = getAndRefresh(key, expireSeconds);
|
||||||
if (handler != null) handler.completed(rs, key);
|
if (handler != null) handler.completed(rs, key);
|
||||||
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -324,9 +327,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getCollection(final AsyncHandler<Collection<V>, K> handler, @RpcAttachment final K key) {
|
public Collection<V> getCollection(final AsyncHandler<Collection<V>, K> handler, @RpcAttachment final K key) {
|
||||||
Collection<V> rs = getCollection(key);
|
Collection<V> rs = getCollection(key);
|
||||||
if (handler != null) handler.completed(rs, key);
|
if (handler != null) handler.completed(rs, key);
|
||||||
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -335,9 +339,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getCollectionAndRefresh(final AsyncHandler<Collection<V>, K> handler, @RpcAttachment final K key, final int expireSeconds) {
|
public Collection<V> getCollectionAndRefresh(final AsyncHandler<Collection<V>, K> handler, @RpcAttachment final K key, final int expireSeconds) {
|
||||||
Collection<V> rs = getCollectionAndRefresh(key, expireSeconds);
|
Collection<V> rs = getCollectionAndRefresh(key, expireSeconds);
|
||||||
if (handler != null) handler.completed(rs, key);
|
if (handler != null) handler.completed(rs, key);
|
||||||
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -53,11 +53,11 @@ public interface CacheSource<K extends Serializable, V extends Object> {
|
|||||||
public void removeSetItem(final K key, final V value);
|
public void removeSetItem(final K key, final V value);
|
||||||
|
|
||||||
//----------------------异步版---------------------------------
|
//----------------------异步版---------------------------------
|
||||||
public void exists(final AsyncHandler<Boolean, K> handler, final K key);
|
public boolean exists(final AsyncHandler<Boolean, K> handler, final K key);
|
||||||
|
|
||||||
public void get(final AsyncHandler<V, K> handler, final K key);
|
public V get(final AsyncHandler<V, K> handler, final K key);
|
||||||
|
|
||||||
public void getAndRefresh(final AsyncHandler<V, K> handler, final K key, final int expireSeconds);
|
public V getAndRefresh(final AsyncHandler<V, K> handler, final K key, final int expireSeconds);
|
||||||
|
|
||||||
public void refresh(final AsyncHandler<Void, K> handler, final K key, final int expireSeconds);
|
public void refresh(final AsyncHandler<Void, K> handler, final K key, final int expireSeconds);
|
||||||
|
|
||||||
@@ -69,9 +69,9 @@ public interface CacheSource<K extends Serializable, V extends Object> {
|
|||||||
|
|
||||||
public void remove(final AsyncHandler<Void, K> handler, final K key);
|
public void remove(final AsyncHandler<Void, K> handler, final K key);
|
||||||
|
|
||||||
public void getCollection(final AsyncHandler<Collection<V>, K> handler, final K key);
|
public Collection<V> getCollection(final AsyncHandler<Collection<V>, K> handler, final K key);
|
||||||
|
|
||||||
public void getCollectionAndRefresh(final AsyncHandler<Collection<V>, K> handler, final K key, final int expireSeconds);
|
public Collection<V> getCollectionAndRefresh(final AsyncHandler<Collection<V>, K> handler, final K key, final int expireSeconds);
|
||||||
|
|
||||||
public void appendListItem(final AsyncHandler<Void, K> handler, final K key, final V value);
|
public void appendListItem(final AsyncHandler<Void, K> handler, final K key, final V value);
|
||||||
|
|
||||||
@@ -81,7 +81,8 @@ public interface CacheSource<K extends Serializable, V extends Object> {
|
|||||||
|
|
||||||
public void removeSetItem(final AsyncHandler<Void, K> handler, final K key, final V value);
|
public void removeSetItem(final AsyncHandler<Void, K> handler, final K key, final V value);
|
||||||
|
|
||||||
default void isOpen(final AsyncHandler<Boolean, Void> handler) {
|
default boolean isOpen(final AsyncHandler<Boolean, Void> handler) {
|
||||||
if (handler != null) handler.completed(Boolean.TRUE, null);
|
if (handler != null) handler.completed(Boolean.TRUE, null);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ 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.*;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -59,8 +59,11 @@ public interface HttpResponseDesc {
|
|||||||
public HttpResponse skipHeader();
|
public HttpResponse skipHeader();
|
||||||
|
|
||||||
//异步输出指定内容
|
//异步输出指定内容
|
||||||
public <A> void sendBody(ByteBuffer buffer, A attachment, CompletionHandler<Integer, A> handler);
|
public <A> void sendBody(ByteBuffer buffer, A attachment, AsyncHandler<Integer, A> handler);
|
||||||
|
|
||||||
|
//创建AsyncHandler实例,将非字符串对象以JSON格式输出,字符串以文本输出
|
||||||
|
public AsyncHandler createAsyncHandler();
|
||||||
|
|
||||||
//关闭HTTP连接,如果是keep-alive则不强制关闭
|
//关闭HTTP连接,如果是keep-alive则不强制关闭
|
||||||
public void finish();
|
public void finish();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user