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
Vous devez vous connecter pour publier un commentaire.
Une façon est de faire quelque chose comme:
Faire de cette façon, finira par le suivant:
Pas besoin d'un fil supplémentaire sur la partie principale.
Maintenant, la question est: quel type d'événement? C'est à vous... Peut-être un message dans l'écho gestionnaire comme "shutdown", qui sera prise comme un ordre d'arrêt et pas seulement "quitter" qui va tourner à la fermeture uniquement le client canal. Pourrait être quelque chose d'autre...
Si vous ne manipulez pas l'arrêt à partir d'un enfant de canal (donc par le biais de votre gestionnaire), mais par un autre processus (par exemple la recherche d'un fichier d'arrêt existant), alors vous avez besoin d'un coup de fil qui va attendre cet événement, puis directement
channel.close()
où le canal va être le parent d'un (def.channel()
) par exemple...De nombreuses autres solutions existent.
OriginalL'auteur Frederic Brégier