Arrêt netty par programmation

Je suis en utilisant netty 4.0.24.Final.

J'ai besoin pour démarrer/arrêter netty serveur par programme.
Sur le démarrage du serveur, plus le fil est bloqué à

f.channel().closeFuture().sync()

S'il vous plaît aider avec quelques conseils sur la façon de le faire correctement.
Ci-dessous est la EchoServer qui est appelée par la classe Principale.
Merci.

package nettytests;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class EchoServer {
private final int PORT = 8007;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
public void start() throws Exception {
//Configure the server.
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(1);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
});
//Start the server.
ChannelFuture f = b.bind(PORT).sync();
//Wait until the server socket is closed. Thread gets blocked.
f.channel().closeFuture().sync();
} finally {
//Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public void stop(){
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
package nettytests;
public class Main {
public static void main(String[] args) throws Exception {
EchoServer server = new EchoServer();
//start server
server.start();
//not called, because the thread is blocked above
server.stop();
}
}

Mise à JOUR:
J'ai changé le EchoServer classe de la manière suivante. L'idée est de démarrer le serveur dans un nouveau thread et de préserver les liens vers le EventLoopGroups.
Est-ce la bonne manière?

package nettytests;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* Echoes back any received data from a client.
*/
public class EchoServer {
private final int PORT = 8007;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
public void start() throws Exception {
new Thread(() -> {
//Configure the server.
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(1);
Thread.currentThread().setName("ServerThread");
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
});
//Start the server.
ChannelFuture f = b.bind(PORT).sync();
//Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}
public void stop() throws InterruptedException {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}

OriginalL'auteur fh76 | 2015-01-19