List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:com.google.android.stardroid.data.AsciiToBinaryProtoWriter.java
public static void main(String[] args) throws IOException { if (args.length != 1 || !args[0].endsWith(".ascii")) { System.out.println("Usage: AsciiToBinaryProtoWriter <inputprefix>.ascii"); System.exit(1);/*from w w w . j av a 2 s . co m*/ } FileReader in = null; FileOutputStream out = null; try { in = new FileReader(args[0]); AstronomicalSourcesProto.Builder builder = AstronomicalSourcesProto.newBuilder(); TextFormat.merge(in, builder); out = new FileOutputStream(args[0].substring(0, args[0].length() - 6) + ".binary"); AstronomicalSourcesProto sources = builder.build(); System.out.println("Source count " + sources.getSourceCount()); sources.writeTo(out); } finally { Closeables.closeQuietly(in); Closeables.close(out, false); } }
From source file:com.github.adeshmukh.pom2fs.Main.java
/** * @param args//from www . j av a 2 s . c om */ public static void main(String[] args) throws Exception { InputStream is = null; try { clip.parseArgument(args); if (cli.showUsage()) { clip.printUsage(out); } else { Injector injector = Guice.createInjector(new Xml2fsModule(cli.getOutputDir()), new PomExtractorModule()); Xml2fs xml2fs = injector.getInstance(Xml2fs.class); xml2fs.run(cli.getInputStream()); } } catch (Exception e) { handleError(e); } finally { Closeables.closeQuietly(is); } }
From source file:com.jdom.mediadownloader.MediaDownloader.java
public static void main(String[] args) { if (args.length != 1) { throw new IllegalArgumentException( "You must pass the location to the properties file to the application!"); }/*from w w w . ja v a 2 s . co m*/ File file = new File(args[0]); Properties properties = new Properties(); FileReader fileReader = null; try { fileReader = new FileReader(file); properties.load(fileReader); } catch (Exception e) { throw new RuntimeException(e); } finally { Closeables.closeQuietly(fileReader); } System.getProperties().putAll(properties); initializeContext(); MediaDownloader mediaDownloader = ctx.getBean(MediaDownloader.class); mediaDownloader.processDownloads(); }
From source file:zookeeper.example.cache.PathCacheExample.java
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null;// www . j a va 2s . c o m PathChildrenCache cache = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); // in this example we will cache data. Notice that this is optional. cache = new PathChildrenCache(client, PATH, true); cache.start(); processCommands(client, cache); } finally { Closeables.closeQuietly(cache); Closeables.closeQuietly(client); Closeables.closeQuietly(server); } }
From source file:zookeeper.example.leader.LeaderSelectorExample.java
public static void main(String[] args) throws Exception { // all of the useful sample code is in ExampleClient.java System.out.println("Create " + CLIENT_QTY + " clients, have each negotiate for leadership and then wait a random number of seconds before letting another leader election occur."); System.out.println(//from w w w .j a va 2 s . c om "Notice that leader election is fair: all clients will become leader and will do so the same number of times."); List<CuratorFramework> clients = Lists.newArrayList(); List<ExampleClient> examples = Lists.newArrayList(); TestingServer server = new TestingServer(); try { for (int i = 0; i < CLIENT_QTY; ++i) { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); clients.add(client); ExampleClient example = new ExampleClient(client, PATH, "Client #" + i); examples.add(example); client.start(); example.start(); } System.out.println("Press enter/return to quit\n"); new BufferedReader(new InputStreamReader(System.in)).readLine(); } finally { System.out.println("Shutting down..."); for (ExampleClient exampleClient : examples) { Closeables.closeQuietly(exampleClient); } for (CuratorFramework client : clients) { Closeables.closeQuietly(client); } Closeables.closeQuietly(server); } }
From source file:com.anjuke.romar.train.LocalFileTrainer.java
public static void main(String[] args) { RomarConfig config = RomarConfig.getInstance(); LOG.info("init dataModel"); DataModel dataModel = PersistenceDataModelFactory.createDataModel(config); MahoutServiceFactory serviceFactory = config.getMahoutServiceFactory(); LOG.info("init in memory similarity"); ReadableSimilarity similarity = serviceFactory.createReadableSimilarity(dataModel); FastByIDMap<FastByIDMap<Double>> similarityDataMap = similarity.getSimilarityMaps(); SimilarityFileWriter writer = null;//from w w w . j ava 2 s.c o m LOG.info("start write to " + config.getSimilarityFile()); try { writer = SimilarityFileWriterFactory.createFileWriter(config); for (Map.Entry<Long, FastByIDMap<Double>> idValueEntry : similarityDataMap.entrySet()) { long id1 = idValueEntry.getKey(); for (Map.Entry<Long, Double> entry : idValueEntry.getValue().entrySet()) { long id2 = entry.getKey(); double value = entry.getValue(); writer.write(id1, id2, value); } } } catch (IOException e) { LOG.error(e.getMessage(), e); } finally { Closeables.closeQuietly(writer); } LOG.info("train finish"); }
From source file:locking.LockingExample.java
public static void main(String[] args) throws Exception { // all of the useful sample code is in ExampleClientThatLocks.java // FakeLimitedResource simulates some external resource that can only be access by one process at a time final FakeLimitedResource resource = new FakeLimitedResource(); ExecutorService service = Executors.newFixedThreadPool(QTY); final TestingServer server = new TestingServer(); try {/*from w ww.j a v a 2 s .c o m*/ for (int i = 0; i < QTY; ++i) { final int index = i; Callable<Void> task = new Callable<Void>() { @Override public Void call() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); try { client.start(); ExampleClientThatLocks example = new ExampleClientThatLocks(client, PATH, resource, "Client " + index); for (int j = 0; j < REPETITIONS; ++j) { example.doWork(10, TimeUnit.SECONDS); } } catch (Throwable e) { e.printStackTrace(); } finally { Closeables.closeQuietly(client); } return null; } }; service.submit(task); } service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); } finally { Closeables.closeQuietly(server); } }
From source file:com.babyduncan.lock.LockingExample.java
public static void main(String[] args) throws Exception { // all of the useful sample code is in ExampleClientThatLocks.java // FakeLimitedResource simulates some external resource that can only be access by one process at a time final FakeLimitedResource resource = new FakeLimitedResource(); ExecutorService service = Executors.newFixedThreadPool(QTY); try {/*from ww w. j av a 2 s . c om*/ for (int i = 0; i < QTY; ++i) { final int index = i; Callable<Void> task = new Callable<Void>() { @Override public Void call() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3)); try { client.start(); ExampleClientThatLocks example = new ExampleClientThatLocks(client, PATH, resource, "Client " + index); for (int j = 0; j < REPETITIONS; ++j) { example.doWork(10, TimeUnit.SECONDS); } } catch (Throwable e) { e.printStackTrace(); } finally { Closeables.closeQuietly(client); } return null; } }; service.submit(task); } Thread.sleep(5000); System.out.println("result is " + i); // result is 250 ?i service.shutdown(); service.awaitTermination(10, TimeUnit.MINUTES); } finally { } }
From source file:ch.ledcom.log4jtools.Log4JXMLCategorizer.java
public static void main(String[] args) throws IOException, InvalidFormatException { JCOptions options = new JCOptions(); new JCommander(options, args); List<CategorizationFilter> filters = readConfig(options.getConfigFile()); CategorizingProcessor processor = new CategorizingProcessor(filters, initAppenders(options.getOutputDirectory(), filters)); List<LogProcessor> processors = new ArrayList<LogProcessor>(); processors.add(processor);/* w ww . ja v a 2 s.c om*/ LogFileXMLReader reader = new LogFileXMLReader(processors); if (options.getInputFiles().size() == 0) { reader.process(new BufferedReader(new InputStreamReader(System.in)), options.getHost(), options.getApplication()); } else { for (File f : getLogFiles(options.getInputFiles())) { BufferedReader in = new BufferedReader(new FileReader(f)); try { reader.process(in, options.getHost(), options.getApplication()); } finally { Closeables.closeQuietly(in); } } } }
From source file:zookeeper.example.discovery.DiscoveryExample.java
public static void main(String[] args) throws Exception { // This method is scaffolding to get the example up and running TestingServer server = new TestingServer(); CuratorFramework client = null;/*from w ww . j a v a 2 s . c o m*/ ServiceDiscovery<InstanceDetails> serviceDiscovery = null; Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap(); try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>( InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH) .serializer(serializer).build(); serviceDiscovery.start(); processCommands(serviceDiscovery, providers, client); } finally { for (ServiceProvider<InstanceDetails> cache : providers.values()) { Closeables.closeQuietly(cache); } Closeables.closeQuietly(serviceDiscovery); Closeables.closeQuietly(client); Closeables.closeQuietly(server); } }