Example usage for org.apache.commons.lang3.time StopWatch toString

List of usage examples for org.apache.commons.lang3.time StopWatch toString

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time StopWatch toString.

Prototype

@Override
public String toString() 

Source Link

Document

Gets a summary of the time that the stopwatch recorded as a string.

Usage

From source file:com.norconex.commons.lang.url.URLStreamer.java

/**
 * Streams URL content to a String.//from  w ww.j a v a2s.  c o  m
 * @param url the URL to stream
 * @param creds credentials for a protected URL
 * @param proxy proxy to use to stream the URL
 * @return a URL content as a String
 */
public static String streamToString(String url, Credentials creds, HttpHost proxy) {
    StopWatch watch = null;
    if (LOG.isDebugEnabled()) {
        watch = new StopWatch();
        watch.start();
        LOG.debug("Streaming URL:" + url);
    }
    String out;
    try {
        out = IOUtils.toString(stream(url, creds, proxy));
    } catch (IOException e) {
        throw new URLException("Could not stream URL to string: " + url, e);
    }
    if (LOG.isDebugEnabled()) {
        watch.stop();
        LOG.debug("Streaming elapsed time: " + watch.toString());
    }
    return out;
}

From source file:com.norconex.commons.lang.url.URLStreamer.java

/**
 * Streams URL content to a String./*w  w  w  .  j av  a 2  s.co  m*/
 * @param url the URL to stream
 * @param creds credentials for a protected URL
 * @param proxy proxy to use to stream the URL
 * @param proxyCreds credentials to access the proxy
 * @return a URL content as a String
 */
public static String streamToString(String url, Credentials creds, HttpHost proxy, Credentials proxyCreds) {
    StopWatch watch = null;
    if (LOG.isDebugEnabled()) {
        watch = new StopWatch();
        watch.start();
        LOG.debug("Streaming URL:" + url);
    }
    String out;
    try {
        out = IOUtils.toString(stream(url, creds, proxy, proxyCreds));
    } catch (IOException e) {
        throw new URLException("Could not stream URL to string: " + url, e);
    }
    if (LOG.isDebugEnabled()) {
        watch.stop();
        LOG.debug("Streaming elapsed time: " + watch.toString());
    }
    return out;
}

From source file:com.sludev.mssqlapplylog.MSSQLHelper.java

/**
 * Restore a Backup Log using a backup file on the file-system.
 * //from   w ww . ja  v  a  2s .  c  om
 * @param logPath
 * @param sqlProcessUser Optionally, give this user file-system permissions.  So SQL Server can RESTORE.
 * @param sqlDb The name of the database to restore.
 * @param conn  Open connection
 * @throws SQLException 
 */
public static void restoreLog(final Path logPath, final String sqlProcessUser, final String sqlDb,
        final Connection conn) throws SQLException {
    LOGGER.info(String.format("\nStarting Log restore of '%s'...", logPath));

    StopWatch sw = new StopWatch();

    sw.start();

    if (StringUtils.isNoneBlank(sqlProcessUser)) {
        try {
            FSHelper.addRestorePermissions(sqlProcessUser, logPath);
        } catch (IOException ex) {
            LOGGER.debug(String.format("Error adding read permission for user '%s' to '%s'", sqlProcessUser,
                    logPath), ex);
        }
    }

    String strDevice = logPath.toAbsolutePath().toString();

    String query = String.format("RESTORE LOG %s FROM DISK='%s' WITH NORECOVERY", sqlDb, strDevice);

    Statement stmt = null;

    stmt = conn.createStatement();

    try {
        boolean sqlRes = stmt.execute(query);
    } catch (SQLException ex) {
        LOGGER.error(String.format("Error executing...\n'%s'", query), ex);

        throw ex;
    }

    sw.stop();

    LOGGER.debug(String.format("Query...\n'%s'\nTook %s", query, sw.toString()));
}

From source file:broadwick.data.DataReader.java

/**
 * Read the data section from the configuration file. If a database section is found then it is used and any files
 * specified are ignored.//from   w  w  w  . j a  va 2s. com
 */
