ing
This commit is contained in:
parent
1c35dd6024
commit
e7ac429f2b
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -82,7 +82,7 @@ hs_err_pid*
|
|||
|
||||
.idea/
|
||||
*.iml
|
||||
/target/
|
||||
target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
|
|
6
pom.xml
6
pom.xml
|
@ -18,6 +18,7 @@
|
|||
|
||||
<properties>
|
||||
<netty.version>4.1.17.Final</netty.version>
|
||||
<spring.version>5.0.5.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -26,6 +27,11 @@
|
|||
<artifactId>netty-all</artifactId>
|
||||
<version>${netty.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
100
src/main/java/com/loafle/commons/server/Server.java
Normal file
100
src/main/java/com/loafle/commons/server/Server.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.Unpooled;
|
|
@ -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.Unpooled;
|
|
@ -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.Unpooled;
|
|
@ -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.Unpooled;
|
|
@ -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.Unpooled;
|
|
@ -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;
|
||||
|
|
@ -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.channel.ChannelHandlerContext;
|
|
@ -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.DefaultByteBufHolder;
|
|
@ -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.ChannelPipeline;
|
|
@ -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.ChannelPipeline;
|
|
@ -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.handler.codec.MessageToMessageDecoder;
|
|
@ -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.channel.ChannelFutureListener;
|
|
@ -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.Unpooled;
|
|
@ -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.Unpooled;
|
|
@ -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.handler.codec.CorruptedFrameException;
|
Loading…
Reference in New Issue
Block a user