AsynchronousServerSocketChannelを使用してリスナーを実装しました。チャネルを開き、ポートにバインドして接続を受け入れます。私はメッセージを聞いて読むことができます。リスナーがデータを受け入れているチャネルを閉じた後の問題イベント。AsynchronousServerSocketChannelが確立された接続をクローズしない
次のようにサーバーチャネルのソケット接続を閉じます。ここで
this.serverChannel.close();
this.serverChannel = null;
は接続
this.serverChannel
.accept(null,
new java.nio.channels.CompletionHandler<AsynchronousSocketChannel, Void>() {
@Override
public void completed(
AsynchronousSocketChannel result,
Void attachment) {
acceptConnections();
read(new HSLParsingContext(), result);
}
@Override
public void failed(Throwable exc, Void attachment) {
if (!(exc instanceof AsynchronousCloseException))
});
public void read(final HSLParsingContext context,
final AsynchronousSocketChannel channel) {
try {
channel.read(context.buffer, null,
new java.nio.channels.CompletionHandler<Integer, Void>() {
@Override
public void completed(Integer bytesRead, Void attachment) {
try {
HSLSysLogTcpListener.this.logger.infoFmt(
"Bytes received: %s ", bytesRead);
if (bytesRead > 0) {
messageHandler(context, false);
}
if (bytesRead == -1) {
// when nothing is there to read in the
// channel make sure there
// is nothing in the content buffer before
// closing the channel
if (context.content.length() > 0) {
messageHandler(context, true);
}
throw new EOFException();
}
// keep reading data asynchronously
read(context, channel);
} catch (Exception e) {
failed(e, null);
}
}
@Override
public void failed(Throwable e, Void attachment) {
logReadFailureAndClose(channel, e);
}
});
} catch (Throwable e) {
logReadFailureAndClose(channel, e);
}
私は一種のacceptメソッドによって開かよりAsynchronousSocketChannelが閉じていないと感じるの受け入れを完了ハンドラです。したがって、私はAsynchronousServerSocketChannelを閉じた後にmessagesイベントを受け取ります。
はserversocketchannel.Pleaseを閉じているときに、私は完全にAsynchronousServerSocketChannelを停止した最高のを知っていると、それはAsynchronousSocketChannel
私はlogReadFailureAndClose()で終了します。これは、i bytesRead == -1(ストリームの終わり)に達するか、またはピアによってコネクションがリセットされ、ソケットシャネルが強制的に閉じられることがあります。しかし、このイベントが発生せず、リスナをキャンセルしたいときもあります。その時点で私はAsynchronousServerSocketChannelを閉じます。 AsynchronousSocketChannelのリストを取得するにはどうすればよいですか?これは、各AsynchronousSocketChannelの一意のid属性ですか? –
また、AsynchronousChannelGroupをAsynchronousServerSocketChannelに追加し、グループに対してshutdownを呼び出す方法についても説明しました。それは働くだろうか? –
@ KarthikeyanRamasamyいいえ、それは本当に糸工場です。あなたは私の答えで言ったことをしなければなりません:受け入れられたソケットのコレクションを保管し、あなた自身でそれをすべて閉じます。 – EJP