public final void readDataSection() {
    final StopWatch sw = new StopWatch();
    final DataFiles files = data.getDatafiles();
    if (files != null) {
        sw.start();
        readDataFiles(files);
        sw.stop();
        log.info("Processed input data in {} ms", sw.toString());
        log.info("Data stored internally in {}.", dbName);
    }
}

From source file:fr.mycellar.infrastructure.shared.search.MassIndexerService.java

@PostConstruct
public void createIndex() {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from ww w .  j a v a2  s .c  o  m*/
    try {
        getFullTextEntityManager(entityManager) //
                .createIndexer() //
                .batchSizeToLoadObjects(batchSizeToLoadObjects) //
                .threadsToLoadObjects(threadsToLoadObjects) //
                .startAndWait();
    } catch (InterruptedException e) {
        logger.warn("Interrupted while indexing.", e);
        Thread.currentThread().interrupt();
    } finally {
        stopWatch.stop();
        logger.info("Indexation done in {}.", stopWatch.toString());
    }
}

From source file:com.quarterfull.newsAndroid.ListView.SubscriptionExpandableListAdapter.java

public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ReloadAdapter() {
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false);

    ArrayList<AbstractItem> mCategoriesArrayListAsync = new ArrayList<>();
    mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.allUnreadFeeds), null,
            ALL_UNREAD_ITEMS.getValue()));
    mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), null,
            ALL_STARRED_ITEMS.getValue()));

    StopWatch sw = new StopWatch();
    sw.start();/*from   ww  w.j a v a2  s .c  om*/

    List<Folder> folderList;
    //if(showOnlyUnread) {
    //    folderList = dbConn.getListOfFoldersWithUnreadItems();
    //} else {
    folderList = dbConn.getListOfFolders();
    //}

    sw.stop();
    Log.v(TAG, "Time needed (fetch folder list): " + sw.toString());

    for (Folder folder : folderList) {
        mCategoriesArrayListAsync.add(new FolderSubscribtionItem(folder.getLabel(), null, folder.getId()));
    }

    for (Feed feed : dbConn.getListOfFeedsWithoutFolders(showOnlyUnread)) {
        mCategoriesArrayListAsync.add(new ConcreteFeedItem(feed.getFeedTitle(),
                (long) ITEMS_WITHOUT_FOLDER.getValue(), feed.getId(), feed.getFaviconUrl(), feed.getId()));
    }

    SparseArray<ArrayList<ConcreteFeedItem>> mItemsArrayListAsync = new SparseArray<>();

    for (int groupPosition = 0; groupPosition < mCategoriesArrayListAsync.size(); groupPosition++) {
        //int parent_id = (int)getGroupId(groupPosition);
        int parent_id = (int) mCategoriesArrayListAsync.get(groupPosition).id_database;
        mItemsArrayListAsync.append(parent_id, new ArrayList<ConcreteFeedItem>());

        List<Feed> feedItemList = null;

        if (parent_id == ALL_UNREAD_ITEMS.getValue()) {
            feedItemList = dbConn.getAllFeedsWithUnreadRssItems();
        } else if (parent_id == ALL_STARRED_ITEMS.getValue()) {
            feedItemList = dbConn.getAllFeedsWithStarredRssItems();
        } else {
            for (Folder folder : folderList) {//Find the current selected folder
                if (folder.getId() == parent_id) {//Current item
                    feedItemList = dbConn.getAllFeedsWithUnreadRssItemsForFolder(folder.getId());
                    break;
                }
            }
        }

        if (feedItemList != null) {
            for (Feed feed : feedItemList) {
                ConcreteFeedItem newItem = new ConcreteFeedItem(feed.getFeedTitle(), (long) parent_id,
                        feed.getId(), feed.getFaviconUrl(), feed.getId());
                mItemsArrayListAsync.get(parent_id).add(newItem);
            }
        }
    }

    return new Tuple<>(mCategoriesArrayListAsync, mItemsArrayListAsync);
}

