This commit is contained in:
crusader 2018-04-24 01:16:25 +09:00
parent 1c35dd6024
commit e7ac429f2b
18 changed files with 122 additions and 16 deletions

2
.gitignore vendored
View File

@ -82,7 +82,7 @@ hs_err_pid*
.idea/ .idea/
*.iml *.iml
/target/ target/
.settings/ .settings/
.classpath .classpath
.project .project

View File

@ -18,6 +18,7 @@
<properties> <properties>
<netty.version>4.1.17.Final</netty.version> <netty.version>4.1.17.Final</netty.version>
<spring.version>5.0.5.RELEASE</spring.version>
</properties> </properties>
<dependencies> <dependencies>
@ -26,6 +27,11 @@
<artifactId>netty-all</artifactId> <artifactId>netty-all</artifactId>
<version>${netty.version}</version> <version>${netty.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,100 @@
package com.loafle.commons.server;
import java.net.SocketAddress;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.ssl.SslContext;
/**
* Server
*/
public abstract class Server {
@Value("${server.netty.thread.count.boss}")
protected int threadCountBoss;
@Value("${server.netty.thread.count.worker}")
protected int threadCountWorker;
@Autowired
protected SslContext sslContext;
@Autowired
protected List<Class<ServerChannel>> channelClasses;
@Autowired
protected List<ChannelOptionItem<?>> channelOptions;
@Autowired
protected List<ChannelHandler> channelHandlers;
@Autowired
protected ChannelInitializer<?> channelInitializer;
@Autowired
protected SocketAddress address;
public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(this.threadCountBoss);
EventLoopGroup workerGroup = new NioEventLoopGroup(this.threadCountWorker);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
if (null != this.channelClasses) {
for (Class<ServerChannel> channelClass : this.channelClasses) {
serverBootstrap.channel(channelClass);
}
}
if (null != this.channelOptions) {
for (ChannelOptionItem<?> optionItem : this.channelOptions) {
optionItem.setOption(serverBootstrap);
}
}
if (null != this.channelHandlers) {
for (ChannelHandler channelHandler : this.channelHandlers) {
serverBootstrap.handler(channelHandler);
}
}
serverBootstrap.childHandler(this.channelInitializer);
this.init();
ChannelFuture f = serverBootstrap.bind(address).sync();
this.onStart();
f.channel().closeFuture().sync();
} catch (Exception e) {
} finally {
this.onStop();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
this.destroy();
}
}
protected abstract void init() throws Exception;
protected abstract void onStart() throws Exception;
protected abstract void onStop() throws Exception;
protected abstract void destroy() throws Exception;
public static class ChannelOptionItem<T> {
private final ChannelOption<T> option;
private final T value;
public ChannelOptionItem(ChannelOption<T> option, T value) {
this.option = option;
this.value = value;
}
void setOption(ServerBootstrap serverBootstrap) {
serverBootstrap.option(option, value);
}
}
}

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import static io.netty.buffer.ByteBufUtil.readBytes; import static io.netty.buffer.ByteBufUtil.readBytes;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.DefaultByteBufHolder;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.channel.ChannelOutboundHandler; import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageDecoder;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;

View File

@ -1,4 +1,4 @@
package com.loafle.commons.server.socket.netty.handler.codec; package com.loafle.commons.server.socket.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.CorruptedFrameException;