use java8 functional
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-02-08 18:15:39 +08:00
parent 169bf6f290
commit d395b987e6
9 changed files with 69 additions and 39 deletions

View File

@@ -52,7 +52,7 @@ public class SdkClient extends AbstractClient {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline()
.addLast(new SdkClientDecoder())
.addLast(new SdkClientDecoder(Constants.DECODER_FRAMELENGTH)) // 如果长度不够会等待
.addLast(new SdkClientEncoder())
.addLast(new SdkClientHandler());
}

View File

@@ -29,27 +29,32 @@ import io.github.ehlxr.did.common.Try;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* @author ehlxr
* @since 2021-01-20 14:42.
*/
public class SdkClientDecoder extends ByteToMessageDecoder {
public class SdkClientDecoder extends FixedLengthFrameDecoder {
private final Logger logger = LoggerFactory.getLogger(SdkClientDecoder.class);
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
Try.of(() -> {
byte[] bytes = new byte[in.readableBytes()];
in.readBytes(bytes);
public SdkClientDecoder(int frameLength) {
super(frameLength);
}
out.add(NettyUtil.toObject(bytes));
}).trap(e -> logger.error("decode error", e)).run();
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
return Try.of(() -> {
ByteBuf decode = (ByteBuf) super.decode(ctx, in);
byte[] bytes = new byte[decode.readableBytes()];
decode.readBytes(bytes);
decode.release();
return NettyUtil.toObject(bytes);
}).trap(e -> logger.error("decode error", e)).get();
}
@Override

View File

@@ -44,7 +44,6 @@ public class SdkClientEncoder extends MessageToByteEncoder<SdkProto> {
@Override
protected void encode(ChannelHandlerContext ctx, SdkProto sdkProto, ByteBuf out) {
System.out.println("-------------");
Try.of(() -> {
out.writeBytes(NettyUtil.toBytes(sdkProto));
}).trap(e -> logger.error("encode error", e)).run();

View File

@@ -1,10 +1,13 @@
package io.github.ehlxr.did;
import io.github.ehlxr.did.client.SdkClient;
import io.github.ehlxr.did.common.Try;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
/**
@@ -31,19 +34,19 @@ public class DidSdkTest {
// 测试同步请求
IntStream.range(0, NUM).parallel().forEach(i -> System.out.println(client.invokeSync()));
// System.out.println("invokeync test finish");
System.out.println("invokeync test finish");
// 测试异步请求
// final CountDownLatch countDownLatch = new CountDownLatch(NUM);
// IntStream.range(0, NUM).forEach(i ->
// Try.of(() -> client.invokeAsync(responseFuture -> {
// System.out.println(responseFuture.getSdkProto());
// countDownLatch.countDown();
// })).trap(Throwable::printStackTrace).run());
//
// //noinspection ResultOfMethodCallIgnored
// countDownLatch.await(10, TimeUnit.SECONDS);
// System.out.println("invokeAsync test finish");
final CountDownLatch countDownLatch = new CountDownLatch(NUM);
IntStream.range(0, NUM).forEach(i ->
Try.of(() -> client.invokeAsync(responseFuture -> {
System.out.println(responseFuture.getSdkProto());
countDownLatch.countDown();
})).trap(Throwable::printStackTrace).run());
//noinspection ResultOfMethodCallIgnored
countDownLatch.await(10, TimeUnit.SECONDS);
System.out.println("invokeAsync test finish");
}
@Test