From 7df4249e1b7b729fff35f2a1632289b8966014eb Mon Sep 17 00:00:00 2001 From: xxlabaza Date: Fri, 6 Mar 2020 02:44:15 +0300 Subject: [PATCH] fix typos and make small refactoring --- .../client/builder/ConfigurerBootstrap.java | 4 +- .../builder/ConfigurerChannelInitializer.java | 4 +- .../ConfigurerNettyClientProperties.java | 3 +- .../NettyClientChannelInitializer.java | 79 +++++++++++++++++- .../NettyClientApplicationListener.java | 39 +++++---- .../builder/ConfigurerChannelInitializer.java | 4 +- .../NettyServerChannelInitializer.java | 80 ++++++++++++++++++- .../NettyServersApplicationListener.java | 39 +++++---- .../ChannelHandlerInitializerPipeline.java | 31 ++++++- .../substitution/TestConfiguration.java | 5 +- .../substitution/TestConfiguration.java | 5 +- 11 files changed, 238 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerBootstrap.java b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerBootstrap.java index f6a74ac..959a1bf 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerBootstrap.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerBootstrap.java @@ -68,8 +68,8 @@ private Optional fromConfig (NettyClientBuildContext context) { } private Optional fromContext (NettyClientBuildContext context) { - val properties = context.getBean(Bootstrap.class); - return ofNullable(properties); + val result = context.getBean(Bootstrap.class); + return ofNullable(result); } private Bootstrap create (NettyClientBuildContext context) { diff --git a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerChannelInitializer.java b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerChannelInitializer.java index f97b65c..6abb769 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerChannelInitializer.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerChannelInitializer.java @@ -16,6 +16,7 @@ package com.xxlabaza.utils.netty.config.client.builder; +import static com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline.pipelineOf; import static java.util.Optional.of; import static java.util.Optional.ofNullable; @@ -23,7 +24,6 @@ import java.util.Optional; import com.xxlabaza.utils.netty.config.BuildContextConfigurer; -import com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; @@ -72,6 +72,6 @@ private Optional> fromContext (NettyClientBuil private ChannelInitializer create (NettyClientBuildContext context) { val handlers = context.getBeansOfType(ChannelHandler.class); Collections.sort(handlers, AnnotationAwareOrderComparator.INSTANCE); - return ChannelHandlerInitializerPipeline.of(handlers); + return pipelineOf(handlers); } } diff --git a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerNettyClientProperties.java b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerNettyClientProperties.java index dc216aa..97e5d09 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerNettyClientProperties.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/ConfigurerNettyClientProperties.java @@ -53,7 +53,8 @@ public void configure (NettyClientBuildContext context) { } private Optional fromConfig (NettyClientBuildContext context) { - return ofNullable(context.config.getProperties()); + val result = context.config.getProperties(); + return ofNullable(result); } private Optional fromEnvironment (NettyClientBuildContext context) { diff --git a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/NettyClientChannelInitializer.java b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/NettyClientChannelInitializer.java index e0dcf14..b85083e 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/client/builder/NettyClientChannelInitializer.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/client/builder/NettyClientChannelInitializer.java @@ -16,14 +16,87 @@ package com.xxlabaza.utils.netty.config.client.builder; +import static lombok.AccessLevel.PRIVATE; + +import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; /** - * A marker class for easy default client context initializing. + * A marker delegate class for easy default client context initializing. */ -@SuppressWarnings("PMD.AbstractClassWithoutAnyMethod") -public abstract class NettyClientChannelInitializer extends ChannelInitializer { +@RequiredArgsConstructor +@FieldDefaults(level = PRIVATE, makeFinal = true) +public class NettyClientChannelInitializer extends ChannelInitializer { + + ChannelInitializer delegate; + + /** + * The constructor for inheritance purposes. + */ + protected NettyClientChannelInitializer () { + this(null); + } + + @Override + public void handlerAdded (ChannelHandlerContext context) throws Exception { + delegate.handlerAdded(context); + } + + @Override + public void handlerRemoved (ChannelHandlerContext context) throws Exception { + delegate.handlerRemoved(context); + } + + @Override + public void exceptionCaught (ChannelHandlerContext context, Throwable cause) throws Exception { + delegate.exceptionCaught(context, cause); + } + + @Override + public void channelUnregistered (ChannelHandlerContext context) throws Exception { + delegate.channelUnregistered(context); + } + + @Override + public void channelActive (ChannelHandlerContext context) throws Exception { + delegate.channelActive(context); + } + + @Override + public void channelInactive (ChannelHandlerContext context) throws Exception { + delegate.channelInactive(context); + } + + @Override + public void channelRead (ChannelHandlerContext context, Object message) throws Exception { + delegate.channelRead(context, message); + } + + @Override + public void channelReadComplete (ChannelHandlerContext context) throws Exception { + delegate.channelReadComplete(context); + } + + @Override + public void userEventTriggered (ChannelHandlerContext context, Object event) throws Exception { + delegate.userEventTriggered(context, event); + } + + @Override + public void channelWritabilityChanged (ChannelHandlerContext context) throws Exception { + delegate.channelWritabilityChanged(context); + } + @Override + public boolean isSharable () { + return delegate.isSharable(); + } + @Override + protected void initChannel (SocketChannel socketChannel) throws Exception { + // nothing + } } diff --git a/src/main/java/com/xxlabaza/utils/netty/config/client/lifecycle/NettyClientApplicationListener.java b/src/main/java/com/xxlabaza/utils/netty/config/client/lifecycle/NettyClientApplicationListener.java index 95485d9..39d647a 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/client/lifecycle/NettyClientApplicationListener.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/client/lifecycle/NettyClientApplicationListener.java @@ -23,28 +23,44 @@ import lombok.extern.slf4j.Slf4j; import lombok.val; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationContextEvent; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextStartedEvent; import org.springframework.context.event.ContextStoppedEvent; -import org.springframework.context.event.EventListener; +import org.springframework.core.Ordered; @Slf4j -class NettyClientApplicationListener { +class NettyClientApplicationListener implements ApplicationListener, Ordered { @Autowired(required = false) List clients; - @EventListener - void handle (ContextRefreshedEvent event) { + @Override + public int getOrder () { + return LOWEST_PRECEDENCE; + } + + @Override + public void onApplicationEvent (ApplicationContextEvent event) { + if (event instanceof ContextRefreshedEvent || event instanceof ContextStartedEvent) { + start(); + } else if (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent) { + stop(); + } + } + + void start () { if (clients == null) { + log.warn("didn't find any netty client bean"); return; } for (val client : clients) { val properties = client.getProperties(); if (client.isConnected() || properties.isAutoConnect() == false) { - return; + continue; } log.info("starting {}", client); @@ -52,8 +68,7 @@ void handle (ContextRefreshedEvent event) { } } - @EventListener - void handle (ContextClosedEvent event) { + void stop () { if (clients == null) { return; } @@ -67,14 +82,4 @@ void handle (ContextClosedEvent event) { client.close(); } } - - @EventListener - void handle (ContextStartedEvent event) { - handle((ContextRefreshedEvent) null); - } - - @EventListener - void handle (ContextStoppedEvent event) { - handle((ContextClosedEvent) null); - } } diff --git a/src/main/java/com/xxlabaza/utils/netty/config/server/builder/ConfigurerChannelInitializer.java b/src/main/java/com/xxlabaza/utils/netty/config/server/builder/ConfigurerChannelInitializer.java index 9cee21b..b67edc9 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/server/builder/ConfigurerChannelInitializer.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/server/builder/ConfigurerChannelInitializer.java @@ -16,6 +16,7 @@ package com.xxlabaza.utils.netty.config.server.builder; +import static com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline.pipelineOf; import static java.util.Optional.of; import static java.util.Optional.ofNullable; @@ -23,7 +24,6 @@ import java.util.Optional; import com.xxlabaza.utils.netty.config.BuildContextConfigurer; -import com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; @@ -72,6 +72,6 @@ private Optional> fromContext (NettyServerBuil private ChannelInitializer create (NettyServerBuildContext context) { val handlers = context.getBeansOfType(ChannelHandler.class); Collections.sort(handlers, AnnotationAwareOrderComparator.INSTANCE); - return ChannelHandlerInitializerPipeline.of(handlers); + return pipelineOf(handlers); } } diff --git a/src/main/java/com/xxlabaza/utils/netty/config/server/builder/NettyServerChannelInitializer.java b/src/main/java/com/xxlabaza/utils/netty/config/server/builder/NettyServerChannelInitializer.java index d4ef5f9..351cab1 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/server/builder/NettyServerChannelInitializer.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/server/builder/NettyServerChannelInitializer.java @@ -16,13 +16,87 @@ package com.xxlabaza.utils.netty.config.server.builder; +import static lombok.AccessLevel.PRIVATE; + +import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; /** - * A marker class for easy default server context initializing. + * A marker delegate class for easy default server context initializing. */ -@SuppressWarnings("PMD.AbstractClassWithoutAnyMethod") -public abstract class NettyServerChannelInitializer extends ChannelInitializer { +@RequiredArgsConstructor +@FieldDefaults(level = PRIVATE, makeFinal = true) +public class NettyServerChannelInitializer extends ChannelInitializer { + + ChannelInitializer delegate; + + /** + * The constructor for inheritance purposes. + */ + protected NettyServerChannelInitializer () { + this(null); + } + + @Override + public void handlerAdded (ChannelHandlerContext context) throws Exception { + delegate.handlerAdded(context); + } + + @Override + public void handlerRemoved (ChannelHandlerContext context) throws Exception { + delegate.handlerRemoved(context); + } + + @Override + public void exceptionCaught (ChannelHandlerContext context, Throwable cause) throws Exception { + delegate.exceptionCaught(context, cause); + } + + @Override + public void channelUnregistered (ChannelHandlerContext context) throws Exception { + delegate.channelUnregistered(context); + } + + @Override + public void channelActive (ChannelHandlerContext context) throws Exception { + delegate.channelActive(context); + } + + @Override + public void channelInactive (ChannelHandlerContext context) throws Exception { + delegate.channelInactive(context); + } + + @Override + public void channelRead (ChannelHandlerContext context, Object message) throws Exception { + delegate.channelRead(context, message); + } + + @Override + public void channelReadComplete (ChannelHandlerContext context) throws Exception { + delegate.channelReadComplete(context); + } + + @Override + public void userEventTriggered (ChannelHandlerContext context, Object event) throws Exception { + delegate.userEventTriggered(context, event); + } + + @Override + public void channelWritabilityChanged (ChannelHandlerContext context) throws Exception { + delegate.channelWritabilityChanged(context); + } + + @Override + public boolean isSharable () { + return delegate.isSharable(); + } + @Override + protected void initChannel (SocketChannel socketChannel) throws Exception { + // nothing + } } diff --git a/src/main/java/com/xxlabaza/utils/netty/config/server/lifecycle/NettyServersApplicationListener.java b/src/main/java/com/xxlabaza/utils/netty/config/server/lifecycle/NettyServersApplicationListener.java index c9d0331..8425303 100644 --- a/src/main/java/com/xxlabaza/utils/netty/config/server/lifecycle/NettyServersApplicationListener.java +++ b/src/main/java/com/xxlabaza/utils/netty/config/server/lifecycle/NettyServersApplicationListener.java @@ -23,28 +23,44 @@ import lombok.extern.slf4j.Slf4j; import lombok.val; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationContextEvent; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextStartedEvent; import org.springframework.context.event.ContextStoppedEvent; -import org.springframework.context.event.EventListener; +import org.springframework.core.PriorityOrdered; @Slf4j -class NettyServersApplicationListener { +class NettyServersApplicationListener implements ApplicationListener, PriorityOrdered { @Autowired(required = false) List servers; - @EventListener - void handle (ContextRefreshedEvent event) { + @Override + public int getOrder () { + return HIGHEST_PRECEDENCE; + } + + @Override + public void onApplicationEvent (ApplicationContextEvent event) { + if (event instanceof ContextRefreshedEvent || event instanceof ContextStartedEvent) { + start(); + } else if (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent) { + stop(); + } + } + + void start () { if (servers == null) { + log.warn("didn't find any netty server bean"); return; } for (val server : servers) { val properties = server.getProperties(); if (server.isRunning() || properties.isAutoStart() == false) { - return; + continue; } log.info("starting {}", server); @@ -52,8 +68,7 @@ void handle (ContextRefreshedEvent event) { } } - @EventListener - void handle (ContextClosedEvent event) { + void stop () { if (servers == null) { return; } @@ -67,14 +82,4 @@ void handle (ContextClosedEvent event) { server.close(); } } - - @EventListener - void handle (ContextStartedEvent event) { - handle((ContextRefreshedEvent) null); - } - - @EventListener - void handle (ContextStoppedEvent event) { - handle((ContextClosedEvent) null); - } } diff --git a/src/main/java/com/xxlabaza/utils/netty/handler/ChannelHandlerInitializerPipeline.java b/src/main/java/com/xxlabaza/utils/netty/handler/ChannelHandlerInitializerPipeline.java index 2952bee..a53a981 100644 --- a/src/main/java/com/xxlabaza/utils/netty/handler/ChannelHandlerInitializerPipeline.java +++ b/src/main/java/com/xxlabaza/utils/netty/handler/ChannelHandlerInitializerPipeline.java @@ -21,6 +21,9 @@ import java.util.Collection; +import com.xxlabaza.utils.netty.config.client.builder.NettyClientChannelInitializer; +import com.xxlabaza.utils.netty.config.server.builder.NettyServerChannelInitializer; + import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; @@ -29,19 +32,39 @@ import lombok.experimental.FieldDefaults; import lombok.val; -@AllArgsConstructor(access = PRIVATE) +@AllArgsConstructor @FieldDefaults(level = PRIVATE, makeFinal = true) public class ChannelHandlerInitializerPipeline extends ChannelInitializer { - public static ChannelInitializer of (ChannelHandler... handlers) { + public static ChannelInitializer pipelineOf (ChannelHandler... handlers) { val collection = asList(handlers); - return new ChannelHandlerInitializerPipeline(collection); + return pipelineOf(collection); } - public static ChannelInitializer of (Collection handlers) { + public static ChannelInitializer pipelineOf (Collection handlers) { return new ChannelHandlerInitializerPipeline(handlers); } + public static NettyClientChannelInitializer clientPipeline (ChannelHandler... handlers) { + val collection = asList(handlers); + return clientPipeline(collection); + } + + public static NettyClientChannelInitializer clientPipeline (Collection handlers) { + val delegate = pipelineOf(handlers); + return new NettyClientChannelInitializer(delegate); + } + + public static NettyServerChannelInitializer serverPipeline (ChannelHandler... handlers) { + val collection = asList(handlers); + return serverPipeline(collection); + } + + public static NettyServerChannelInitializer serverPipeline (Collection handlers) { + val delegate = pipelineOf(handlers); + return new NettyServerChannelInitializer(delegate); + } + @NonNull Collection handlers; diff --git a/src/test/java/com/xxlabaza/utils/netty/client/substitution/TestConfiguration.java b/src/test/java/com/xxlabaza/utils/netty/client/substitution/TestConfiguration.java index 6f47a8e..2c07421 100644 --- a/src/test/java/com/xxlabaza/utils/netty/client/substitution/TestConfiguration.java +++ b/src/test/java/com/xxlabaza/utils/netty/client/substitution/TestConfiguration.java @@ -16,8 +16,9 @@ package com.xxlabaza.utils.netty.client.substitution; +import static com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline.pipelineOf; + import com.xxlabaza.utils.netty.ApplicationContextHolder; -import com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline; import io.appulse.utils.Bytes; import io.netty.channel.ChannelHandler.Sharable; @@ -41,7 +42,7 @@ ApplicationContextHolder applicationContextHolder () { @Bean ChannelInitializer myClientChannelInititalizer () { - return ChannelHandlerInitializerPipeline.of(new HelloHandler()); + return pipelineOf(new HelloHandler()); } @Sharable diff --git a/src/test/java/com/xxlabaza/utils/netty/server/substitution/TestConfiguration.java b/src/test/java/com/xxlabaza/utils/netty/server/substitution/TestConfiguration.java index 063e839..8350fe7 100644 --- a/src/test/java/com/xxlabaza/utils/netty/server/substitution/TestConfiguration.java +++ b/src/test/java/com/xxlabaza/utils/netty/server/substitution/TestConfiguration.java @@ -16,8 +16,9 @@ package com.xxlabaza.utils.netty.server.substitution; +import static com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline.pipelineOf; + import com.xxlabaza.utils.netty.ApplicationContextHolder; -import com.xxlabaza.utils.netty.handler.ChannelHandlerInitializerPipeline; import io.appulse.utils.Bytes; import io.netty.channel.ChannelHandler.Sharable; @@ -41,7 +42,7 @@ ApplicationContextHolder applicationContextHolder () { @Bean ChannelInitializer myServerChannelInititalizer () { - return ChannelHandlerInitializerPipeline.of(new HelloHandler()); + return pipelineOf(new HelloHandler()); } @Sharable