List of usage examples for com.google.common.io Closeables close
public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException
From source file:io.codis.jodis.RoundRobinJedisPool.java
@Override public void close() { try {/*from w w w. j a v a 2 s .co m*/ Closeables.close(watcher, true); } catch (IOException e) { throw new AssertionError("IOException should not have been thrown", e); } if (closeCurator) { curatorClient.close(); } List<PooledObject> pools = this.pools; this.pools = ImmutableList.of(); for (PooledObject pool : pools) { pool.close(); } jedisPoolClosingExecutor.shutdown(); }
From source file:org.apache.druid.firehose.kafka.KafkaEightSimpleConsumerFirehoseFactory.java
@Override public FirehoseV2 connect(final ByteBufferInputRowParser firehoseParser, Object lastCommit) { final Map<Integer, Long> lastOffsets = loadOffsetFromPreviousMetaData(lastCommit); for (Integer partition : partitionIdList) { final KafkaSimpleConsumer kafkaSimpleConsumer = new KafkaSimpleConsumer(feed, partition, clientId, brokerList, earliest);/*from w w w . java 2 s. co m*/ Long startOffset = lastOffsets.get(partition); PartitionConsumerWorker worker = new PartitionConsumerWorker(feed, kafkaSimpleConsumer, partition, startOffset == null ? -1 : startOffset); consumerWorkers.add(worker); } final LinkedBlockingQueue<BytesMessageWithOffset> messageQueue = new LinkedBlockingQueue<BytesMessageWithOffset>( queueBufferLength); log.info("Kicking off all consumers"); for (PartitionConsumerWorker worker : consumerWorkers) { worker.go(messageQueue); } log.info("All consumer started"); return new FirehoseV2() { private Map<Integer, Long> lastOffsetPartitions; private volatile boolean stopped; private volatile BytesMessageWithOffset msg = null; private volatile InputRow row = null; private volatile Iterator<InputRow> nextIterator = Collections.emptyIterator(); { lastOffsetPartitions = Maps.newHashMap(); lastOffsetPartitions.putAll(lastOffsets); } @Override public void start() { } @Override public boolean advance() { if (stopped) { return false; } nextMessage(); return true; } private void nextMessage() { try { row = null; while (row == null) { if (!nextIterator.hasNext()) { if (msg != null) { lastOffsetPartitions.put(msg.getPartition(), msg.offset()); } msg = messageQueue.take(); final byte[] message = msg.message(); nextIterator = message == null ? Collections.emptyIterator() : firehoseParser.parseBatch(ByteBuffer.wrap(message)).iterator(); continue; } row = nextIterator.next(); } } catch (InterruptedException e) { //Let the caller decide whether to stop or continue when thread is interrupted. log.warn(e, "Thread Interrupted while taking from queue, propagating the interrupt"); Thread.currentThread().interrupt(); } } @Override public InputRow currRow() { if (stopped) { return null; } // currRow will be called before the first advance if (row == null) { try { nextMessage(); } catch (ParseException e) { return null; } } return row; } @Override public Committer makeCommitter() { final Map<Integer, Long> offsets = Maps.newHashMap(lastOffsetPartitions); return new Committer() { @Override public Object getMetadata() { return offsets; } @Override public void run() { } }; } @Override public void close() throws IOException { log.info("Stopping kafka 0.8 simple firehose"); stopped = true; for (PartitionConsumerWorker t : consumerWorkers) { Closeables.close(t, true); } } }; }
From source file:IaaSEngine.java
public void close() throws IOException { Closeables.close(keystoneApi, true); }
From source file:org.apache.mahout.math.hadoop.decomposer.DistributedLanczosSolver.java
/** * @param state The final LanczosState to be serialized * @param outputPath The path (relative to the current Configuration's FileSystem) to save the output to. *//*from w w w . j a v a 2 s. com*/ public void serializeOutput(LanczosState state, Path outputPath) throws IOException { int numEigenVectors = state.getIterationNumber(); log.info("Persisting {} eigenVectors and eigenValues to: {}", numEigenVectors, outputPath); Configuration conf = getConf() != null ? getConf() : new Configuration(); FileSystem fs = FileSystem.get(outputPath.toUri(), conf); SequenceFile.Writer seqWriter = new SequenceFile.Writer(fs, conf, outputPath, IntWritable.class, VectorWritable.class); try { IntWritable iw = new IntWritable(); for (int i = 0; i < numEigenVectors; i++) { // Persist eigenvectors sorted by eigenvalues in descending order\ NamedVector v = new NamedVector(state.getRightSingularVector(numEigenVectors - 1 - i), "eigenVector" + i + ", eigenvalue = " + state.getSingularValue(numEigenVectors - 1 - i)); Writable vw = new VectorWritable(v); iw.set(i); seqWriter.append(iw, vw); } } finally { Closeables.close(seqWriter, false); } }
From source file:org.jclouds.examples.rackspace.DeleteAll.java
private void deleteCloudDatabases() throws IOException { TroveApi troveApi = ContextBuilder/*from w ww. j a v a 2 s . com*/ .newBuilder(System.getProperty("provider.cdb", "rackspace-clouddatabases-us")) .credentials(username, apiKey).buildApi(TroveApi.class); for (String region : troveApi.getConfiguredRegions()) { try { System.out.format("Delete Database Instances of DBs and Users in %s%n", region); InstanceApi instanceApi = troveApi.getInstanceApi(region); for (Instance instance : instanceApi.list()) { System.out.format(" %s%n", instance.getName()); DatabaseApi databaseApi = troveApi.getDatabaseApi(region, instance.getId()); for (String database : databaseApi.list()) { System.out.format(" %s%n", database); databaseApi.delete(database); } UserApi userApi = troveApi.getUserApi(region, instance.getId()); for (User user : userApi.list()) { System.out.format(" %s%n", user.getName()); userApi.delete(user.getName()); } instanceApi.delete(instance.getId()); } } catch (Exception e) { e.printStackTrace(); } } Closeables.close(troveApi, true); }
From source file:net.oneandone.troilus.CassandraDB.java
@SuppressWarnings("unchecked") protected static int prepare() { String cassandraDirName = "target" + File.separator + "cassandra-junit-" + new Random().nextInt(1000000); File cassandraDir = new File(cassandraDirName); cassandraDir.mkdirs();/* ww w .j a va 2 s.c om*/ InputStream cassandraConfigurationInput = null; Writer cassandraConfigurationOutput = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); cassandraConfigurationInput = loader.getResourceAsStream(CASSANDRA_YAML_FILE); Yaml yaml = new Yaml(); Map<String, Object> cassandraConfiguration = (Map<String, Object>) yaml .load(cassandraConfigurationInput); int rpcPort = findUnusedLocalPort(); if (rpcPort == -1) { throw new RuntimeException("Can not start embedded cassandra: no unused local port found!"); } cassandraConfiguration.put("rpc_port", rpcPort); int storagePort = findUnusedLocalPort(); if (storagePort == -1) { throw new RuntimeException("Can not start embedded cassandra: no unused local port found!"); } cassandraConfiguration.put("storage_port", storagePort); int nativeTransportPort = findUnusedLocalPort(); if (nativeTransportPort == -1) { throw new RuntimeException("Can not start embedded cassandra: no unused local port found!"); } cassandraConfiguration.put("native_transport_port", nativeTransportPort); cassandraConfiguration.put("start_native_transport", "true"); Object obj = cassandraConfiguration.get("data_file_directories"); cassandraConfiguration.put("data_file_directories", Lists.newArrayList(new File(cassandraDir, "data").getAbsolutePath())); cassandraConfiguration.put("commitlog_directory", new File(cassandraDir, "commitlog").getAbsolutePath()); cassandraConfiguration.put("saved_caches_directory", new File(cassandraDir, "saved_caches").getAbsolutePath()); // jwestra: 3.x API change: resolve error on start of 'hints_directory' is missing cassandraConfiguration.put("hints_directory", new File(cassandraDir, "hints_directory").getAbsolutePath()); cassandraConfigurationOutput = new OutputStreamWriter( new FileOutputStream(cassandraDirName + File.separator + CASSANDRA_YAML_FILE), Charsets.UTF_8); // jwestra: 3.x API change: resolve error on start of -Dcassandra.storage_dir is not set String storageDir = new File(cassandraDir, "storageDir").getAbsolutePath(); System.setProperty("cassandra.storage_dir", storageDir); // end: 3.x API change yaml.dump(cassandraConfiguration, cassandraConfigurationOutput); System.setProperty("cassandra.config", new File(cassandraDirName, CASSANDRA_YAML_FILE).toURI().toString()); System.setProperty("cassandra-foreground", "true"); DatabaseDescriptor.createAllDirectories(); return nativeTransportPort; } catch (IOException ioe) { throw new RuntimeException(ioe); } finally { Closeables.closeQuietly(cassandraConfigurationInput); try { Closeables.close(cassandraConfigurationOutput, true); } catch (IOException ignore) { } } }
From source file:eu.interedition.web.text.TextController.java
protected void annotate(Text text, Reader annotations) throws IOException { final JsonParser jp = objectMapper.getJsonFactory().createJsonParser(annotations); try {//from w w w .j a v a2 s. c o m jsonSerializer.unserialize(jp, text); } finally { Closeables.close(jp, false); } }
From source file:org.qcri.pca.ReconstructionErrJob.java
public void loadResults(Path outDirPath, Configuration conf) throws IOException { Path finalNumberFile = new Path(outDirPath, "part-r-00000"); SequenceFileIterator<IntWritable, DoubleWritable> iterator = new SequenceFileIterator<IntWritable, DoubleWritable>( finalNumberFile, true, conf); try {//from w w w .j a v a 2 s . c o m while (iterator.hasNext()) { Pair<IntWritable, DoubleWritable> next = iterator.next(); readIndividualResult(next.getFirst().get(), next.getSecond().get()); } } finally { Closeables.close(iterator, false); } }
From source file:org.jclouds.examples.google.computeengine.CreateServer.java
/** * Always close your service when you're done with it. *//*from w w w.ja v a 2s.c o m*/ public final void close() throws IOException { Closeables.close(computeService.getContext(), true); }
From source file:be.wimsymons.intellij.polopolyimport.PPImporter.java
private void postData(final String name, final Reader reader, final String contentType, final String extraParams) throws IOException { Writer writer = null;// w w w . j a v a 2s .co m LOGGER.info("Doing HTTP POST for " + name); try { URL httpURL = buildURL(target, extraParams); HttpURLConnection httpConnection = (HttpURLConnection) httpURL.openConnection(); httpConnection.setDoInput(true); httpConnection.setDoOutput(true); httpConnection.setUseCaches(false); httpConnection.setRequestMethod("PUT"); httpConnection.setRequestProperty("Content-Type", contentType); httpConnection.setConnectTimeout(2000); httpConnection.setReadTimeout(60000); httpConnection.connect(); if (contentType.contains("UTF-8")) { copyAndFlush(reader, httpConnection.getOutputStream()); } else { writer = new OutputStreamWriter(httpConnection.getOutputStream()); CharStreams.copy(reader, writer); writer.flush(); } int responseCode = httpConnection.getResponseCode(); String responseMessage = httpConnection.getResponseMessage(); if (responseCode < 200 || responseCode >= 300) { failureCount++; PPImportPlugin.doNotify("Import of " + name + " failed: " + responseCode + " - " + responseMessage + "\nCheck the server log for more details.", NotificationType.ERROR); } else { successCount++; } } catch (IOException e) { failureCount++; PPImportPlugin.doNotify("Import of " + name + " failed: " + e.getMessage(), NotificationType.ERROR); } finally { Closeables.close(reader, true); Closeables.close(writer, true); } }