Example usage for java.util.concurrent ForkJoinPool isShutdown

List of usage examples for java.util.concurrent ForkJoinPool isShutdown

Introduction

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

Prototype

public boolean isShutdown() 

Source Link

Document

Returns true if this pool has been shut down.

Usage

From source file:com.hygenics.parser.JDump.java

private void toFile() {
    ArrayList<String> archs = new ArrayList<String>();
    List<Future<ArrayList<String>>> qfutures;
    Set<Callable<ArrayList<String>>> qcollect = new HashSet<Callable<ArrayList<String>>>(4);

    ForkJoinPool fjp = new ForkJoinPool((int) Math.ceil(Runtime.getRuntime().availableProcessors() * procnum));

    int dumped = 0;

    if (archive) {
        log.info("Cleaning");
        for (String k : fpaths.keySet()) {
            String fpath = "";

            for (String ofp : fpaths.get(k).keySet()) {
                fpath = ofp;/*from  w ww.j a  v a 2 s  .c om*/
            }

            if (fpath.length() > 0) {
                String[] barr = fpath.split("\\/");
                String basefile = "";
                Archiver zip = new Archiver();
                for (int i = 0; i > barr.length - 1; i++) {
                    basefile += (i == 0) ? barr[i] : "/" + barr[i];
                }
                if (basefile.trim().length() > 0) {
                    zip.setBasedirectory(basefile);
                    zip.setZipDirectory(basefile + "archive.zip");
                    zip.setAvoidanceString(".zip|archive");
                    zip.setDelFiles(true);
                    zip.run();
                }
            }
        }
    }

    log.info("Dumping");
    for (String table : fpaths.keySet()) {
        int offset = 0;
        if (template.checkTable(table, table.split("\\.")[0])) {
            if (template.getCount(table) > 0) {
                log.info("Dumping for " + table);
                // get header
                String select = "SELECT * FROM " + table;
                String fpath = null;
                ArrayList<String> jsons;
                String condition;
                int w = 0;
                int start = offset;
                int chunksize = (int) Math.ceil(pullsize / qnum);

                // get fpath
                for (String ofp : fpaths.get(table).keySet()) {
                    start = fpaths.get(table).get(ofp);
                    fpath = ofp;
                }

                // perform write
                if (headers != null && fpath != null) {
                    List<String> headersList = headers.get(table);

                    String output = null;
                    boolean existed = true;

                    if (addFileDate) {
                        fpath = fpath
                                + Calendar.getInstance().getTime().toString().trim().replaceAll(":|\\s", "")
                                + ".txt";
                    }

                    // check to see if file should be created
                    if (!new File(fpath).exists()) {

                        try {
                            new File(fpath).createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        existed = false;
                    }

                    // check to see if file must be recreated
                    if (!append) {

                        File f = new File(fpath);
                        f.delete();
                        try {
                            f.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (headersList != null && (append == false || existed == false)) {
                        for (String header : headersList) {
                            output = (output == null) ? StringEscapeUtils.unescapeXml(header)
                                    : output + delimeter + StringEscapeUtils.unescapeXml(header);
                        }
                    }

                    do {

                        // get records
                        jsons = new ArrayList<String>(pullsize);
                        log.info("Looking for Pages.");
                        for (int conn = 0; conn < qnum; conn++) {
                            // create condition
                            condition = " WHERE " + pullid + " >= " + (start + (conn * chunksize)) + " AND "
                                    + pullid + " < " + Integer.toString(start + (chunksize * (conn + 1)));

                            if (extracondition != null) {
                                condition += " " + extracondition.trim();
                            }

                            // get queries
                            qcollect.add(new SplitQuery(template, (select + condition)));
                            log.info("Fetching " + select + condition);
                        }

                        start += (chunksize * qnum);

                        qfutures = fjp.invokeAll(qcollect);

                        w = 0;
                        while (fjp.getActiveThreadCount() > 0 && fjp.isQuiescent() == false) {
                            w++;
                        }
                        log.info("Waited for " + w + " cycles");

                        for (Future<ArrayList<String>> f : qfutures) {
                            try {

                                ArrayList<String> test = f.get();
                                if (test != null) {
                                    if (test.size() > 0) {
                                        jsons.addAll(test);
                                    }
                                }

                                if (f.isDone() == false) {
                                    f.cancel(true);
                                }

                                f = null;
                            } catch (Exception e) {
                                log.warn("Encoding Error!");
                                e.printStackTrace();
                            }
                        }
                        qcollect = new HashSet<Callable<ArrayList<String>>>(4);
                        qfutures = null;
                        log.info("Finished Getting Pages");

                        // post records to the file
                        try (FileWriter fw = new FileWriter(new File(fpath), true)) {
                            // get and write headers

                            if (jsons.size() > 0) {
                                fw.write(output + "\n");
                                // write data
                                for (String json : jsons) {
                                    output = null;
                                    JsonObject jo = JsonObject.readFrom(json);
                                    if (jo.size() >= headersList.size()) {// allows
                                        // trimming
                                        // of
                                        // table
                                        // to
                                        // key
                                        // aspects
                                        output = null;

                                        for (String key : headers.get(table)) {

                                            if (jo.get(key.toLowerCase()) != null) {
                                                String data = StringEscapeUtils
                                                        .unescapeXml(jo.get(key.toLowerCase()).asString());

                                                if (replacementPattern != null) {
                                                    data = data.replaceAll(replacementPattern, "");
                                                    data = data.replace(delimeter, delimreplace);
                                                }

                                                output = (output == null)
                                                        ? data.replaceAll("[^\u0020-\u0070 ]+", "")
                                                        : output + delimeter
                                                                + data.replaceAll("[^\u0020-\u0070 ]+", "");
                                            } else {
                                                output += delimeter;
                                            }
                                        }

                                        if (output != null && output.trim().length() > headersList.size()) {
                                            fw.write(output + "\n");
                                        }
                                    } else {
                                        if (jsons.size() == 0) {
                                            Log.info(
                                                    "Number of Headers and Keys from Json Array and Headers List Impossible to Match");
                                            try {
                                                throw new MismatchException(
                                                        "Number of Headers: " + headersList.size()
                                                                + " && Number of Keys: " + jo.size());
                                            } catch (MismatchException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }

                                    output = null;
                                }
                            } else {
                                log.info("EOF FOUND! No New Records in This Iteration....Stopping.");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } while (jsons.size() > 0);

                } else {
                    try {
                        throw new NullPointerException(
                                "No Headers Input to Class. Please Create the Requisite Map.");
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                }
                dumped += 1;
            } else {
                try {
                    throw new NoDataException("No Data Found in Table " + table);
                } catch (NoDataException e) {
                    e.printStackTrace();
                }
            }
        } else {
            log.info("Missing Table " + table);
            try {
                throw new NullPointerException("Table " + table + " Does Not Exist!!!");
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    } // end LOOP

    if (!fjp.isShutdown()) {
        fjp.shutdownNow();
    }

    if (dumped == 0) {
        log.error("No Data Found in Any Table");
        System.exit(-1);
    }
}

From source file:com.hygenics.parser.JDumpWithReference.java

private void toFile() {
    List<Future<ArrayList<String>>> qfutures;
    Set<Callable<ArrayList<String>>> qcollect = new HashSet<Callable<ArrayList<String>>>(4);

    ForkJoinPool fjp = new ForkJoinPool((int) Math.ceil(Runtime.getRuntime().availableProcessors() * procnum));
    int dumped = 0;

    if (archive) {
        log.info("Cleaning");
        for (String k : fpaths.keySet()) {
            String fpath = "";

            for (String ofp : fpaths.get(k).keySet()) {
                fpath = ofp;//  w  w  w .j  a  va 2 s.c om
            }

            if (fpath.length() > 0) {
                String[] barr = fpath.split("\\/");
                String basefile = "";
                Archiver zip = new Archiver();
                for (int i = 0; i > barr.length - 1; i++) {
                    basefile += (i == 0) ? barr[i] : "/" + barr[i];
                }
                if (basefile.trim().length() > 0) {
                    zip.setBasedirectory(basefile);
                    zip.setZipDirectory(basefile + "archive.zip");
                    zip.setAvoidanceString(".zip|archive");
                    zip.setDelFiles(true);
                    zip.run();
                }
            }
        }
    }

    log.info("Dumping");
    for (String table : fpaths.keySet()) {
        int offset = 0;
        if (template.checkTable(this.baseschema + "." + table, this.baseschema)) {
            if (template.getCount(this.baseschema + "." + table) > 0) {
                log.info("Dumping for " + table);
                // get header
                String select = "SELECT * FROM " + this.baseschema + "." + table;
                String fpath = null;
                ArrayList<String> jsons;
                String condition;
                int w = 0;
                int start = offset;
                int chunksize = (int) Math.ceil(pullsize / qnum);

                // get fpath
                for (String ofp : fpaths.get(table).keySet()) {
                    start = fpaths.get(table).get(ofp);
                    fpath = ofp;
                }

                // perform write
                if (headers != null && fpath != null) {
                    List<String> headersList = headers.get(table);

                    String output = null;
                    boolean existed = true;

                    if (addFileDate) {
                        fpath = fpath
                                + Calendar.getInstance().getTime().toString().trim().replaceAll(":|\\s", "")
                                + ".txt";
                    }

                    // check to see if file should be created
                    if (!new File(fpath).exists()) {

                        try {
                            new File(this.baseFilePath + fpath).createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        existed = false;
                    }

                    // check to see if file must be recreated
                    if (!append) {

                        File f = new File(this.baseFilePath + fpath);
                        f.delete();
                        try {
                            f.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (headersList != null && (append == false || existed == false)) {
                        for (String header : headersList) {
                            output = (output == null) ? StringEscapeUtils.unescapeXml(header)
                                    : output + delimeter + StringEscapeUtils.unescapeXml(header);
                        }
                    }

                    do {

                        // get records
                        jsons = new ArrayList<String>(pullsize);
                        log.info("Looking for Pages.");
                        for (int conn = 0; conn < qnum; conn++) {
                            // create condition
                            condition = " WHERE " + pullid + " >= " + (start + (conn * chunksize)) + " AND "
                                    + pullid + " < " + Integer.toString(start + (chunksize * (conn + 1)));

                            if (extracondition != null) {
                                condition += " " + extracondition.trim();
                            }

                            // get queries
                            qcollect.add(new SplitQuery(template, (select + condition)));
                            log.info("Fetching " + select + condition);
                        }

                        start += (chunksize * qnum);

                        qfutures = fjp.invokeAll(qcollect);

                        w = 0;
                        while (fjp.getActiveThreadCount() > 0 && fjp.isQuiescent() == false) {
                            w++;
                        }
                        log.info("Waited for " + w + " cycles");

                        for (Future<ArrayList<String>> f : qfutures) {
                            try {

                                ArrayList<String> test = f.get();
                                if (test != null) {
                                    if (test.size() > 0) {
                                        jsons.addAll(test);
                                    }
                                }

                                if (f.isDone() == false) {
                                    f.cancel(true);
                                }

                                f = null;
                            } catch (Exception e) {
                                log.warn("Encoding Error!");
                                e.printStackTrace();
                            }
                        }
                        qcollect = new HashSet<Callable<ArrayList<String>>>(4);
                        qfutures = null;
                        log.info("Finished Getting Pages");

                        // post records to the file
                        try (FileWriter fw = new FileWriter(new File(this.baseFilePath + fpath), true)) {
                            // get and write headers

                            if (jsons.size() > 0) {
                                fw.write(output + "\n");
                                // write data
                                for (String json : jsons) {
                                    output = null;
                                    JsonObject jo = JsonObject.readFrom(json);
                                    if (jo.size() >= headersList.size()) {// allows
                                        // trimming
                                        // of
                                        // table
                                        // to
                                        // key
                                        // aspects
                                        output = null;

                                        for (String key : headers.get(table)) {

                                            if (jo.get(key.toLowerCase()) != null) {
                                                String data = StringEscapeUtils
                                                        .unescapeXml(jo.get(key.toLowerCase()).asString());

                                                if (replacementPattern != null) {
                                                    data = data.replaceAll(replacementPattern, "");
                                                    data = data.replace(delimeter, delimreplace);
                                                }

                                                output = (output == null)
                                                        ? data.replaceAll("[^\u0020-\u007E ]+", "")
                                                        : output + delimeter
                                                                + data.replaceAll("[^\u0020-\u007E ]+", "");
                                            } else {
                                                output += delimeter;
                                            }
                                        }

                                        if (output != null && output.trim().length() > headersList.size()) {
                                            fw.write(output + "\n");
                                        }
                                    } else {
                                        if (jsons.size() == 0) {
                                            Log.info(
                                                    "Number of Headers and Keys from Json Array and Headers List Impossible to Match");
                                            try {
                                                throw new MismatchException(
                                                        "Number of Headers: " + headersList.size()
                                                                + " && Number of Keys: " + jo.size());
                                            } catch (MismatchException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }

                                    output = null;
                                }
                            } else {
                                log.info("EOF FOUND! No New Records in This Iteration....Stopping.");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } while (jsons.size() > 0);

                } else {
                    try {
                        throw new NullPointerException(
                                "No Headers Input to Class. Please Create the Requisite Map.");
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                }
                dumped += 1;
            } else {
                try {
                    throw new NoDataException("No Data in Table " + table);
                } catch (NoDataException e) {
                    e.printStackTrace();
                }
            }
        } else {
            log.info("Missing Table " + table);
            try {
                throw new NullPointerException("Table " + table + " Does Not Exist!!!");
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    } // end LOOP

    if (!fjp.isShutdown()) {
        fjp.shutdownNow();
    }

    if (dumped == 0) {
        log.error("No Data found in Any Tables");
        System.exit(-1);
    }
}