SocketChannel
のclose
メソッドが呼び出されたときに通知を受けたい。私の最初の考えは、close
メソッド自体がでAbstractInterruptibleChannel
と宣言されているので、implCloseSelectableChannel
メソッドが呼び出されたときにリスナーに通知するラッパーを作成することでした。このソリューションは動作しますが、私はSelector
に登録しようとしたとき、私は理由SelectorImpl
で次のチェックのIllegalSelectorException
になるだろう:SocketChannelが閉じられたときの通知方法
/* */ protected final SelectionKey register(AbstractSelectableChannel paramAbstractSelectableChannel, int paramInt, Object paramObject)
/* */ {
/* 128 */ if (!(paramAbstractSelectableChannel instanceof SelChImpl))
/* 129 */ throw new IllegalSelectorException();
今、私はラップSocketChannel
に委任するregister
メソッドをオーバーライドすることはできませんので、 final
がAbstractSelectableChannel
にあり、sun.nio.ch
パッケージにデフォルトの可視性があるため、SelChImpl
を実装できません。私がここから進むために見ることができる唯一の方法は、私自身のSelectorProvider
とSelector
を作ることですが、それはあまりにも簡単なことのために過度のようです。
SocketChannel
が閉鎖されたとき、またはプログラムデザインを再考する必要があるときに通知する簡単な方法はありますか?
SocketChannelWrapper
例:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class SocketChannelWrapper extends SocketChannel {
private static interface CloseListener {
public void socketChannelClosed(SocketChannel channel);
}
private final SocketChannel socket;
private final CloseListener listener;
public SocketChannelWrapper(SocketChannel socket, CloseListener l) {
super(socket.provider());
this.socket = socket;
listener = l;
}
@Override
public SocketAddress getLocalAddress() throws IOException {
return socket.getLocalAddress();
}
@Override
public <T> T getOption(SocketOption<T> name) throws IOException {
return socket.getOption(name);
}
@Override
public Set<SocketOption<?>> supportedOptions() {
return socket.supportedOptions();
}
@Override
public SocketChannel bind(SocketAddress local) throws IOException {
return socket.bind(local);
}
@Override
public <T> SocketChannel setOption(SocketOption<T> name, T value)
throws IOException {
return socket.setOption(name, value);
}
@Override
public SocketChannel shutdownInput() throws IOException {
return socket.shutdownInput();
}
@Override
public SocketChannel shutdownOutput() throws IOException {
return socket.shutdownOutput();
}
@Override
public Socket socket() {
return socket.socket();
}
@Override
public boolean isConnected() {
return socket.isConnected();
}
@Override
public boolean isConnectionPending() {
return socket.isConnectionPending();
}
@Override
public boolean connect(SocketAddress remote) throws IOException {
return socket.connect(remote);
}
@Override
public boolean finishConnect() throws IOException {
return socket.finishConnect();
}
@Override
public SocketAddress getRemoteAddress() throws IOException {
return socket.getRemoteAddress();
}
@Override
public int read(ByteBuffer dst) throws IOException {
return socket.read(dst);
}
@Override
public long read(ByteBuffer[] dsts, int offset, int length)
throws IOException {
return socket.read(dsts, offset, length);
}
@Override
public int write(ByteBuffer src) throws IOException {
return socket.write(src);
}
@Override
public long write(ByteBuffer[] srcs, int offset, int length)
throws IOException {
return socket.write(srcs, offset, length);
}
@Override
protected void implCloseSelectableChannel() throws IOException {
socket.close();
listener.socketChannelClosed(this);
}
@Override
protected void implConfigureBlocking(boolean block) throws IOException {
socket.configureBlocking(block);
}
public static void main(String[] args) throws UnknownHostException,
IOException {
final Selector selector = Selector.open();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
selector.select();
Iterator<SelectionKey> itr = selector.selectedKeys()
.iterator();
while (itr.hasNext()) {
SelectionKey key = itr.next();
itr.remove();
if (key.isValid()) {
if (key.isAcceptable()) {
((ServerSocketChannel) key.channel())
.accept();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
t.setDaemon(true);
ServerSocketChannel server = ServerSocketChannel.open().bind(
new InetSocketAddress(1234));
server.configureBlocking(false);
server.register(selector, SelectionKey.OP_ACCEPT);
t.start();
SocketChannel socket = new SocketChannelWrapper(
SocketChannel.open(new InetSocketAddress(InetAddress
.getLocalHost(), 1234)), new CloseListener() {
@Override
public void socketChannelClosed(SocketChannel channel) {
System.out.println("Socket closed!");
}
});
socket.configureBlocking(false);
// socket.close(); //prints out "Socket closed!"
socket.register(selector, SelectionKey.OP_READ);
}
}
問題は、私のプログラムがうまく動作するためには、私に通知するために 'SocketChannel'を閉じる人が誰かが必要であるということです。もしそうでなければ、物事が破れ始める。私を怠惰な/忘れてしまったと呼びますが、これを達成する最も簡単な方法は 'SocketChannel'にコールバックを入れることだけです。この問題が引き続き発生する場合、私のデザインを再考する必要があるかもしれません。 – Jeffrey
@Jeffreyアプリケーションをデバッグする必要があります。 – EJP
私のアプリケーションにはバグはありませんが、自分自身に通知するのを忘れた場合は、エラーがポップアップします。これまでのところ、私は問題を理解し、 "瞬間"の瞬間を持ち越しましたが、最近のうちに私は自分自身に通知することを忘れてしまい、理由を理解することができません。 'SocketChannel'がどうやって問題を解決するのかを修正できるかどうかを知りたかったのです。 – Jeffrey