This commit is contained in:
Redkale
2017-03-17 14:54:00 +08:00
parent 4b48f85162
commit b1d810188c
5 changed files with 47 additions and 18 deletions

View File

@@ -14,6 +14,7 @@ import java.nio.file.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import org.redkale.convert.json.JsonConvert;
import org.redkale.net.*;
import org.redkale.util.AnyValue.DefaultAnyValue;
@@ -200,6 +201,26 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
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格式输出
*
@@ -457,7 +478,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param attachment 异步回调参数
* @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.contentLength < 0) this.contentLength = buffer == null ? 0 : buffer.remaining();
ByteBuffer headbuf = createHeader();
@@ -796,7 +817,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
this.bufferHandler = bufferHandler;
}
protected final class TransferFileHandler implements CompletionHandler<Integer, ByteBuffer> {
protected final class TransferFileHandler implements AsyncHandler<Integer, ByteBuffer> {
private final AsynchronousFileChannel filechannel;

View File

@@ -8,7 +8,6 @@ package org.redkale.net.http;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.security.*;
import java.util.*;
import java.util.logging.*;
@@ -128,7 +127,7 @@ public abstract class WebSocketServlet extends HttpServlet {
response.setHeader("Connection", "Upgrade");
response.addHeader("Upgrade", "websocket");
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
public void completed(Integer result, Void attachment) {

View File

@@ -192,9 +192,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
}
@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);
if (handler != null) handler.completed(rs, key);
return rs;
}
@Override
@@ -208,9 +209,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
}
@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);
if (handler != null) handler.completed(rs, key);
return rs;
}
@Override
@@ -227,9 +229,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
}
@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);
if (handler != null) handler.completed(rs, key);
return rs;
}
@Override
@@ -324,9 +327,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
}
@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);
if (handler != null) handler.completed(rs, key);
return rs;
}
@Override
@@ -335,9 +339,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
}
@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);
if (handler != null) handler.completed(rs, key);
return rs;
}
@Override

View File

@@ -53,11 +53,11 @@ public interface CacheSource<K extends Serializable, V extends Object> {
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);
@@ -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 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);
@@ -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);
default void isOpen(final AsyncHandler<Boolean, Void> handler) {
default boolean isOpen(final AsyncHandler<Boolean, Void> handler) {
if (handler != null) handler.completed(Boolean.TRUE, null);
return true;
}
}

View File

@@ -9,11 +9,11 @@ import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.util.function.BiConsumer;
import org.redkale.convert.json.*;
import org.redkale.net.http.*;
import org.redkale.util.AsyncHandler;
/**
*
@@ -59,8 +59,11 @@ public interface HttpResponseDesc {
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则不强制关闭
public void finish();