优化部分代码

master
剑器近 2021-11-10 16:52:26 +08:00
parent 6339e56b43
commit 9131645204
11 changed files with 26 additions and 35 deletions

View File

@ -33,8 +33,8 @@ public class TCPServer {
private EventLoopGroup bossGroup = null;
private EventLoopGroup workerGroup = null;
private String name;
private NettyConfig config;
private final String name;
private final NettyConfig config;
public TCPServer(String name, NettyConfig config) {
this.name = name;
@ -53,8 +53,8 @@ public class TCPServer {
.childOption(NioChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
private MessageEncoderWrapper messageEncoderWrapper = new MessageEncoderWrapper(config.encoder);
private MessageDecoderWrapper messageDecoderWrapper = new MessageDecoderWrapper(config.decoder);
private final MessageEncoderWrapper messageEncoderWrapper = new MessageEncoderWrapper(config.encoder);
private final MessageDecoderWrapper messageDecoderWrapper = new MessageDecoderWrapper(config.decoder);
@Override
public void initChannel(NioSocketChannel channel) {
@ -71,7 +71,7 @@ public class TCPServer {
log.warn("==={}启动成功, port={}===", name, config.port);
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
log.warn("==={}出现异常, port={}===", e);
log.warn("==={}出现异常, port={}===", e, config.port);
} finally {
stop();
}

View File

@ -27,13 +27,13 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(TCPServerHandler.class.getSimpleName());
private HandlerMapping handlerMapping;
private final HandlerMapping handlerMapping;
private HandlerInterceptor interceptor;
private final HandlerInterceptor interceptor;
private SessionManager sessionManager;
private final SessionManager sessionManager;
private SessionListener sessionListener;
private final SessionListener sessionListener;
public TCPServerHandler(HandlerMapping handlerMapping,
HandlerInterceptor interceptor,

View File

@ -40,7 +40,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
}
}
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) {
// Try all delimiters and choose the delimiter which yields the shortest frame.
int minFrameLength = Integer.MAX_VALUE;
Delimiter minDelim = null;

View File

@ -100,7 +100,7 @@ public class LengthFieldAndDelimiterFrameDecoder extends DelimiterBasedFrameDeco
throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less than initialBytesToStrip: " + initialBytesToStrip);
}
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
if (in.readableBytes() < this.lengthFieldEndOffset) {
return null;
} else {

View File

@ -16,7 +16,7 @@ import io.netty.handler.codec.DecoderException;
@ChannelHandler.Sharable
public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter {
private MessageDecoder decoder;
private final MessageDecoder decoder;
public MessageDecoderWrapper(MessageDecoder decoder) {
this.decoder = decoder;

View File

@ -17,7 +17,7 @@ import io.netty.handler.codec.EncoderException;
@ChannelHandler.Sharable
public class MessageEncoderWrapper extends ChannelOutboundHandlerAdapter {
private MessageEncoder encoder;
private final MessageEncoder encoder;
public MessageEncoderWrapper(MessageEncoder encoder) {
this.encoder = encoder;

View File

@ -17,7 +17,7 @@ import java.util.Map;
*/
public abstract class AbstractHandlerMapping implements HandlerMapping {
private final Map<Object, Handler> handlerMap = new HashMap(60);
private final Map<Object, Handler> handlerMap = new HashMap<>(64);
/**
* Endpoint@Mapping
@ -25,8 +25,6 @@ public abstract class AbstractHandlerMapping implements HandlerMapping {
protected synchronized void registerHandlers(Object bean) {
Class<?> beanClass = bean.getClass();
Method[] methods = beanClass.getDeclaredMethods();
if (methods == null)
return;
for (Method method : methods) {

View File

@ -23,17 +23,17 @@ public class AsyncBatchHandler extends Handler {
private static final Logger log = LoggerFactory.getLogger(AsyncBatchHandler.class.getSimpleName());
private ConcurrentLinkedQueue<Message> queue;
private final ConcurrentLinkedQueue<Message> queue;
private ThreadPoolExecutor executor;
private final ThreadPoolExecutor executor;
private int poolSize;
private final int poolSize;
private int maxElements;
private final int maxElements;
private int maxWait;
private final int maxWait;
private int warningLines;
private final int warningLines;
public AsyncBatchHandler(Object actionClass, Method actionMethod, String desc, int poolSize, int maxElements, int maxWait) {
super(actionClass, actionMethod, desc);
@ -50,8 +50,7 @@ public class AsyncBatchHandler extends Handler {
this.warningLines = maxElements * poolSize * 50;
this.queue = new ConcurrentLinkedQueue();
this.executor = new ThreadPoolExecutor(this.poolSize, this.poolSize, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(400),
new BasicThreadFactory.Builder().daemon(true).namingPattern(actionMethod.getName() + "-pool-%d").build());
this.executor = new ThreadPoolExecutor(this.poolSize, this.poolSize, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(400), new BasicThreadFactory.Builder().daemon(true).namingPattern(actionMethod.getName() + "-pool-%d").build());
for (int i = 0; i < poolSize; i++) {
boolean master = i == 0;
@ -65,7 +64,7 @@ public class AsyncBatchHandler extends Handler {
}
}
public Message invoke(Message request, Session session) {
public <T extends Message> T invoke(T request, Session session) {
queue.offer(request);
return null;
}

View File

@ -34,7 +34,7 @@ public abstract class Handler {
try {
for (int i = 0; i < types.length; i++) {
Type type = types[i];
Class clazz;
Class<?> clazz;
if (type instanceof ParameterizedTypeImpl)
clazz = (Class<?>) ((ParameterizedTypeImpl) type).getActualTypeArguments()[0];
else

View File

@ -16,7 +16,7 @@ public class SimpleHandler extends Handler {
super(actionClass, actionMethod, desc);
}
public Message invoke(Message request, Session session) throws Exception {
public <T extends Message> T invoke(T request, Session session) throws Exception {
return super.invoke(request, session);
}
}

View File

@ -8,7 +8,6 @@ import java.lang.annotation.Annotation;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
@ -24,17 +23,12 @@ public class ClassUtils {
public static List<Class<?>> getClassList(String packageName, Class<? extends Annotation> annotationClass) {
List<Class<?>> classList = getClassList(packageName);
Iterator<Class<?>> iterator = classList.iterator();
while (iterator.hasNext()) {
Class<?> next = iterator.next();
if (!next.isAnnotationPresent(annotationClass))
iterator.remove();
}
classList.removeIf(next -> !next.isAnnotationPresent(annotationClass));
return classList;
}
public static List<Class<?>> getClassList(String packageName) {
List<Class<?>> classList = new LinkedList();
List<Class<?>> classList = new LinkedList<>();
String path = packageName.replace(".", "/");
try {
Enumeration<URL> urls = ClassUtils.getClassLoader().getResources(path);