下面列出了怎么用java.nio.channels.Channel的API类实例代码及写法,或者点击链接到github查看源代码。
/** {@inheritDoc} */
@Override public IgniteInternalFuture<Channel> openChannel(ClusterNode remote,
Message initMsg) throws IgniteSpiException {
try {
if (block) {
U.log(log, "Start waiting on trying open a new channel");
latch.await(5, TimeUnit.SECONDS);
}
}
catch (InterruptedException e) {
throw new IgniteException(e);
}
return super.openChannel(remote, initMsg);
}
public static void main(String args[]) {
// test the assertion that SelectorProvider.inheritedChannel()
// and System.inheritedChannel return null when standard input
// is not connected to a socket
Channel c1, c2;
try {
c1 = SelectorProvider.provider().inheritedChannel();
c2 = System.inheritedChannel();
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
if (c1 != null || c2 != null) {
throw new RuntimeException("Channel returned - unexpected");
}
}
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
public static void main(String args[]) {
// test the assertion that SelectorProvider.inheritedChannel()
// and System.inheritedChannel return null when standard input
// is not connected to a socket
Channel c1, c2;
try {
c1 = SelectorProvider.provider().inheritedChannel();
c2 = System.inheritedChannel();
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
if (c1 != null || c2 != null) {
throw new RuntimeException("Channel returned - unexpected");
}
}
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
private void doClose() {
connLock.lock();
try {
Channel channel = this.channel;
if (channel != null) {
if (channel.isOpen()) {
IOUtils.close(channel);
listener.onDisConnected(client);
logger.w("channel closed !!!");
}
this.channel = null;
}
} finally {
state.set(disconnected);
connLock.unlock();
}
}
/**
* This should be followed by closing a selector.
*
* @param channel the channel to close.
*/
public static void closeHard(final Channel channel) {
if (channel != null) {
try {
// Close socket
if (channel instanceof SocketChannel) {
closeHard(((SocketChannel) channel).socket());
}
// Close channel
channel.close();
} catch (final Exception e) {
ignoreException(e, "closing hard");
}
}
}
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
public static void main(String args[]) {
// test the assertion that SelectorProvider.inheritedChannel()
// and System.inheritedChannel return null when standard input
// is not connected to a socket
Channel c1, c2;
try {
c1 = SelectorProvider.provider().inheritedChannel();
c2 = System.inheritedChannel();
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
if (c1 != null || c2 != null) {
throw new RuntimeException("Channel returned - unexpected");
}
}
public void createFileHandler() {
ExpiredCallback<Object, Object> expiredCallback = new ExpiredCallback<Object, Object>() {
@Override
public void expire(Object key, Object val) {
try {
LOG.info("Close file " + String.valueOf(key));
if (val != null) {
if (val instanceof Channel) {
Channel channel = (Channel) val;
channel.close();
} else if (val instanceof BufferFileInputStream) {
BufferFileInputStream is = (BufferFileInputStream) val;
is.close();
}
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
};
int file_copy_expiration_secs = JStormUtils.parseInt(conf.get(Config.NIMBUS_FILE_COPY_EXPIRATION_SECS), 30);
uploaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
downloaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
}
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
public static void main(String args[]) {
// test the assertion that SelectorProvider.inheritedChannel()
// and System.inheritedChannel return null when standard input
// is not connected to a socket
Channel c1, c2;
try {
c1 = SelectorProvider.provider().inheritedChannel();
c2 = System.inheritedChannel();
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
if (c1 != null || c2 != null) {
throw new RuntimeException("Channel returned - unexpected");
}
}
public void releaseFileLock() {
if (fileLock != null) {
if (log.isTraceEnable()) {
log.info(this, "Releasing the file lock of " + this.filePath.getFileName());
}
Channel fc = fileLock.acquiredBy();
try {
fileLock.release();
fileLock = null;
if (fc != null) {
fc.close();
}
}
catch (IOException e) {
}
}
}
public static void main(String args[]) {
// test the assertion that SelectorProvider.inheritedChannel()
// and System.inheritedChannel return null when standard input
// is not connected to a socket
Channel c1, c2;
try {
c1 = SelectorProvider.provider().inheritedChannel();
c2 = System.inheritedChannel();
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
if (c1 != null || c2 != null) {
throw new RuntimeException("Channel returned - unexpected");
}
}
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
private void disconnectAllKeysAndShutdown()
{
for (SelectionKey selectionKey : selector.keys()) {
if (selectionKey.isValid()) {
Channel channel = selectionKey.channel();
if (channel != null && channel.isOpen()) {
Listener l = (Listener)selectionKey.attachment();
try {
selectionKey.channel().close();
if (l != null) {
if (l instanceof Listener.ClientListener) {
((Listener.ClientListener)l).disconnected();
}
l.unregistered(selectionKey);
}
} catch (IOException e) {
if (l != null) {
l.handleException(e, this);
} else {
logger.warn("Exception while closing channel {} on unregistered key {}", channel, selectionKey, e);
}
}
}
}
}
alive = false;
selector.wakeup();
}
static void closeChannel(@Nullable Channel channel) {
if (channel != null && channel.isOpen()) {
try {
channel.close();
}
catch (IOException ignored) {
}
}
}
private static void checkAccess(Channel c) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(
new RuntimePermission("inheritedChannel")
);
}
}
@Override
public WriteCacheImpl newWriteCache(final IBufferAccess buf,
final boolean useChecksum, final boolean bufferHasData,
final IReopenChannel<? extends Channel> opener,
final long fileExtent)
throws InterruptedException {
return new WriteCacheImpl(0/* baseOffset */, buf, useChecksum,
bufferHasData, (IReopenChannel<FileChannel>) opener,
fileExtent);
}
private static void checkAccess(Channel c) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(
new RuntimePermission("inheritedChannel")
);
}
}
/**
* Closes all objects passed as the argument.
* If any of them throws an exception, the first exception is thrown.
* The remaining exceptions are added as suppressed to the first one.
*
* @param closeables
* @throws IOException
*/
public static void closeAll(Iterable<? extends AutoCloseable> closeables) throws IOException {
if (closeables != null) {
Exception firstException = null;
for (AutoCloseable closeable: closeables) {
if (closeable != null) {
if ((closeable instanceof Channel) && !((Channel) closeable).isOpen()) {
continue; // channel is already closed
}
try {
closeable.close();
} catch (Exception ex) {
if (firstException == null) {
firstException = ex;
} else {
firstException.addSuppressed(ex);
}
}
}
}
if (firstException != null) {
throw ExceptionUtils.getIOException(firstException);
}
}
}
private static void closeStream(final Channel closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// ignore
}
}
}
static void closeChannel(@Nullable Channel channel) {
if (channel != null && channel.isOpen()) {
try {
channel.close();
}
catch (IOException ignored) {
}
}
}