From source file:gov.va.vinci.leo.listener.BaseDatabaseListener.java

/**
 * This is called when a batch is ready to be send to the database. It connects to the database (if needed),
 * created the prepared statement batch, and executes it.
 *
 * @throws ClassNotFoundException  thrown if the JDBC driver cannot be found.
 * @throws IllegalAccessException    thrown if any of the reflection calls throw an exception.
 * @throws SQLException       thrown if  there is an SQL Exception accessing the database.
 * @throws InstantiationException  thrown if the JDBC driver cannot be instantiated.
 *//*w ww.  j  ava2s.c  o  m*/
protected synchronized void processBatch()
        throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    if (this.validateConnectionEachBatch) {
        validateConnection();
    }
    LOG.info("Dumping DB Batch of (" + batchSize + ") after processing " + numReceived + " records");
    StopWatch clock = new StopWatch();
    clock.start();
    for (Object[] row : batch) {
        int i = 1;
        for (Object o : row) {
            ps.setObject(i, o);
            i++;
        }
        ps.addBatch();
    }
    try {
        ps.executeBatch();
    } catch (SQLException e) {
        LOG.error("Error executing the batch!", e);
        LOG.debug("BATCH DATA:\n\n" + batch.toString()); //Could be a LOT of output so just print if logging is set to debug
        throw e;
    } finally {
        //Always clear the batch to avoid re-executing SQL with errors
        batch.clear();
    }
    clock.stop();
    LOG.info("Finished dumping DB batch: " + clock.toString());
}

From source file:com.norconex.collector.core.crawler.AbstractCrawler.java

protected boolean processNextReference(final ICrawlDataStore crawlDataStore,
        final JobStatusUpdater statusUpdater, final boolean delete) {
    if (!delete && isMaxDocuments()) {
        LOG.info(getId() + ": Maximum documents reached: " + getCrawlerConfig().getMaxDocuments());
        return false;
    }/*  ww w  .jav a  2s . c  om*/
    BaseCrawlData queuedCrawlData = (BaseCrawlData) crawlDataStore.nextQueued();

    if (LOG.isTraceEnabled()) {
        LOG.trace(getId() + " Processing next reference from Queue: " + queuedCrawlData);
    }
    if (queuedCrawlData != null) {
        StopWatch watch = null;
        if (LOG.isDebugEnabled()) {
            watch = new StopWatch();
            watch.start();
        }
        processNextQueuedCrawlData(queuedCrawlData, crawlDataStore, delete);
        setProgress(statusUpdater, crawlDataStore);
        if (LOG.isDebugEnabled()) {
            watch.stop();
            LOG.debug(getId() + ": " + watch.toString() + " to process: " + queuedCrawlData.getReference());
        }
    } else {
        int activeCount = crawlDataStore.getActiveCount();
        boolean queueEmpty = crawlDataStore.isQueueEmpty();
        if (LOG.isTraceEnabled()) {
            LOG.trace(getId() + " Number of references currently being processed: " + activeCount);
            LOG.trace(getId() + " Is reference queue empty? " + queueEmpty);
        }
        if (activeCount == 0 && queueEmpty) {
            return false;
        }
        Sleeper.sleepMillis(MINIMUM_DELAY);
    }
    return true;
}

From source file:com.sludev.mssqlapplylog.MSSQLApplyLog.java

