提交 8dfbf12e 编写于 作者: G guide

Merge branch 'MrZhang-badminton-feature/20220306_beautify_code'

package github.javaguide.extension;
import github.javaguide.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
......@@ -51,7 +52,7 @@ public final class ExtensionLoader<T> {
}
public T getExtension(String name) {
if (name == null || name.isEmpty()) {
if (StringUtil.isBlank(name)) {
throw new IllegalArgumentException("Extension name should not be null or empty.");
}
// firstly get from cache, if not hit, create one
......
package github.javaguide.utils;
import java.util.Collection;
/**
* 集合工具类
*
* @author zhanghua
* @createTime 2022/3/6 12:58 上午
*/
public class CollectionUtil {
public static boolean isEmpty(Collection<?> c) {
return c == null || c.isEmpty();
}
}
package github.javaguide.utils;
/**
* String 工具类
*
* @author zhanghua
* @createTime 2022/3/6 12:58 上午
*/
public class StringUtil {
public static boolean isBlank(String s) {
if (s == null || s.length() == 0) {
return true;
}
for (int i = 0; i < s.length(); ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
}
......@@ -20,7 +20,7 @@ import java.util.concurrent.TimeUnit;
* @createTime 2020年05月26日 16:00:00
*/
@Slf4j
public final class ThreadPoolFactoryUtils {
public final class ThreadPoolFactoryUtil {
/**
* 通过 threadNamePrefix 来区分不同线程池(我们可以把相同 threadNamePrefix 的线程池看作是为同一业务场景服务)。
......@@ -29,7 +29,7 @@ public final class ThreadPoolFactoryUtils {
*/
private static final Map<String, ExecutorService> THREAD_POOLS = new ConcurrentHashMap<>();
private ThreadPoolFactoryUtils() {
private ThreadPoolFactoryUtil() {
}
......
......@@ -2,7 +2,7 @@ package github.javaguide.config;
import github.javaguide.registry.zk.util.CuratorUtils;
import github.javaguide.remoting.transport.netty.server.NettyRpcServer;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtils;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtil;
import lombok.extern.slf4j.Slf4j;
import java.net.InetAddress;
......@@ -31,7 +31,7 @@ public class CustomShutdownHook {
CuratorUtils.clearRegistry(CuratorUtils.getZkClient(), inetSocketAddress);
} catch (UnknownHostException ignored) {
}
ThreadPoolFactoryUtils.shutDownAllThreadPool();
ThreadPoolFactoryUtil.shutDownAllThreadPool();
}));
}
}
package github.javaguide.loadbalance;
import github.javaguide.remoting.dto.RpcRequest;
import github.javaguide.utils.CollectionUtil;
import java.util.List;
......@@ -13,7 +14,7 @@ import java.util.List;
public abstract class AbstractLoadBalance implements LoadBalance {
@Override
public String selectServiceAddress(List<String> serviceAddresses, RpcRequest rpcRequest) {
if (serviceAddresses == null || serviceAddresses.size() == 0) {
if (CollectionUtil.isEmpty(serviceAddresses)) {
return null;
}
if (serviceAddresses.size() == 1) {
......
......@@ -16,8 +16,9 @@ public interface LoadBalance {
/**
* Choose one from the list of existing service addresses list
*
* @param serviceAddresses Service address list
* @param serviceUrlList Service address list
* @param rpcRequest
* @return target service address
*/
String selectServiceAddress(List<String> serviceAddresses, RpcRequest rpcRequest);
String selectServiceAddress(List<String> serviceUrlList, RpcRequest rpcRequest);
}
......@@ -7,6 +7,7 @@ import github.javaguide.loadbalance.LoadBalance;
import github.javaguide.registry.ServiceDiscovery;
import github.javaguide.registry.zk.util.CuratorUtils;
import github.javaguide.remoting.dto.RpcRequest;
import github.javaguide.utils.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
......@@ -32,7 +33,7 @@ public class ZkServiceDiscoveryImpl implements ServiceDiscovery {
String rpcServiceName = rpcRequest.getRpcServiceName();
CuratorFramework zkClient = CuratorUtils.getZkClient();
List<String> serviceUrlList = CuratorUtils.getChildrenNodes(zkClient, rpcServiceName);
if (serviceUrlList == null || serviceUrlList.size() == 0) {
if (CollectionUtil.isEmpty(serviceUrlList)) {
throw new RpcException(RpcErrorMessageEnum.SERVICE_CAN_NOT_BE_FOUND, rpcServiceName);
}
// load balancing
......
......@@ -8,7 +8,7 @@ import github.javaguide.provider.impl.ZkServiceProviderImpl;
import github.javaguide.remoting.transport.netty.codec.RpcMessageDecoder;
import github.javaguide.remoting.transport.netty.codec.RpcMessageEncoder;
import github.javaguide.utils.RuntimeUtil;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtils;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
......@@ -56,7 +56,7 @@ public class NettyRpcServer {
EventLoopGroup workerGroup = new NioEventLoopGroup();
DefaultEventExecutorGroup serviceHandlerGroup = new DefaultEventExecutorGroup(
RuntimeUtil.cpus() * 2,
ThreadPoolFactoryUtils.createThreadFactory("service-handler-group", false)
ThreadPoolFactoryUtil.createThreadFactory("service-handler-group", false)
);
try {
ServerBootstrap b = new ServerBootstrap();
......
......@@ -5,7 +5,7 @@ import github.javaguide.config.RpcServiceConfig;
import github.javaguide.factory.SingletonFactory;
import github.javaguide.provider.ServiceProvider;
import github.javaguide.provider.impl.ZkServiceProviderImpl;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtils;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
......@@ -29,7 +29,7 @@ public class SocketRpcServer {
public SocketRpcServer() {
threadPool = ThreadPoolFactoryUtils.createCustomThreadPoolIfAbsent("socket-server-rpc-pool");
threadPool = ThreadPoolFactoryUtil.createCustomThreadPoolIfAbsent("socket-server-rpc-pool");
serviceProvider = SingletonFactory.getInstance(ZkServiceProviderImpl.class);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册