java.lang.AutoCloseable.close()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(5.4k)|赞(0)|评价(0)|浏览(163)

本文整理了Java中java.lang.AutoCloseable.close()方法的一些代码示例,展示了AutoCloseable.close()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AutoCloseable.close()方法的具体详情如下:
包路径:java.lang.AutoCloseable
类名称:AutoCloseable
方法名:close

AutoCloseable.close介绍

[英]Closes the object and release any system resources it holds.
[中]关闭对象并释放它所持有的任何系统资源。

代码示例

代码示例来源:origin: apache/flink

/**
 * Closes the given AutoCloseable.
 *
 * <p><b>Important:</b> This method is expected to never throw an exception.
 */
public static void closeQuietly(AutoCloseable closeable) {
  try {
    if (closeable != null) {
      closeable.close();
    }
  } catch (Throwable ignored) {}
}

代码示例来源:origin: dropwizard/dropwizard

/**
   * Calls {@link AutoCloseable#close()} on the closable provided in
   * {@link AutoCloseableManager#AutoCloseableManager(AutoCloseable)}.
   *
   * @throws Exception propagates {@link AutoCloseable#close()} exception
   */
  @Override
  public void stop() throws Exception {
    this.autoCloseable.close();
  }
}

代码示例来源:origin: thinkaurelius/titan

public static void closeQuietly(AutoCloseable c) {

    try {
      if (c != null)
        c.close();
    } catch (Exception e) {
      logger.warn("Failed closing " + c, e);
    }
  }
}

代码示例来源:origin: prestodb/presto

private static void closeUnchecked(AutoCloseable closeable)
{
  try {
    closeable.close();
  }
  catch (Exception e) {
    throwIfUnchecked(e);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: apache/kafka

/**
 * Closes {@code closeable} and if an exception is thrown, it is logged at the WARN level.
 */
public static void closeQuietly(AutoCloseable closeable, String name) {
  if (closeable != null) {
    try {
      closeable.close();
    } catch (Throwable t) {
      log.warn("Failed to close {} with type {}", name, closeable.getClass().getName(), t);
    }
  }
}

代码示例来源:origin: SonarSource/sonarqube

/**
  * Closes the object and throws an {@link java.lang.IllegalStateException} on error.
  * @since 5.1
  */
 public void close(AutoCloseable closeable) {
  try {
   closeable.close();
  } catch (Exception e) {
   throw new IllegalStateException("Fail to close " + closeable, e);
  }
 }
}

代码示例来源:origin: thinkaurelius/titan

public synchronized void release() throws Exception {
    Preconditions.checkState(null != current);
    Preconditions.checkState(0 < refCount);
    refCount--;
    if (0 == refCount) {
      current.close();
      current = null;
    }
  }
}

代码示例来源:origin: Tencent/tinker

/**
   * Closes the given {@code obj}. Suppresses any exceptions.
   */
  @SuppressWarnings("NewApi")
  public static void closeQuietly(Object obj) {
    if (obj == null) return;
    try {
      if (obj instanceof Closeable) {
        ((Closeable) obj).close();
      } else if (obj instanceof AutoCloseable) {
        ((AutoCloseable) obj).close();
      } else if (obj instanceof ZipFile) {
        ((ZipFile) obj).close();
      }
    } catch (Throwable ignored) {
      // ignored.
    }
  }
}

代码示例来源:origin: prestodb/presto

static void closeQuietly(AutoCloseable closeable)
  {
    try {
      closeable.close();
    }
    catch (Exception ignored) {
    }
  }
}

代码示例来源:origin: neo4j/neo4j

@Override
  public void shutdown() throws Exception
  {
    closeable.close();
  }
}

代码示例来源:origin: apache/incubator-druid

@Override
 public void close() throws IOException
 {
  if (closeable != null) {
   try {
    closeable.close();
   }
   catch (Exception e) {
    Throwables.propagateIfInstanceOf(e, IOException.class);
    throw Throwables.propagate(e);
   }
  }
 }
};

代码示例来源:origin: SonarSource/sonarqube

@Override
 protected void doClose() throws Exception {
  // iterator can be already closed by doNext(), but closing here ensures
  // that iterator is closed when it is not fully traversed.
  iterator.close();
  for (AutoCloseable otherCloseable : otherCloseables) {
   otherCloseable.close();
  }
 }
}

代码示例来源:origin: apache/storm

@SuppressWarnings("deprecation")
private static <T extends AutoCloseable> TimeCacheMap<String, T> fileCacheMap(Map<String, Object> conf) {
  return new TimeCacheMap<>(ObjectReader.getInt(conf.get(DaemonConfig.NIMBUS_FILE_COPY_EXPIRATION_SECS), 600),
    (id, stream) -> {
      try {
        stream.close();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    });
}

代码示例来源:origin: neo4j/neo4j

@Override
protected void after()
{
  for ( AutoCloseable toClose : toCloseAfterwards )
  {
    try
    {
      toClose.close();
    }
    catch ( Exception e )
    {
      throw new RuntimeException( e );
    }
  }
}

代码示例来源:origin: apache/storm

@Override
  public void close() throws Exception {
    try {
      super.close();
    } finally {
      getMeasured().close();
    }
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void closeAllSilently() throws Exception
{
  IOUtils.closeAllSilently( goodClosable1, faultyClosable, goodClosable2 );
  verify( goodClosable1 ).close();
  verify( goodClosable2 ).close();
  verify( faultyClosable ).close();
}

代码示例来源:origin: apache/flink

@Override
public void close() throws Exception {
  if (bulkProcessor != null) {
    bulkProcessor.close();
    bulkProcessor = null;
  }
  if (client != null) {
    client.close();
    client = null;
  }
  callBridge.cleanup();
  // make sure any errors from callbacks are rethrown
  checkErrorAndRethrow();
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldCleanupMultipleObjectsInReverseAddedOrder() throws Throwable
{
  // GIVEN
  CleanupRule rule = new CleanupRule();
  AutoCloseable closeable = rule.add( mock( AutoCloseable.class ) );
  Dirt dirt = rule.add( mock( Dirt.class ) );
  // WHEN
  simulateTestExecution( rule );
  // THEN
  InOrder inOrder = inOrder( dirt, closeable );
  inOrder.verify( dirt, times( 1 ) ).shutdown();
  inOrder.verify( closeable, times( 1 ) ).close();
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldCleanupAutoCloseable() throws Throwable
{
  // GIVEN
  CleanupRule rule = new CleanupRule();
  AutoCloseable toClose = rule.add( mock( AutoCloseable.class ) );
  // WHEN
  simulateTestExecution( rule );
  // THEN
  verify( toClose ).close();
}

代码示例来源:origin: neo4j/neo4j

@Test
public void closeAllAndRethrowException() throws Exception
{
  doThrow( new IOException( "Faulty closable" ) ).when( faultyClosable ).close();
  expectedException.expect( IOException.class );
  expectedException.expectMessage( "Exception closing multiple resources" );
  expectedException.expect( new NestedThrowableMatcher( IOException.class ) );
  IOUtils.closeAll( goodClosable1, faultyClosable, goodClosable2 );
}

相关文章