本文整理汇总了Java中java.nio.channels.SocketChannel.isOpen方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.isOpen方法的具体用法?Java SocketChannel.isOpen怎么用?Java SocketChannel.isOpen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.isOpen方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeData
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void writeData(
RequestContext requestContext,
Function<RequestContext, ByteBuffer> toByteBuffer) {
SocketChannel socketChannel = requestContext.getSocketChannel();
if (!socketChannel.isOpen()) {
LOG.info("SocketChannel closed. Not writing.");
return;
}
try {
ByteBuffer byteBuffer = toByteBuffer.apply(requestContext);
int size = byteBuffer.position();
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
socketChannel.write(byteBuffer);
}
LOG.info("Write finished: " + size + " bytes.");
} catch (IOException e) {
LOG.error("Error writing data", e);
CloseableUtils.close(socketChannel);
}
}
示例2: closeBlocking
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public void closeBlocking() throws InterruptedException {
super.closeBlocking();
try {
Field channelField = this.getClass().getSuperclass().getDeclaredField("channel");
channelField.setAccessible(true);
SocketChannel channel = (SocketChannel) channelField.get(this);
if (channel != null && channel.isOpen()) {
Socket socket = channel.socket();
if (socket != null)
socket.close();
}
}
catch (Exception ex) {
System.out.println("Exception in Websocket close hack.");
ex.printStackTrace();
}
}
示例3: createOrReuseSocket
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public SocketChannel createOrReuseSocket(InetAddress inetAddress, int port) throws IOException {
String key = NIOHandler.makeKey(inetAddress, port);
SocketChannel channel = null;
keyedSemaphore.enterIOCriticalSection(key);
try {
channel = getSocket(key);
if(channel != null && (!channel.isConnected() || !channel.isOpen())) {
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("Channel disconnected " + channel);
channel = null;
}
if(channel == null) { // this is where the threads will race
SocketAddress sockAddr = new InetSocketAddress(inetAddress, port);
channel = messageProcessor.blockingConnect((InetSocketAddress) sockAddr, 10000);
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("create channel = " + channel + " " + inetAddress + " " + port);
if(channel != null && channel.isConnected()) {
putSocket(NIOHandler.makeKey(inetAddress, port), channel);
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("channel cached channel = " + channel);
}
}
return channel;
} finally {
keyedSemaphore.leaveIOCriticalSection(key);
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("Returning socket " + key + " channel = " + channel);
}
}
示例4: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* The contract is similar to {@link SocketChannel#connect(SocketAddress)}
* with a timeout.
*
* @see SocketChannel#connect(SocketAddress)
*
* @param channel - this should be a {@link SelectableChannel}
* @param endpoint
* @throws IOException
*/
static void connect(SocketChannel channel,
SocketAddress endpoint, int timeout) throws IOException {
boolean blockingOn = channel.isBlocking();
if (blockingOn) {
channel.configureBlocking(false);
}
try {
if (channel.connect(endpoint)) {
return;
}
long timeoutLeft = timeout;
long endTime = (timeout > 0) ? (Time.now() + timeout): 0;
while (true) {
// we might have to call finishConnect() more than once
// for some channels (with user level protocols)
int ret = selector.select((SelectableChannel)channel,
SelectionKey.OP_CONNECT, timeoutLeft);
if (ret > 0 && channel.finishConnect()) {
return;
}
if (ret == 0 ||
(timeout > 0 &&
(timeoutLeft = (endTime - Time.now())) <= 0)) {
throw new SocketTimeoutException(
timeoutExceptionString(channel, timeout,
SelectionKey.OP_CONNECT));
}
}
} catch (IOException e) {
// javadoc for SocketChannel.connect() says channel should be closed.
try {
channel.close();
} catch (IOException ignored) {}
throw e;
} finally {
if (blockingOn && channel.isOpen()) {
channel.configureBlocking(true);
}
}
}
示例5: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* The contract is similar to {@link SocketChannel#connect(SocketAddress)}
* with a timeout.
*
* @see SocketChannel#connect(SocketAddress)
*
* @param channel - this should be a {@link SelectableChannel}
* @param endpoint
* @throws IOException
*/
static void connect(SocketChannel channel,
SocketAddress endpoint, int timeout) throws IOException {
boolean blockingOn = channel.isBlocking();
if (blockingOn) {
channel.configureBlocking(false);
}
try {
if (channel.connect(endpoint)) {
return;
}
long timeoutLeft = timeout;
long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout): 0;
while (true) {
// we might have to call finishConnect() more than once
// for some channels (with user level protocols)
int ret = selector.select((SelectableChannel)channel,
SelectionKey.OP_CONNECT, timeoutLeft);
if (ret > 0 && channel.finishConnect()) {
return;
}
if (ret == 0 ||
(timeout > 0 &&
(timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) {
throw new SocketTimeoutException(
timeoutExceptionString(channel, timeout,
SelectionKey.OP_CONNECT));
}
}
} catch (IOException e) {
// javadoc for SocketChannel.connect() says channel should be closed.
/*try {
channel.close();
} catch (IOException ignored) {}*/
throw e;
} finally {
if (blockingOn && channel.isOpen()) {
channel.configureBlocking(true);
}
}
}