/** * Close specified object if it is instance of AutoCloseable * @param mayBeCloseable */ public static void closeIfCloseable(Object mayBeCloseable) { if(mayBeCloseable instanceof AutoCloseable) { close((AutoCloseable) mayBeCloseable); } }
public void remove() { Closeables.close(raf); file.delete(); }
/** * Due this subscription "lazy", it close subscription each time when no listeners. * Therefore we can call {@link #clean()} and {@link #getBus()} many time at lifecycle. */ private void clean() { Runnable closer; MessageBus<M> bus; synchronized (busLock) { bus = this.bus; this.bus = null; closer = this.closer; } Closeables.close(bus); if(closer != null) { closer.run(); } } }
private void close(CloseableSupplier closeable) { try { AutoCloseable ac = closeable.get(); Closeables.close(ac); } catch (Exception e) { // we do not want to known what it happen here } }
/** * remove and close specified subscription * @param id */ public void unsubscribe(String id) { AutoCloseable subs = this.subs.remove(id); Closeables.close(subs); fire(); }
@Override public void close() throws Exception { synchronized (lock) { indexFile.setList(this.files.stream().map(QFileHandle::getFileName).collect(Collectors.toList())); indexFile.close(); QFileHandle<E> fh; while((fh = files.pollFirst()) != null) { Closeables.close(fh); } } } }
@Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { final HttpServletRequest request = (HttpServletRequest) req; final HttpServletResponse response = (HttpServletResponse) res; String id = Uuids.longUid(); NettyHandler handler = null; try { String uri = Utils.reconstructUri(request); log.debug("{}: start {} {}", id, request.getMethod(), uri); String upgrade = request.getHeader(org.springframework.http.HttpHeaders.UPGRADE); if (upgrade != null) { doUpgrade(id, request, response); return; } ChannelFuture cf = backend.connect().sync(); Channel channel = cf.channel(); DefaultFullHttpRequest backendReq = buildRequest(id, request, uri); handler = new NettyHandler(id, request, response); channel.pipeline().addLast(handler); channel.writeAndFlush(backendReq).sync(); channel.closeFuture().sync(); } catch (Exception e) { log.error("{}: error in service(): ", id, e); } finally { Closeables.close(handler); log.debug("{}: end", id); } }