Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
byit-myth-job
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liyuan
byit-myth-job
Commits
d24f878a
Commit
d24f878a
authored
Apr 03, 2020
by
huangfusuper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Netty服务端 main方式与异步线程方式的实现 编写完毕
parent
e6e970f1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
164 additions
and
0 deletions
+164
-0
MainNettyPluginServer.java
...ain/java/com/byit/server/netty/MainNettyPluginServer.java
+80
-0
NettyPluginServer.java
...rc/main/java/com/byit/server/netty/NettyPluginServer.java
+84
-0
No files found.
byit-plugin-core/myth-plugin-rpc-server/src/main/java/com/byit/server/netty/MainNettyPluginServer.java
0 → 100644
View file @
d24f878a
package
com
.
byit
.
server
.
netty
;
import
com.byit.factory.PluginServerFactory
;
import
com.byit.handler.PackerSpliterHandler
;
import
com.byit.handler.PacketDecodeHandler
;
import
com.byit.handler.PacketEncodeHandler
;
import
com.byit.server.PluginServer
;
import
com.byit.server.netty.handler.NettyPluginServerHandler
;
import
com.byit.utils.ThreadPoolUtil
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.*
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
import
java.util.concurrent.ThreadPoolExecutor
;
/**
* main方法启动
* @author huangfu
*/
public
class
MainNettyPluginServer
extends
PluginServer
{
@Override
public
void
start
(
PluginServerFactory
pluginServerFactory
)
{
ThreadPoolExecutor
threadPoolExecutor
=
ThreadPoolUtil
.
makeServerThreadPool
(
NettyPluginServer
.
class
.
getName
(),
pluginServerFactory
.
getCorePoolSize
(),
pluginServerFactory
.
getMaxPoolSize
());
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
();
EventLoopGroup
workGroup
=
new
NioEventLoopGroup
();
try
{
ServerBootstrap
serverBootstrap
=
new
ServerBootstrap
();
serverBootstrap
.
group
(
bossGroup
,
workGroup
)
.
channel
(
NioServerSocketChannel
.
class
)
.
childOption
(
ChannelOption
.
TCP_NODELAY
,
true
)
.
childOption
(
ChannelOption
.
SO_KEEPALIVE
,
true
)
.
childHandler
(
new
ChannelInitializer
<
SocketChannel
>()
{
@Override
protected
void
initChannel
(
SocketChannel
ch
)
{
ChannelPipeline
pipeline
=
ch
.
pipeline
();
pipeline
.
addLast
(
"packerSpliterHandler"
,
new
PackerSpliterHandler
());
pipeline
.
addLast
(
"packetDecodeHandler"
,
new
PacketDecodeHandler
());
pipeline
.
addLast
(
"nettyPluginServerHandler"
,
new
NettyPluginServerHandler
(
pluginServerFactory
,
threadPoolExecutor
));
pipeline
.
addLast
(
"packetEncodeHandler"
,
new
PacketEncodeHandler
());
}
});
ChannelFuture
channelFuture
=
serverBootstrap
.
bind
(
pluginServerFactory
.
getPort
()).
sync
();
super
.
onStart
();
ChannelFuture
closeFuture
=
channelFuture
.
channel
().
closeFuture
().
sync
();
closeFuture
.
addListener
(
future
->{
if
(
future
.
isSuccess
())
{
System
.
out
.
println
(
"-----------------"
);
}
});
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
try
{
threadPoolExecutor
.
shutdown
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
try
{
bossGroup
.
shutdownGracefully
();
workGroup
.
shutdownGracefully
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
}
@Override
public
void
stop
()
{
System
.
out
.
println
(
"-------------"
);
super
.
onStop
();
}
}
byit-plugin-core/myth-plugin-rpc-server/src/main/java/com/byit/server/netty/NettyPluginServer.java
0 → 100644
View file @
d24f878a
package
com
.
byit
.
server
.
netty
;
import
com.byit.factory.PluginServerFactory
;
import
com.byit.handler.PackerSpliterHandler
;
import
com.byit.handler.PacketDecodeHandler
;
import
com.byit.handler.PacketEncodeHandler
;
import
com.byit.server.PluginServer
;
import
com.byit.server.netty.handler.NettyPluginServerHandler
;
import
com.byit.utils.ThreadPoolUtil
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.*
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
import
java.util.concurrent.ThreadPoolExecutor
;
/**
* Netty服务启动
* @author huangfu
*/
public
class
NettyPluginServer
extends
PluginServer
{
private
Thread
serverThread
;
@Override
public
void
start
(
PluginServerFactory
pluginServerFactory
)
{
serverThread
=
new
Thread
(()
->{
final
ThreadPoolExecutor
threadPoolExecutor
=
ThreadPoolUtil
.
makeServerThreadPool
(
NettyPluginServer
.
class
.
getName
(),
pluginServerFactory
.
getCorePoolSize
(),
pluginServerFactory
.
getMaxPoolSize
());
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
();
EventLoopGroup
workGroup
=
new
NioEventLoopGroup
();
try
{
ServerBootstrap
serverBootstrap
=
new
ServerBootstrap
();
serverBootstrap
.
group
(
bossGroup
,
workGroup
)
.
channel
(
NioServerSocketChannel
.
class
)
.
childOption
(
ChannelOption
.
TCP_NODELAY
,
true
)
.
childOption
(
ChannelOption
.
SO_KEEPALIVE
,
true
)
.
childHandler
(
new
ChannelInitializer
<
SocketChannel
>()
{
@Override
protected
void
initChannel
(
SocketChannel
ch
)
{
ChannelPipeline
pipeline
=
ch
.
pipeline
();
pipeline
.
addLast
(
"packerSpliterHandler"
,
new
PackerSpliterHandler
());
pipeline
.
addLast
(
"packetDecodeHandler"
,
new
PacketDecodeHandler
());
pipeline
.
addLast
(
"nettyPluginServerHandler"
,
new
NettyPluginServerHandler
(
pluginServerFactory
,
threadPoolExecutor
));
pipeline
.
addLast
(
"packetEncodeHandler"
,
new
PacketEncodeHandler
());
}
});
ChannelFuture
channelFuture
=
serverBootstrap
.
bind
(
pluginServerFactory
.
getPort
()).
sync
();
super
.
onStart
();
channelFuture
.
channel
().
closeFuture
().
sync
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
finally
{
try
{
threadPoolExecutor
.
shutdown
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
try
{
bossGroup
.
shutdownGracefully
();
workGroup
.
shutdownGracefully
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
});
serverThread
.
setName
(
"【com.byit.server.netty.NettyPluginServer#start thread run】"
+
serverThread
.
hashCode
());
serverThread
.
setDaemon
(
true
);
serverThread
.
start
();
}
@Override
public
void
stop
()
{
if
(
serverThread
!=
null
&&
serverThread
.
isAlive
())
{
serverThread
.
interrupt
();
}
super
.
onStop
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment