Example usage for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue

List of usage examples for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue

Introduction

In this page you can find the example usage for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue.

Prototype

public ArrayBlockingQueue(int capacity) 

Source Link

Document

Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy.

Usage

From source file:org.meresco.triplestore.VirtuosoServer.java

public static void main(String[] args) throws Exception {
    Option option;/*  ww w  . j  a  v a2  s .com*/

    Options options = new Options();

    option = new Option("p", "port", true, "Port number");
    option.setType(Integer.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option("d", "stateDir", true, "State directory for backup e.d.");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option(null, "hostname", true, "Hostame of the virtuoso instance");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option(null, "odbcPort", true, "Odbc port number of the virtuoso instance");
    option.setType(Integer.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option(null, "username", true, "Username of the virtuoso instance");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option(null, "password", true, "Password of the virtuoso instance");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    PosixParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (MissingOptionException e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("start-virtuoso", options);
        System.exit(1);
    }

    Integer port = new Integer(commandLine.getOptionValue("p"));
    String stateDir = commandLine.getOptionValue("d");
    String hostname = commandLine.getOptionValue("hostname");
    Integer odbcPort = new Integer(commandLine.getOptionValue("odbcPort"));
    String username = commandLine.getOptionValue("username");
    String password = commandLine.getOptionValue("password");
    Boolean disableTransactionLog = commandLine.hasOption("disableTransactionLog");

    if (Charset.defaultCharset() != Charset.forName("UTF-8")) {
        System.err.println("file.encoding must be UTF-8.");
        System.exit(1);
    }

    Triplestore tripleStore = new VirtuosoTriplestore(new File(stateDir), hostname, odbcPort, username,
            password);

    ExecutorThreadPool pool = new ExecutorThreadPool(50, 200, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1000));
    Server server = new Server(pool);

    ContextHandler context = new ContextHandler("/");
    context.setHandler(new HttpHandler(tripleStore));

    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory());
    http.setPort(port);
    server.addConnector(http);

    System.out.println("Triplestore started with " + String.valueOf(tripleStore.size()) + " statements");
    System.out.flush();

    server.setHandler(context);
    server.start();
    server.join();
}

From source file:ca.ualberta.exemplar.core.Exemplar.java

public static void main(String[] rawArgs) throws FileNotFoundException, UnsupportedEncodingException {

    CommandLineParser cli = new BasicParser();

    Options options = new Options();
    options.addOption("h", "help", false, "shows this message");
    options.addOption("b", "benchmark", true, "expects input to be a benchmark file (type = binary | nary)");
    options.addOption("p", "parser", true, "defines which parser to use (parser = stanford | malt)");

    CommandLine line = null;/*from  w w  w.ja v  a  2s. c om*/

    try {
        line = cli.parse(options, rawArgs);
    } catch (ParseException exp) {
        System.err.println(exp.getMessage());
        System.exit(1);
    }

    String[] args = line.getArgs();
    String parserName = line.getOptionValue("parser", "malt");

    if (line.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("sh ./exemplar", options);
        System.exit(0);
    }

    if (args.length != 2) {
        System.out.println("error: exemplar requires an input file and output file.");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("sh ./exemplar <input> <output>", options);
        System.exit(0);
    }

    File input = new File(args[0]);
    File output = new File(args[1]);

    String benchmarkType = line.getOptionValue("benchmark", "");
    if (!benchmarkType.isEmpty()) {
        if (benchmarkType.equals("binary")) {
            BenchmarkBinary evaluation = new BenchmarkBinary(input, output, parserName);
            evaluation.runAndTime();
            System.exit(0);
        } else {
            if (benchmarkType.equals("nary")) {
                BenchmarkNary evaluation = new BenchmarkNary(input, output, parserName);
                evaluation.runAndTime();
                System.exit(0);
            } else {
                System.out.println("error: benchmark option has to be either 'binary' or 'nary'.");
                System.exit(0);
            }
        }
    }

    Parser parser = null;
    if (parserName.equals("stanford")) {
        parser = new ParserStanford();
    } else {
        if (parserName.equals("malt")) {
            parser = new ParserMalt();
        } else {
            System.out.println(parserName + " is not a valid parser.");
            System.exit(0);
        }
    }

    System.out.println("Starting EXEMPLAR...");

    RelationExtraction exemplar = null;
    try {
        exemplar = new RelationExtraction(parser);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    BlockingQueue<String> inputQueue = new ArrayBlockingQueue<String>(QUEUE_SIZE);
    PlainTextReader reader = null;
    reader = new PlainTextReader(inputQueue, input);

    Thread readerThread = new Thread(reader);
    readerThread.start();

    PrintStream statementsOut = null;

    try {
        statementsOut = new PrintStream(output, "UTF-8");
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        System.exit(0);
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        System.exit(0);
    }

    statementsOut.println("Subjects\tRelation\tObjects\tNormalized Relation\tSentence");

    while (true) {
        String doc = null;
        try {
            doc = inputQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (doc.isEmpty()) {
            break;
        }

        List<RelationInstance> instances = exemplar.extractRelations(doc);

        for (RelationInstance instance : instances) {

            // Output SUBJ arguments in a separate field, for clarity
            boolean first = true;
            for (Argument arg : instance.getArguments()) {
                if (arg.argumentType.equals("SUBJ")) {
                    if (first) {
                        first = false;
                    } else {
                        statementsOut.print(",,");
                    }
                    statementsOut.print(arg.argumentType + ":" + arg.entityId);
                }
            }

            // Output the original relation
            statementsOut.print("\t" + instance.getOriginalRelation() + "\t");

            // Output the DOBJ arguments, followed by POBJ
            first = true;
            for (Argument arg : instance.getArguments()) {
                if (arg.argumentType.equals("DOBJ")) {
                    if (first) {
                        first = false;
                    } else {
                        statementsOut.print(",,");
                    }
                    statementsOut.print(arg.argumentType + ":" + arg.entityId);
                }
            }
            for (Argument arg : instance.getArguments()) {
                if (arg.argumentType.startsWith("POBJ")) {
                    if (first) {
                        first = false;
                    } else {
                        statementsOut.print(",,");
                    }
                    statementsOut.print(arg.argumentType + ":" + arg.entityId);
                }
            }
            statementsOut.print("\t" + instance.getNormalizedRelation());
            statementsOut.print("\t" + instance.getSentence());
            statementsOut.println();
        }
    }

    System.out.println("Done!");
    statementsOut.close();

}

From source file:org.meresco.triplestore.GraphDBServer.java

public static void main(String[] args) throws Exception {
    Option option;//from   w w  w . j a  v  a  2 s .c  o m

    Options options = new Options();

    // Port Number
    option = new Option("p", "port", true, "Port number");
    option.setType(Integer.class);
    option.setRequired(true);
    options.addOption(option);

    // Triplestore name
    option = new Option("n", "name", true, "Name of the triplestore. Defaults to triplestore");
    option.setType(String.class);
    option.setRequired(false);
    options.addOption(option);

    // Triplestore location
    option = new Option("d", "stateDir", true, "Directory in which triplestore is located");
    option.setType(String.class);
    option.setRequired(true);
    options.addOption(option);

    option = new Option(null, "cacheMemory", true, "Cache size; Usually half -Xmx");
    option.setType(String.class);
    option.setRequired(false);
    options.addOption(option);

    option = new Option(null, "entityIndexSize", true,
            "Entity index size. Usually half the number of entities (Uri's, blank nodes, literals). Cannot be changed after indexing.");
    option.setType(Integer.class);
    option.setRequired(false);
    options.addOption(option);

    option = new Option(null, "queryTimeout", true, "Query timeout in seconds. Defaults to 0 (no limit)");
    option.setType(Integer.class);
    option.setRequired(false);
    options.addOption(option);

    option = new Option(null, "maxCommitCount", true, "Max number of commits for updates.");
    option.setType(Integer.class);
    option.setRequired(false);
    options.addOption(option);

    option = new Option(null, "maxCommitTimeout", true, "Maximum seconds after update for a commit.");
    option.setType(Integer.class);
    option.setRequired(false);
    options.addOption(option);

    PosixParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (MissingOptionException e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("start-graphdb", options);
        System.exit(1);
    }

    Integer port = new Integer(commandLine.getOptionValue("p"));
    String storeLocation = commandLine.getOptionValue("d");
    String storeName = commandLine.getOptionValue("n");
    if (storeName == null)
        storeName = "triplestore";
    String cacheMemory = commandLine.getOptionValue("cacheMemory");
    String entityIndexSize = commandLine.getOptionValue("entityIndexSize");
    String queryTimeout = commandLine.getOptionValue("queryTimeout");
    if (queryTimeout == null)
        queryTimeout = "0";
    String maxCommitCount = commandLine.getOptionValue("maxCommitCount");
    String maxCommitTimeout = commandLine.getOptionValue("maxCommitTimeout");

    if (Charset.defaultCharset() != Charset.forName("UTF-8")) {
        System.err.println("file.encoding must be UTF-8.");
        System.exit(1);
    }

    long startTime = System.currentTimeMillis();
    GraphDBTriplestore tripleStore = new GraphDBTriplestore(new File(storeLocation), storeName, cacheMemory,
            entityIndexSize, queryTimeout);
    if (maxCommitCount != null)
        tripleStore.setMaxCommitCount(Integer.parseInt(maxCommitCount));
    if (maxCommitTimeout != null)
        tripleStore.setMaxCommitTimeout(Integer.parseInt(maxCommitTimeout));

    ExecutorThreadPool pool = new ExecutorThreadPool(50, 200, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1000));
    Server server = new Server(pool);

    ContextHandler context = new ContextHandler("/");
    context.setHandler(new HttpHandler(tripleStore));

    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory());
    http.setPort(port);
    server.addConnector(http);

    registerShutdownHandler(tripleStore, server);

    server.setHandler(context);
    server.start();
    server.join();
}

From source file:com.betfair.cougar.test.socket.app.SocketCompatibilityTestingApp.java

public static void main(String[] args) throws Exception {

    Parser parser = new PosixParser();
    Options options = new Options();
    options.addOption("r", "repo", true, "Repository type to search: local|central");
    options.addOption("c", "client-concurrency", true,
            "Max threads to allow each client tester to run tests, defaults to 10");
    options.addOption("t", "test-concurrency", true, "Max client testers to run concurrently, defaults to 5");
    options.addOption("m", "max-time", true,
            "Max time (in minutes) to allow tests to complete, defaults to 10");
    options.addOption("v", "version", false, "Print version and exit");
    options.addOption("h", "help", false, "This help text");
    CommandLine commandLine = parser.parse(options, args);
    if (commandLine.hasOption("h")) {
        System.out.println(options);
        System.exit(0);/*from w  w w.j  ava  2  s .  c o  m*/
    }
    if (commandLine.hasOption("v")) {
        System.out.println("How the hell should I know?");
        System.exit(0);
    }
    // 1. Find all testers in given repos
    List<RepoSearcher> repoSearchers = new ArrayList<>();
    for (String repo : commandLine.getOptionValues("r")) {
        if ("local".equals(repo.toLowerCase())) {
            repoSearchers.add(new LocalRepoSearcher());
        } else if ("central".equals(repo.toLowerCase())) {
            repoSearchers.add(new CentralRepoSearcher());
        } else {
            System.err.println("Unrecognized repo: " + repo);
            System.err.println(options);
            System.exit(1);
        }
    }
    int clientConcurrency = 10;
    if (commandLine.hasOption("c")) {
        try {
            clientConcurrency = Integer.parseInt(commandLine.getOptionValue("c"));
        } catch (NumberFormatException nfe) {
            System.err.println(
                    "client-concurrency is not a valid integer: '" + commandLine.getOptionValue("c") + "'");
            System.exit(1);
        }
    }
    int testConcurrency = 5;
    if (commandLine.hasOption("t")) {
        try {
            testConcurrency = Integer.parseInt(commandLine.getOptionValue("t"));
        } catch (NumberFormatException nfe) {
            System.err.println(
                    "test-concurrency is not a valid integer: '" + commandLine.getOptionValue("t") + "'");
            System.exit(1);
        }
    }
    int maxMinutes = 10;
    if (commandLine.hasOption("m")) {
        try {
            maxMinutes = Integer.parseInt(commandLine.getOptionValue("m"));
        } catch (NumberFormatException nfe) {
            System.err.println("max-time is not a valid integer: '" + commandLine.getOptionValue("m") + "'");
            System.exit(1);
        }
    }

    Properties clientProps = new Properties();
    clientProps.setProperty("client.concurrency", String.valueOf(clientConcurrency));

    File baseRunDir = new File(System.getProperty("user.dir") + "/run");
    baseRunDir.mkdirs();

    File tmpDir = new File(baseRunDir, "jars");
    tmpDir.mkdirs();

    List<ServerRunner> serverRunners = new ArrayList<>();
    List<ClientRunner> clientRunners = new ArrayList<>();
    for (RepoSearcher searcher : repoSearchers) {
        List<File> jars = searcher.findAndCache(tmpDir);
        for (File f : jars) {
            ServerRunner serverRunner = new ServerRunner(f, baseRunDir);
            System.out.println("Found tester: " + serverRunner.getVersion());
            serverRunners.add(serverRunner);
            clientRunners.add(new ClientRunner(f, baseRunDir, clientProps));
        }
    }

    // 2. Start servers and collect ports
    System.out.println();
    System.out.println("Starting " + serverRunners.size() + " servers...");
    for (ServerRunner server : serverRunners) {
        server.startServer();
    }
    System.out.println();

    List<TestCombo> tests = new ArrayList<>(serverRunners.size() * clientRunners.size());
    for (ServerRunner server : serverRunners) {
        for (ClientRunner client : clientRunners) {
            tests.add(new TestCombo(server, client));
        }
    }

    System.out.println("Enqueued " + tests.size() + " test combos to run...");

    long startTime = System.currentTimeMillis();
    // 3. Run every client against every server, collecting results
    BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(serverRunners.size() * clientRunners.size());
    ThreadPoolExecutor service = new ThreadPoolExecutor(testConcurrency, testConcurrency, 5000,
            TimeUnit.MILLISECONDS, workQueue);
    service.prestartAllCoreThreads();
    workQueue.addAll(tests);
    while (!workQueue.isEmpty()) {
        Thread.sleep(1000);
    }
    service.shutdown();
    service.awaitTermination(maxMinutes, TimeUnit.MINUTES);
    long endTime = System.currentTimeMillis();
    long totalTimeSecs = Math.round((endTime - startTime) / 1000.0);
    for (ServerRunner server : serverRunners) {
        server.shutdownServer();
    }

    System.out.println();
    System.out.println("=======");
    System.out.println("Results");
    System.out.println("-------");
    // print a summary
    int totalTests = 0;
    int totalSuccess = 0;
    for (TestCombo combo : tests) {
        String clientVer = combo.getClientVersion();
        String serverVer = combo.getServerVersion();
        String results = combo.getClientResults();
        ObjectMapper mapper = new ObjectMapper(new JsonFactory());
        JsonNode node = mapper.reader().readTree(results);
        JsonNode resultsArray = node.get("results");
        int numTests = resultsArray.size();
        int numSuccess = 0;
        for (int i = 0; i < numTests; i++) {
            if ("success".equals(resultsArray.get(i).get("result").asText())) {
                numSuccess++;
            }
        }
        totalSuccess += numSuccess;
        totalTests += numTests;
        System.out.println(clientVer + "/" + serverVer + ": " + numSuccess + "/" + numTests
                + " succeeded - took " + String.format("%2f", combo.getRunningTime()) + " seconds");
    }
    System.out.println("-------");
    System.out.println(
            "Overall: " + totalSuccess + "/" + totalTests + " succeeded - took " + totalTimeSecs + " seconds");

    FileWriter out = new FileWriter("results.json");
    PrintWriter pw = new PrintWriter(out);

    // 4. Output full results
    pw.println("{\n  \"results\": [");
    for (TestCombo combo : tests) {
        combo.emitResults(pw, "    ");
    }
    pw.println("  ],");
    pw.println("  \"servers\": [");
    for (ServerRunner server : serverRunners) {
        server.emitInfo(pw, "    ");
    }
    pw.println("  ],");
    pw.close();
}

From source file:net.tjado.jcdbe.jcdbe.java

public static void main(String[] args) throws Exception {
    // print banner
    System.out.println("------------------------------------------");
    System.out.println("| Java Central DataBase Engine v0.4 beta |");
    System.out.println("------------------------------------------");

    // path to the ini configuration
    String configPath = workingDir + "/config/jcdbe.ini";

    // If the first CLI argument doesn't begin with "-" it must be a config file.
    // We need to do this, to have the possibility to specify other ini config files
    // The ini config file is necessary for the input/output classes, which again specify the
    // required CLI arguments
    if (args.length != 0 && !args[0].startsWith("-")) {
        configPath = args[0];//w w  w.j av a 2 s . com
        // remove first argument from CLI arguments array, so it won't be validated by CLI argument
        // parser
        args = Arrays.copyOfRange(args, 1, args.length);
    }

    // initialize the ini configuration to get required parameters
    Ini config = initConfig(configPath);

    // initialize Logger
    log.init(log4jPropertyFile);

    // setting jdbc property file
    DatabaseOracle.setPropertyFile(jdbcPropertyeFile);

    // declare the input/output classes
    Input input = (Input) Class.forName(inputClass).getDeclaredMethod("getInstance").invoke(null,
            (Object[]) null);
    Output output = (Output) Class.forName(outputClass).getDeclaredMethod("getInstance").invoke(null,
            (Object[]) null);

    // declare options and parser for the CLI arguments
    Options options = new Options();
    CommandLineParser parser = new PosixParser();

    // add "help" CLI argument
    options.addOption("h", "help", false, "print this usage information");

    // add further CLI arguments by the input/output classes
    input.setCLI(options);
    output.setCLI(options);

    CommandLine cli = null;
    try {
        // parse the CLI arguments
        cli = parser.parse(options, args);

        if (cli.hasOption("help") || cli.getOptions().length == 0) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar jcdbe.jar [options]", options);

            System.exit(1);
        }
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar jcdbe.jar [options]", options);

        System.exit(1);
    }

    // output engine config after initializing of Logger
    log.debug("[CONFIG] Working Directory: " + workingDir);
    log.debug("[CONFIG] Input class:  " + inputClass);
    log.debug("[CONFIG] Output class: " + outputClass);
    log.debug("[CONFIG] JDBC URL prefix: " + jdbcPrefix);
    log.debug("[CONFIG] Java Library Path: " + System.getProperty("java.library.path"));
    log.debug("[CONFIG] Oracle SDU size: " + sduSize);
    log.debug("[CONFIG] Max. threads: " + threadMax);
    log.debug("[CONFIG] Max. running threads: " + threadRun);
    log.debug("[CONFIG] Thread idle timeout: " + threadTTL);
    log.debug("[CONFIG] Advanced Debugging: " + advDebugging);

    // validate Input arguments
    input.validateParameters(cli, config);
    // validate Output arguments
    output.validateParameters(cli, config);

    // start benchmark time
    measureTimeStart();

    log.info("[INPUT] Initialization");
    // run input init and check if it was successfull....
    if (!input.init()) {
        log.fatal("Error during input init...");
        System.exit(2);
    }
    log.info("[OUTPUT] Initialization");
    // run output init and check if it was successfull....
    if (!output.init()) {
        log.fatal("[OUTPUT] Error during output init...");
        System.exit(3);
    }

    // init thread pool
    workQueue = new ArrayBlockingQueue<Runnable>(99999);
    ThreadPoolExecutor threads = new ThreadPoolExecutor(threadRun, threadMax, threadTTL, TimeUnit.SECONDS,
            workQueue);

    // get DatabaseList object which will manage all database infos (url, username, pw, status...)
    DatabaseList dbList = input.getDatabaseList();

    if (dbList.size() == 0) {
        log.info("[QUEUE] database list is empty... nothing do to.");
        System.exit(1);
    }

    // get all SQL queries to execute
    // Integer = Query ID
    // String = SQL Text
    Map<Integer, String> queries = input.getQueries();

    log.info("[QUEUE] Starting Threads");

    // loop thru dbList to create & execute/queue all threads
    for (Integer id : dbList) {
        try {
            // create new runnable instance
            DatabaseThreadSlave slaveThread = new DatabaseThreadSlave(id, dbList, queries, output, jdbcPrefix,
                    sduSize);
            // insert runnable instance into dbList
            dbList.setThread(id, slaveThread);

            // add runnable instance into thread pool queue
            threads.execute(slaveThread);
        } catch (Exception e) {
            advDebug(e);
            log.warn("Exception in thread-starter loop (DBID: " + id + "): " + e.getMessage());
        }
    }

    //
    // waiting for all threads to complete
    //
    // the timeout handling will be done completely over JDBC
    // see docs for more information
    //

    while (!dbList.isFinished() || threads.getActiveCount() > 0) {
        Thread.sleep(500);
    }

    log.info("[QUEUE] Shutting down all threads");
    threads.shutdown();

    Thread.sleep(2000);

    log.info("[INPUT] close input...");
    input.close();

    log.info("[OUTPUT] close output...");
    output.close();

    // end time-benchmark and output
    measureTimeEnd();

    // rc=0
    System.exit(0);
}

From source file:Main.java

public static BlockingQueue<Runnable> makeQueue(Integer capacity) {
    if ((capacity == null) || (capacity <= 0) || (capacity == Integer.MAX_VALUE)) {
        return new LinkedBlockingQueue<Runnable>();
    } else {/*w w w . jav a  2 s. c om*/
        return new ArrayBlockingQueue<Runnable>(capacity);
    }
}

From source file:org.timconrad.vmstats.Main.java

public static void main(String[] args) {

    Logger logger = LoggerFactory.getLogger(Main.class);
    Properties config = new Properties();
    Boolean showPerfMgr = false;//w  w  w. j a  va 2  s .c o  m
    Boolean showEstimate = false;
    Boolean noThreads = false;
    Boolean noGraphite = false;

    File configFile = new File("vmstats.properties");

    Hashtable<String, String> appConfig = new Hashtable<String, String>();

    CommandLineParser parser = new PosixParser();
    Options options = new Options();

    options.addOption("P", "perfMgr", false, "Display Performance Manager Counters and exit");
    options.addOption("E", "estimate", false, "Estimate the # of counters written to graphite and exit");
    options.addOption("N", "noThreads", false,
            "Don't start any threads, just run the main part (helpful for troubleshooting initial issues");
    options.addOption("g", "noGraphite", false, "Don't send anything to graphite");
    options.addOption("c", "configFile", true,
            "Configuration file for vmstats - defaults to 'vmstats.properties' in the .jar directory");
    options.addOption("O", "runOnce", false, "Run the stats gatherer one time - useful for debugging");
    options.addOption("D", "debugOutput", false, "Dump the output to a thread-named file.");
    options.addOption("h", "help", false, "show help");

    try {
        CommandLine line = parser.parse(options, args);
        if (line.hasOption("help")) {
            System.out.println("vmstats.jar -Dlog4j.configuration=file:/path/to/log4j.properties [options]");
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("vmstats.jar", options);
            System.exit(0);
        }
        if (line.hasOption("perfMgr")) {
            showPerfMgr = true;
        }
        if (line.hasOption("estimate")) {
            showEstimate = true;
        }
        if (line.hasOption("noThreads")) {
            noThreads = true;
        }
        if (line.hasOption("noGraphite")) {
            noGraphite = true;
        }
        if (line.hasOption("runOnce")) {
            appConfig.put("runOnce", "true");

        } else {
            appConfig.put("runOnce", "false");
        }
        if (line.hasOption("debugOutput")) {
            appConfig.put("debugOutput", "true");
        } else {
            appConfig.put("debugOutput", "false");
        }

        if (line.hasOption("configFile")) {
            // if the user adds a custom config flag, use it. Otherwise they'll get the default.
            String file = line.getOptionValue("configFile");
            File optFile = new File(file);
            boolean exists = optFile.exists();
            // check to make sure the file exists.
            if (!exists) {
                System.out.println("The configuration file doesn't seem to exist in path: " + file);
                System.exit(0);
            } else {
                configFile = optFile;
            }

        }
    } catch (org.apache.commons.cli.ParseException e) {
        System.out.println("CLI options exception: " + e.getMessage());
        e.printStackTrace();
    }

    try {
        config.load(new FileInputStream(configFile));
    } catch (FileNotFoundException e) {
        logger.info("Configuration file not found!\n\tException: " + e);
        System.exit(-1);
    } catch (IOException e) {
        logger.info("Configuration file not found!\n\tException: " + e);
        System.exit(-1);
    }

    Enumeration configOpts = config.propertyNames();
    // this will have to be manually updated.
    String[] expectedOptions = { "VCS_TAG", "VCS_USER", "GRAPHITE_PORT", "GRAPHITE_TAG", "VCS_HOST", "VCS_PASS",
            "MAX_VMSTAT_THREADS", "GRAPHITE_HOST", "ESX_STATS", "USE_FQDN", "SLEEP_TIME", "SEND_ALL_ABSOLUTE",
            "SEND_ALL_DELTA", "SEND_ALL_PERIODS" };
    ArrayList<String> matchedOptions = new ArrayList<String>();
    while (configOpts.hasMoreElements()) {
        String optTmp = (String) configOpts.nextElement();
        for (int i = 0; i < expectedOptions.length; i++) {
            if (optTmp.equals(expectedOptions[i])) {
                matchedOptions.add(optTmp);
            }
        }
    }

    if (expectedOptions.length != matchedOptions.size()) {
        // this kinda blows, but better than throwing a null pointer exception
        // or doing try/catch for each possible option below.
        System.out.println("Configuration file options are missing");
        System.exit(-1);
    }

    // Get settings from config file
    String vcsHostRaw = config.getProperty("VCS_HOST");
    String vcsUser = config.getProperty("VCS_USER");
    String vcsPass = config.getProperty("VCS_PASS");
    String vcsTag = config.getProperty("VCS_TAG");
    String vcsFilter = config.getProperty("FILTERFILE");

    if (vcsFilter != null) {
        String filterfile = vcsFilter;
        File FilterFile = new File(filterfile);
        List<String> FilterList = null;
        try {
            FilterList = FileUtils.readLines(FilterFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FilterArrays = FilterList.toArray(new String[] {});
    }

    appConfig.put("vcsTag", vcsTag);
    // vcs information
    // this needs to be https://host/sdk
    String vcsHost = "https://" + vcsHostRaw + "/sdk";
    String graphEsx = config.getProperty("ESX_STATS");
    appConfig.put("USE_FQDN", config.getProperty("USE_FQDN"));
    appConfig.put("graphEsx", graphEsx);

    // graphite information
    String graphiteHost = config.getProperty("GRAPHITE_HOST");
    int graphitePort = Integer.parseInt(config.getProperty("GRAPHITE_PORT"));
    String graphiteTag = config.getProperty("GRAPHITE_TAG");

    try {
        appConfig.put("graphiteTag", graphiteTag);
    } catch (NullPointerException e) {
        System.out.println("Issue with configuration file - Missing GRAPHITE_TAG");
        System.exit(-1);
    }

    // TODO: make this dynamic. maybe.
    int MAX_VMSTAT_THREADS = Integer.parseInt(config.getProperty("MAX_VMSTAT_THREADS"));
    int MAX_ESXSTAT_THREADS = Integer.parseInt(config.getProperty("MAX_ESXSTAT_THREADS"));
    int MAX_GRAPHITE_THREADS = Integer.parseInt(config.getProperty("MAX_GRAPHITE_THREADS"));

    appConfig.put("MAX_VMSTAT_THREADS", String.valueOf(MAX_VMSTAT_THREADS));
    appConfig.put("MAX_ESXSTAT_THREADS", String.valueOf(MAX_ESXSTAT_THREADS));
    String SLEEP_TIME = config.getProperty("SLEEP_TIME");
    appConfig.put("SLEEP_TIME", SLEEP_TIME);
    String SEND_ALL_PERIODS = config.getProperty("SEND_ALL_PERIODS");
    appConfig.put("SEND_ALL_PERIODS", SEND_ALL_PERIODS);
    int sleep_time = Integer.parseInt(SLEEP_TIME);
    if ((sleep_time % 20) == 0) {
        int periods = sleep_time / 20;
        appConfig.put("PERIODS", String.valueOf(periods));
    } else {
        System.out.println("SLEEP_TIME needs to be divisible by 20, please fix");
        System.exit(-1);
    }
    appConfig.put("SEND_ALL_ABSOLUTE", config.getProperty("SEND_ALL_ABSOLUTE"));
    appConfig.put("SEND_ALL_DELTA", config.getProperty("SEND_ALL_DELTA"));

    // Build internal data structures. 

    // use a hashtable to store performance id information
    Hashtable<String, Hashtable<String, String>> perfKeys = new Hashtable<String, Hashtable<String, String>>();
    // BlockingQueue to store managed objects - basically anything that vmware knows about
    BlockingQueue<Object> vm_mob_queue = new ArrayBlockingQueue<Object>(10000);
    BlockingQueue<Object> esx_mob_queue = new ArrayBlockingQueue<Object>(10000);
    // BlockingQueue to store arrays of stats - each managed object generates a bunch of strings that are stored in
    BlockingQueue<Object> sender = new ArrayBlockingQueue<Object>(60000);

    // Initialize these vmware types as nulls so we can see if things work properly
    ServiceInstance si = null;
    PerformanceManager perfMgr = null;

    try {
        // TODO: this doesn't handle some ASCII characters well, not sure why.
        si = new ServiceInstance(new URL(vcsHost), vcsUser, vcsPass, true);
    } catch (InvalidLogin e) {
        logger.info("Invalid login vCenter: " + vcsHost + " User: " + vcsUser);
        System.exit(-1);
    } catch (RemoteException e) {
        logger.info("Remote exception: " + e);
        e.printStackTrace();
    } catch (MalformedURLException e) {
        logger.info("MalformedURLexception: " + e);
        e.printStackTrace();
    }

    if (si != null) {

        perfMgr = si.getPerformanceManager();
        PerfCounterInfo[] counters = perfMgr.getPerfCounter();
        // build a hash lookup to turn the counter 23 into 'disk.this.that.the.other'
        // These are not sequential.
        for (int i = 0; i < counters.length; i++) {
            // create a temp hash to push onto the big hash
            Hashtable<String, String> temp_hash = new Hashtable<String, String>();
            String path = counters[i].getGroupInfo().getKey() + "." + counters[i].getNameInfo().getKey();
            // this is a key like cpu.run.0.summation
            temp_hash.put("key", path);
            // one of average, latest, maximum, minimum, none,  summation
            temp_hash.put("rollup", counters[i].getRollupType().toString());
            // one of absolute, delta, rate
            temp_hash.put("statstype", counters[i].getStatsType().toString());
            // it's important to understand that the counters aren't sequential, so they have their own id.
            perfKeys.put("" + counters[i].getKey(), temp_hash);
        }
    } else {
        logger.info("Issues with the service instance that wasn't properly handled");
        System.exit(-1);
    }

    if (showPerfMgr) {
        // show the performance keys that are available to the user
        System.out.println("Showing Performance Counter Entities available:");
        System.out.println("Read the following link for more information:");
        System.out.println(
                "http://vijava.sourceforge.net/vSphereAPIDoc/ver5/ReferenceGuide/vim.PerformanceManager.html");
        Enumeration<String> keys = perfKeys.keys();
        System.out.println("ID|Tag|Rollup");
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            System.out
                    .println(key + "|" + perfKeys.get(key).get("key") + "|" + perfKeys.get(key).get("rollup"));
        }
        System.exit(0);
    }

    if (showEstimate) {
        // estimate the number of keys that will be updated/written to Graphite per minute
        System.out.println("Currently Disabled");
    }

    // this gets the lists of vm's from vCenter
    if (!noThreads) {
        if (si != null && perfMgr != null) {
            logger.info("ServiceInstance: " + si);
            logger.info("PerformanceManager: " + perfMgr);

            meGrabber me_grabber = new meGrabber(si, vm_mob_queue, esx_mob_queue, appConfig, sender);
            ExecutorService grab_exe = Executors.newCachedThreadPool();
            grab_exe.execute(me_grabber);

            // it's easier sometimes to debug things without stats being sent to graphite. make noGraphite = true; to 
            // change this.
            if (!noGraphite) {
                for (int i = 1; i <= MAX_GRAPHITE_THREADS; i++) {
                    GraphiteWriter graphite = new GraphiteWriter(graphiteHost, graphitePort, sender, appConfig);
                    ExecutorService graph_exe = Executors.newCachedThreadPool();
                    graph_exe.execute(graphite);
                }
            } else {
                System.out.println("Graphite output has been disabled via the -g flag.");
            }

            for (int i = 1; i <= MAX_VMSTAT_THREADS; i++) {
                statsGrabber vm_stats_grabber = new statsGrabber(perfMgr, perfKeys, vm_mob_queue, sender,
                        appConfig, "vm", FilterArrays);
                ExecutorService vm_stat_exe = Executors.newCachedThreadPool();
                vm_stat_exe.execute(vm_stats_grabber);
            }

            if (graphEsx.contains("true")) {
                for (int i = 1; i <= MAX_ESXSTAT_THREADS; i++) {
                    statsGrabber esx_stats_grabber = new statsGrabber(perfMgr, perfKeys, esx_mob_queue, sender,
                            appConfig, "ESX", FilterArrays);
                    ExecutorService esx_stat_exe = Executors.newCachedThreadPool();
                    esx_stat_exe.execute(esx_stats_grabber);
                }
            }

        } else {
            logger.info("Either ServiceInstance or PerformanceManager is null, bailing.");
        }
    } else {
        System.out.println("Not running any of the main threads");
        System.exit(0);
    }
}

From source file:Main.java

public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(final int capacity) {
    return new ArrayBlockingQueue<E>(capacity);
}

From source file:jlg.jade.example.RawQueue.java

public RawQueue() {
    this.rawQueue = new ArrayBlockingQueue<>(1000);
}

From source file:Main.java

public static ThreadPoolExecutor newLimitedThreadPool(int minNumberOfThreads, int maxNumberOfThreads,
        long defaultRemoveIdleThread, int bufferSize) {
    return new ThreadPoolExecutor(minNumberOfThreads, maxNumberOfThreads, defaultRemoveIdleThread,
            TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(bufferSize));
}