いいえ、あなたは正しいことをしています。ただし、Channel
インスタンスへの参照は保持する必要があります。そのチャネルを取得したら、それが開いている限り、別のブートストラップを作成する必要はありません。(それがあなたがしていることなら。)
これは私が最近のプロジェクトで使用したものです:
クラス ClientConnection (コンストラクター)
// Configure the client.
bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()
)
);
// Set up the pipeline factory.
bootstrap.setPipelineFactory(
new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
// put your handlers here
);
}
}
);
class ClientConnection.connect(String ホスト, int ポート)
if (isConnected()) {
throw new IllegalStateException("already connected");
}
// Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
channel = future.awaitUninterruptibly().getChannel();
// Wait until the connection is closed or the connection attempt fails.
channel.getCloseFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
new Thread(new Runnable() {
public void run() {
// Shut down thread pools to exit
// (cannot be executed in the same thread pool!
bootstrap.releaseExternalResources();
LOG.log(Level.INFO, "Shutting down");
}
}).start();
}
});
したがって、基本的には と への参照のみを保持しますbootstrap
がchannel
、前者はこれらのコード行以外ではほとんど使用されません。
注:bootstrap.releaseExternalResources();
アプリケーションの終了時に一度だけ実行する必要があります。私の場合、クライアントはいくつかのファイルを送信し、チャネルを閉じて終了します。
接続されたChannel
インスタンスを取得したら、再度閉じるまでそのインスタンスを使用するだけで済みます。閉じたら、 を呼び出してbootstrap
新しいものChannel
を再度作成できます。
個人的には、最初は Netty を理解するのが少し難しいと思いますが、それがどのように機能するかを理解すると、Netty は Java で最高の NIO フレームワークです。IMO。