From 1ff60d6808d6270229cf6f83c2b0b342f887f6ee Mon Sep 17 00:00:00 2001 From: ehlxr Date: Mon, 18 Jan 2021 16:17:14 +0800 Subject: [PATCH] update at 2021-01-18 16:17:14 by ehlxr --- .../java/cn/ceres/did/common/Constants.java | 2 +- .../main/java/cn/ceres/did/sdk/SdkProto.java | 2 +- .../main/java/cn/ceres/did/ServerStarter.java | 8 +-- .../java/cn/ceres/did/core/SnowFlake.java | 4 +- .../java/cn/ceres/did/server/BaseServer.java | 6 +-- .../cn/ceres/did/server/http/HttpServer.java | 4 +- .../did/server/http/HttpServerHandler.java | 4 +- .../cn/ceres/did/server/sdk/SdkServer.java | 2 +- .../did/server/sdk/SdkServerHandler.java | 52 +++++++++---------- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/did-common/src/main/java/cn/ceres/did/common/Constants.java b/did-common/src/main/java/cn/ceres/did/common/Constants.java index 85c48d0..9eaf980 100644 --- a/did-common/src/main/java/cn/ceres/did/common/Constants.java +++ b/did-common/src/main/java/cn/ceres/did/common/Constants.java @@ -6,7 +6,7 @@ import java.util.Map; * @author ehlxr */ public class Constants { - private static Map SYS_ENV = System.getenv(); + private static final Map SYS_ENV = System.getenv(); public static String getEnv(String key) { return SYS_ENV.get(key) == null ? "" : SYS_ENV.get(key); diff --git a/did-sdk/src/main/java/cn/ceres/did/sdk/SdkProto.java b/did-sdk/src/main/java/cn/ceres/did/sdk/SdkProto.java index a1b5a9a..22303a4 100644 --- a/did-sdk/src/main/java/cn/ceres/did/sdk/SdkProto.java +++ b/did-sdk/src/main/java/cn/ceres/did/sdk/SdkProto.java @@ -6,7 +6,7 @@ import java.util.concurrent.atomic.AtomicInteger; * @author ehlxr */ public class SdkProto { - private static AtomicInteger requestId = new AtomicInteger(0); + private static final AtomicInteger requestId = new AtomicInteger(0); /** * 请求的ID diff --git a/did-server/src/main/java/cn/ceres/did/ServerStarter.java b/did-server/src/main/java/cn/ceres/did/ServerStarter.java index dd4399d..71a9ca4 100644 --- a/did-server/src/main/java/cn/ceres/did/ServerStarter.java +++ b/did-server/src/main/java/cn/ceres/did/ServerStarter.java @@ -20,12 +20,12 @@ public class ServerStarter { long machineId = Constants.MACHINES_ID; if (args != null && args.length == 2) { - datacenterId = Long.valueOf(args[0]); - machineId = Long.valueOf(args[1]); + datacenterId = Long.parseLong(args[0]); + machineId = Long.parseLong(args[1]); } - datacenterId = "".equals(Constants.getEnv("DATACENTER_ID")) ? datacenterId : Long.valueOf(Constants.getEnv("DATACENTER_ID")); - machineId = "".equals(Constants.getEnv("MACHINES_ID")) ? machineId : Long.valueOf(Constants.getEnv("MACHINES_ID")); + datacenterId = "".equals(Constants.getEnv("DATACENTER_ID")) ? datacenterId : Long.parseLong(Constants.getEnv("DATACENTER_ID")); + machineId = "".equals(Constants.getEnv("MACHINES_ID")) ? machineId : Long.parseLong(Constants.getEnv("MACHINES_ID")); logger.info("SnowFlake datacenterId is: {}, machineId is: {}", datacenterId, machineId); final SnowFlake snowFlake = new SnowFlake(datacenterId, machineId); diff --git a/did-server/src/main/java/cn/ceres/did/core/SnowFlake.java b/did-server/src/main/java/cn/ceres/did/core/SnowFlake.java index a855935..e824d67 100644 --- a/did-server/src/main/java/cn/ceres/did/core/SnowFlake.java +++ b/did-server/src/main/java/cn/ceres/did/core/SnowFlake.java @@ -58,11 +58,11 @@ public class SnowFlake { /** * 数据中心 */ - private long datacenterId; + private final long datacenterId; /** * 机器标识 */ - private long machineId; + private final long machineId; /** * 序列号 */ diff --git a/did-server/src/main/java/cn/ceres/did/server/BaseServer.java b/did-server/src/main/java/cn/ceres/did/server/BaseServer.java index b1128b9..7a2945c 100644 --- a/did-server/src/main/java/cn/ceres/did/server/BaseServer.java +++ b/did-server/src/main/java/cn/ceres/did/server/BaseServer.java @@ -25,7 +25,7 @@ public abstract class BaseServer implements Server { public void init() { defLoopGroup = new DefaultEventLoopGroup(8, new ThreadFactory() { - private AtomicInteger index = new AtomicInteger(0); + private final AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { @@ -33,7 +33,7 @@ public abstract class BaseServer implements Server { } }); bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { - private AtomicInteger index = new AtomicInteger(0); + private final AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { @@ -41,7 +41,7 @@ public abstract class BaseServer implements Server { } }); workGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 10, new ThreadFactory() { - private AtomicInteger index = new AtomicInteger(0); + private final AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { diff --git a/did-server/src/main/java/cn/ceres/did/server/http/HttpServer.java b/did-server/src/main/java/cn/ceres/did/server/http/HttpServer.java index 123b8be..5964ac0 100644 --- a/did-server/src/main/java/cn/ceres/did/server/http/HttpServer.java +++ b/did-server/src/main/java/cn/ceres/did/server/http/HttpServer.java @@ -21,11 +21,11 @@ import java.net.InetSocketAddress; * @author ehlxr */ public class HttpServer extends BaseServer { - private SnowFlake snowFlake; + private final SnowFlake snowFlake; public HttpServer(SnowFlake snowFlake) { this.snowFlake = snowFlake; - this.port = "".equals(Constants.getEnv("HTTP_PORT")) ? Constants.HTTP_PORT : Integer.valueOf(Constants.getEnv("HTTP_PORT")); + this.port = "".equals(Constants.getEnv("HTTP_PORT")) ? Constants.HTTP_PORT : Integer.parseInt(Constants.getEnv("HTTP_PORT")); } @Override diff --git a/did-server/src/main/java/cn/ceres/did/server/http/HttpServerHandler.java b/did-server/src/main/java/cn/ceres/did/server/http/HttpServerHandler.java index 0a74777..c01b65c 100644 --- a/did-server/src/main/java/cn/ceres/did/server/http/HttpServerHandler.java +++ b/did-server/src/main/java/cn/ceres/did/server/http/HttpServerHandler.java @@ -27,8 +27,8 @@ public class HttpServerHandler extends SimpleChannelInboundHandler { private static final Logger logger = LoggerFactory.getLogger(SdkServerHandler.class); /** * 通过信号量来控制流量 */ - private Semaphore semaphore = new Semaphore(Constants.HANDLE_SDKS_TPS); - private SnowFlake snowFlake; + private final Semaphore semaphore = new Semaphore(Constants.HANDLE_SDKS_TPS); + private final SnowFlake snowFlake; SdkServerHandler(SnowFlake snowFlake) { this.snowFlake = snowFlake; } @Override - protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof SdkProto) { - SdkProto sdkProto = (SdkProto) msg; - if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) { - try { - sdkProto.setDid(snowFlake.nextId()); - ctx.channel().writeAndFlush(sdkProto).addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture channelFuture) { - semaphore.release(); - } - }); - } catch (Exception e) { - semaphore.release(); - logger.error("SdkServerhandler error", e); - } - } else { - sdkProto.setDid(-1); - ctx.channel().writeAndFlush(sdkProto); - String info = String.format("SdkServerHandler tryAcquire semaphore timeout, %dms, waiting thread " + "nums: %d availablePermit: %d", - Constants.ACQUIRE_TIMEOUTMILLIS, this.semaphore.getQueueLength(), this.semaphore.availablePermits()); - logger.warn(info); - throw new Exception(info); + protected void channelRead0(ChannelHandlerContext ctx, SdkProto sdkProto) throws Exception { + // if (msg instanceof SdkProto) { + // SdkProto sdkProto = (SdkProto) msg; + if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) { + try { + sdkProto.setDid(snowFlake.nextId()); + ctx.channel().writeAndFlush(sdkProto).addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) { + semaphore.release(); + } + }); + } catch (Exception e) { + semaphore.release(); + logger.error("SdkServerhandler error", e); } + } else { + sdkProto.setDid(-1); + ctx.channel().writeAndFlush(sdkProto); + String info = String.format("SdkServerHandler tryAcquire semaphore timeout, %dms, waiting thread " + "nums: %d availablePermit: %d", + Constants.ACQUIRE_TIMEOUTMILLIS, this.semaphore.getQueueLength(), this.semaphore.availablePermits()); + logger.warn(info); + throw new Exception(info); } + // } } @Override