Skip to content

Commit

Permalink
fix typos and make small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
xxlabaza committed Mar 5, 2020
1 parent 4189dd6 commit 7df4249
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ private Optional<Bootstrap> fromConfig (NettyClientBuildContext context) {
}

private Optional<Bootstrap> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

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;

import java.util.Collections;
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;
Expand Down Expand Up @@ -72,6 +72,6 @@ private Optional<ChannelInitializer<SocketChannel>> fromContext (NettyClientBuil
private ChannelInitializer<SocketChannel> create (NettyClientBuildContext context) {
val handlers = context.getBeansOfType(ChannelHandler.class);
Collections.sort(handlers, AnnotationAwareOrderComparator.INSTANCE);
return ChannelHandlerInitializerPipeline.of(handlers);
return pipelineOf(handlers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void configure (NettyClientBuildContext context) {
}

private Optional<NettyClientProperties> fromConfig (NettyClientBuildContext context) {
return ofNullable(context.config.getProperties());
val result = context.config.getProperties();
return ofNullable(result);
}

private Optional<NettyClientProperties> fromEnvironment (NettyClientBuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketChannel> {
@RequiredArgsConstructor
@FieldDefaults(level = PRIVATE, makeFinal = true)
public class NettyClientChannelInitializer extends ChannelInitializer<SocketChannel> {

ChannelInitializer<SocketChannel> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,52 @@
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<ApplicationContextEvent>, Ordered {

@Autowired(required = false)
List<NettyClient> 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);
client.connect();
}
}

@EventListener
void handle (ContextClosedEvent event) {
void stop () {
if (clients == null) {
return;
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

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;

import java.util.Collections;
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;
Expand Down Expand Up @@ -72,6 +72,6 @@ private Optional<ChannelInitializer<SocketChannel>> fromContext (NettyServerBuil
private ChannelInitializer<SocketChannel> create (NettyServerBuildContext context) {
val handlers = context.getBeansOfType(ChannelHandler.class);
Collections.sort(handlers, AnnotationAwareOrderComparator.INSTANCE);
return ChannelHandlerInitializerPipeline.of(handlers);
return pipelineOf(handlers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketChannel> {
@RequiredArgsConstructor
@FieldDefaults(level = PRIVATE, makeFinal = true)
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

ChannelInitializer<SocketChannel> 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
}
}
Loading

0 comments on commit 7df4249

Please sign in to comment.