@Override
public Integer call() throws Exception {
    Integer res = 0;//from  ww w  .  j  a va2s . c  om

    String backupDirStr = config.getBackupDirStr();
    String fullBackupPathStr = config.getFullBackupPathStr();
    String fullBackupDatePatternStr = config.getFullBackupDatePatternStr();
    String laterThanStr = config.getLaterThanStr();
    String fullBackupPatternStr = config.getFullBackupPatternStr();
    String logBackupPatternStr = config.getLogBackupPatternStr();
    String logBackupDatePatternStr = config.getLogBackupDatePatternStr();

    String sqlHost = config.getSqlHost();
    String sqlDb = config.getSqlDb();
    String sqlUser = config.getSqlUser();
    String sqlPass = config.getSqlPass();
    String sqlURL = config.getSqlUrl();
    String sqlProcessUser = config.getSqlProcessUser();

    boolean useLogFileLastMode = BooleanUtils.isTrue(config.getUseLogFileLastMode());
    boolean doFullRestore = BooleanUtils.isTrue(config.getDoFullRestore());
    boolean monitorLogBackupDir = BooleanUtils.isTrue(config.getMonitorLogBackupDir());

    Path backupsDir = null;
    Instant laterThan = null;

    Path fullBackupPath = null;

    // Validate the Log Backup Directory
    if (StringUtils.isBlank(backupDirStr)) {
        LOGGER.error("Invalid blank/empty backup directory");

        return 1;
    }

    try {
        backupsDir = Paths.get(backupDirStr);
    } catch (Exception ex) {
        LOGGER.error(String.format("Error parsing Backup Directory '%s'", backupDirStr), ex);

        return 1;
    }

    if (Files.notExists(backupsDir)) {
        LOGGER.error(String.format("Invalid non-existant backup directory '%s'", backupsDir));

        return 1;
    }

    if (StringUtils.isBlank(logBackupPatternStr)) {
        LOGGER.warn(String.format(
                "\"Log Backup Pattern\" cannot be empty.  Defaulting to " + "%s regex in backup directory",
                DEFAULT_LOG_FILE_PATTERN_STR));

        logBackupPatternStr = DEFAULT_LOG_FILE_PATTERN_STR;
    }

    if (StringUtils.isNoneBlank(fullBackupPathStr)) {
        fullBackupPath = Paths.get(fullBackupPathStr);
        if (Files.notExists(fullBackupPath) || Files.isRegularFile(fullBackupPath) == false) {
            LOGGER.error(String.format("Invalid Full Backup file '%s'", backupDirStr));

            return 1;
        }
    }

    if (StringUtils.isNoneBlank(fullBackupDatePatternStr) && StringUtils.isBlank(laterThanStr)
            && fullBackupPath != null) {
        // Retrieve the "Later Than" date from the full backup file name
        laterThan = FSHelper.getTimestampFromFilename(fullBackupPatternStr, fullBackupDatePatternStr, 1,
                fullBackupPath);
        if (laterThan == null) {
            LOGGER.error("'Later Than' cannot be null");

            return 1;
        }
    } else {
        // Use the "Later Than" timestamp from the command line or properties
        // file.

        try {
            laterThan = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(laterThanStr));
        } catch (Exception ex) {
            LOGGER.error(String.format("Error parsing 'Later Than' time '%s'", laterThanStr), ex);
        }
    }

    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException ex) {
        LOGGER.error("Error loading SQL Server driver [ net.sourceforge.jtds.jdbc.Driver ]", ex);

        return 1;
    }

    if (StringUtils.isBlank(sqlURL)) {
        // Build the SQL URL
        sqlURL = String.format("jdbc:jtds:sqlserver://%s;DatabaseName=master", sqlHost);
    }

    Properties props = new Properties();

    props.setProperty("user", sqlUser);
    props.setProperty("password", sqlPass);

    try (Connection conn = MSSQLHelper.getConn(sqlURL, props)) {
        if (conn == null) {
            LOGGER.error("Connection to MSSQL failed.");

            return 1;
        }

        if (doFullRestore) {
            LOGGER.info(String.format("\nStarting full restore of '%s'...", fullBackupPathStr));

            StopWatch sw = new StopWatch();

            sw.start();

            String strDevice = fullBackupPathStr;

            String query = String.format("RESTORE DATABASE %s FROM DISK='%s' WITH NORECOVERY, REPLACE", sqlDb,
                    strDevice);

            Statement stmt = null;

            try {
                stmt = conn.createStatement();
            } catch (SQLException ex) {
                LOGGER.debug("Error creating statement", ex);

                return 1;
            }

            try {
                boolean sqlRes = stmt.execute(query);
            } catch (SQLException ex) {
                LOGGER.error(String.format("Error executing...\n'%s'", query), ex);

                return 1;
            }

            sw.stop();

            LOGGER.debug(String.format("Query...\n'%s'\nTook %s", query, sw.toString()));
        }
    } catch (SQLException ex) {
        LOGGER.error("SQL Exception restoring the full backup", ex);
    }

    // Filter the log files.

    // Loop multiple times to catch new logs that have been transferred
    // while we process.
    List<Path> files = null;
    do {
        try {
            files = FSHelper.listLogFiles(backupsDir, laterThan, useLogFileLastMode, logBackupPatternStr,
                    logBackupDatePatternStr, files);
        } catch (IOException ex) {
            LOGGER.error("Log Backup file filter/sort failed", ex);

            return 1;
        }

        if (files == null || files.isEmpty()) {
            LOGGER.debug("No Log Backup files found this iteration.");

            continue;
        }

        StringBuilder msg = new StringBuilder();

        for (Path file : files) {
            msg.append(String.format("file : '%s'\n", file));
        }

        LOGGER.debug(msg.toString());

        // Restore all log files
        try (Connection conn = MSSQLHelper.getConn(sqlURL, props)) {
            for (Path p : files) {
                try {
                    MSSQLHelper.restoreLog(p, sqlProcessUser, sqlDb, conn);
                } catch (SQLException ex) {
                    LOGGER.error(String.format("SQL Exception restoring the log backup '%s'", p), ex);
                }
            }
        } catch (SQLException ex) {
            LOGGER.error("SQL Exception restoring the log backup", ex);
        }
    } while (files != null && files.isEmpty() == false);

    if (monitorLogBackupDir) {
        // Watch for new log files
        List<Path> paths = new ArrayList();
        paths.add(backupsDir);

        final Watch watch;
        final String currSQLDb = sqlDb;
        final String currSQLProcUser = sqlProcessUser;
        final String currSQLURL = sqlURL;
        final String currLogBackupPatternStr = logBackupPatternStr;

        try {
            watch = Watch.from(paths);
            watch.processEvents((WatchEvent<Path> event, Path path) -> {
                int watchRes = 0;

                if (event.kind() != StandardWatchEventKinds.ENTRY_CREATE) {
                    return watchRes;
                }

                Pattern fileMatcher = Pattern.compile(currLogBackupPatternStr);

                if (fileMatcher.matcher(path.getFileName().toString()).matches()) {
                    try (Connection conn = MSSQLHelper.getConn(currSQLURL, props)) {
                        MSSQLHelper.restoreLog(path, currSQLProcUser, currSQLDb, conn);
                    } catch (SQLException ex) {
                        // There's really no recovering from a failed log backup

                        LOGGER.error("SQL Exception restoring the log backup", ex);

                        System.exit(1);
                    }
                }

                return watchRes;
            });
        } catch (IOException | FileCheckException ex) {
            LOGGER.error(String.format("Error watching backup directory...\n'%s'", backupsDir), ex);

            return 1;
        } catch (InterruptedException ex) {
            LOGGER.info(String.format("Interrupted watching backup directory...\n'%s'", backupsDir), ex);
        }
    }

    return res;
}

From source file:broadwick.data.Lookup.java

/**
 * Get all the tests that have been read from the file(s) specified in the configuration file.
 * @return a collection of movement events that have been recorded.
 *//*from  w  w  w  .ja  va  2  s.co m*/
public Collection<Test> getTests() {
    final Collection<Test> tests = new ArrayList<>();
    final StopWatch sw = new StopWatch();
    sw.start();

    final Result<Record> records = jooq.select().from(TestsFileReader.getTABLE_NAME()).fetch();
    for (Record r : records) {
        final Test test = createTest(r);
        if (test != null) {
            tests.add(test);
        }
    }

    sw.stop();
    log.debug("Found {} tests in {}.", tests.size(), sw.toString());
    return tests;
}