Search in sources :

Example 66 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class AsyncJmsInOutTempDestIT method testAsynchronous.

@Test
public void testAsynchronous() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(100);
    mock.expectsNoDuplicates(body());
    StopWatch watch = new StopWatch();
    for (int i = 0; i < 100; i++) {
        template.sendBody("seda:start", "" + i);
    }
    // just in case we run on slow boxes
    assertMockEndpointsSatisfied(20, TimeUnit.SECONDS);
    log.info("Took " + watch.stop() + " ms. to process 100 messages request/reply over JMS");
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StopWatch(org.apache.camel.util.StopWatch) Test(org.junit.Test)

Example 67 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class SyncJmsInOutIT method testSynchronous.

@Test
public void testSynchronous() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(100);
    mock.expectsNoDuplicates(body());
    StopWatch watch = new StopWatch();
    for (int i = 0; i < 100; i++) {
        template.sendBody("seda:start", "" + i);
    }
    // just in case we run on slow boxes
    assertMockEndpointsSatisfied(20, TimeUnit.SECONDS);
    log.info("Took " + watch.stop() + " ms. to process 100 messages request/reply over JMS");
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StopWatch(org.apache.camel.util.StopWatch) Test(org.junit.Test)

Example 68 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class FtpChangedExclusiveReadLockStrategy method acquireExclusiveReadLock.

public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception {
    boolean exclusive = false;
    LOG.trace("Waiting for exclusive read lock to file: " + file);
    long lastModified = Long.MIN_VALUE;
    long length = Long.MIN_VALUE;
    StopWatch watch = new StopWatch();
    long startTime = new Date().getTime();
    while (!exclusive) {
        // timeout check
        if (timeout > 0) {
            long delta = watch.taken();
            if (delta > timeout) {
                CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
                // we could not get the lock within the timeout period, so return false
                return false;
            }
        }
        long newLastModified = 0;
        long newLength = 0;
        List<FTPFile> files;
        if (fastExistsCheck) {
            // use the absolute file path to only pickup the file we want to check, this avoids expensive
            // list operations if we have a lot of files in the directory
            String path = file.getAbsoluteFilePath();
            if (path.equals("/") || path.equals("\\")) {
                // special for root (= home) directory
                LOG.trace("Using fast exists to update file information in home directory");
                files = operations.listFiles();
            } else {
                LOG.trace("Using fast exists to update file information for {}", path);
                files = operations.listFiles(path);
            }
        } else {
            // fast option not enabled, so list the directory and filter the file name
            String path = file.getParent();
            if (path.equals("/") || path.equals("\\")) {
                // special for root (= home) directory
                LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
                files = operations.listFiles();
            } else {
                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
                files = operations.listFiles(path);
            }
        }
        LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
        for (FTPFile f : files) {
            boolean match;
            if (fastExistsCheck) {
                // uses the absolute file path as well
                match = f.getName().equals(file.getAbsoluteFilePath()) || f.getName().equals(file.getFileNameOnly());
            } else {
                match = f.getName().equals(file.getFileNameOnly());
            }
            if (match) {
                newLength = f.getSize();
                if (f.getTimestamp() != null) {
                    newLastModified = f.getTimestamp().getTimeInMillis();
                }
            }
        }
        LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified);
        LOG.trace("Previous length: " + length + ", new length: " + newLength);
        long newOlderThan = startTime + watch.taken() - minAge;
        LOG.trace("New older than threshold: {}", newOlderThan);
        if (newLength >= minLength && ((minAge == 0 && newLastModified == lastModified && newLength == length) || (minAge != 0 && newLastModified < newOlderThan))) {
            LOG.trace("Read lock acquired.");
            exclusive = true;
        } else {
            // set new base file change information
            lastModified = newLastModified;
            length = newLength;
            boolean interrupted = sleep();
            if (interrupted) {
                // we were interrupted while sleeping, we are likely being shutdown so return false
                return false;
            }
        }
    }
    return exclusive;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) Date(java.util.Date) StopWatch(org.apache.camel.util.StopWatch)

Example 69 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class SftpChangedExclusiveReadLockStrategy method acquireExclusiveReadLock.

public boolean acquireExclusiveReadLock(GenericFileOperations<ChannelSftp.LsEntry> operations, GenericFile<ChannelSftp.LsEntry> file, Exchange exchange) throws Exception {
    boolean exclusive = false;
    LOG.trace("Waiting for exclusive read lock to file: " + file);
    long lastModified = Long.MIN_VALUE;
    long length = Long.MIN_VALUE;
    StopWatch watch = new StopWatch();
    long startTime = new Date().getTime();
    while (!exclusive) {
        // timeout check
        if (timeout > 0) {
            long delta = watch.taken();
            if (delta > timeout) {
                CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
                // we could not get the lock within the timeout period, so return false
                return false;
            }
        }
        long newLastModified = 0;
        long newLength = 0;
        List<ChannelSftp.LsEntry> files;
        if (fastExistsCheck) {
            // use the absolute file path to only pickup the file we want to check, this avoids expensive
            // list operations if we have a lot of files in the directory
            String path = file.getAbsoluteFilePath();
            if (path.equals("/") || path.equals("\\")) {
                // special for root (= home) directory
                LOG.trace("Using fast exists to update file information in home directory");
                files = operations.listFiles();
            } else {
                LOG.trace("Using fast exists to update file information for {}", path);
                files = operations.listFiles(path);
            }
        } else {
            String path = file.getParent();
            if (path.equals("/") || path.equals("\\")) {
                // special for root (= home) directory
                LOG.trace("Using full directory listing in home directory to update file information. Consider enabling fastExistsCheck option.");
                files = operations.listFiles();
            } else {
                LOG.trace("Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", path);
                files = operations.listFiles(path);
            }
        }
        LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size());
        for (ChannelSftp.LsEntry f : files) {
            boolean match;
            if (fastExistsCheck) {
                // uses the absolute file path as well
                match = f.getFilename().equals(file.getAbsoluteFilePath()) || f.getFilename().equals(file.getFileNameOnly());
            } else {
                match = f.getFilename().equals(file.getFileNameOnly());
            }
            if (match) {
                newLastModified = f.getAttrs().getMTime() * 1000L;
                newLength = f.getAttrs().getSize();
            }
        }
        LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified);
        LOG.trace("Previous length: " + length + ", new length: " + newLength);
        long newOlderThan = startTime + watch.taken() - minAge;
        LOG.trace("New older than threshold: {}", newOlderThan);
        if (newLength >= minLength && ((minAge == 0 && newLastModified == lastModified && newLength == length) || (minAge != 0 && newLastModified < newOlderThan))) {
            LOG.trace("Read lock acquired.");
            exclusive = true;
        } else {
            // set new base file change information
            lastModified = newLastModified;
            length = newLength;
            boolean interrupted = sleep();
            if (interrupted) {
                // we were interrupted while sleeping, we are likely being shutdown so return false
                return false;
            }
        }
    }
    return exclusive;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Date(java.util.Date) StopWatch(org.apache.camel.util.StopWatch)

Example 70 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class AggregateProcessor method restoreTimeoutMapFromAggregationRepository.

/**
     * Restores the timeout map with timeout values from the aggregation repository.
     * <p/>
     * This is needed in case the aggregator has been stopped and started again (for example a server restart).
     * Then the existing exchanges from the {@link AggregationRepository} must have their timeout conditions restored.
     */
protected void restoreTimeoutMapFromAggregationRepository() throws Exception {
    // grab the timeout value for each partly aggregated exchange
    Set<String> keys = aggregationRepository.getKeys();
    if (keys == null || keys.isEmpty()) {
        return;
    }
    StopWatch watch = new StopWatch();
    LOG.trace("Starting restoring CompletionTimeout for {} existing exchanges from the aggregation repository...", keys.size());
    for (String key : keys) {
        Exchange exchange = aggregationRepository.get(camelContext, key);
        // grab the timeout value
        long timeout = exchange.hasProperties() ? exchange.getProperty(Exchange.AGGREGATED_TIMEOUT, 0, long.class) : 0;
        if (timeout > 0) {
            LOG.trace("Restoring CompletionTimeout for exchangeId: {} with timeout: {} millis.", exchange.getExchangeId(), timeout);
            addExchangeToTimeoutMap(key, exchange, timeout);
        }
    }
    // log duration of this task so end user can see how long it takes to pre-check this upon starting
    LOG.info("Restored {} CompletionTimeout conditions in the AggregationTimeoutChecker in {}", timeoutMap.size(), TimeUtils.printDuration(watch.stop()));
}
Also used : Exchange(org.apache.camel.Exchange) StopWatch(org.apache.camel.util.StopWatch)

Aggregations

StopWatch (org.apache.camel.util.StopWatch)101 Test (org.junit.Test)40 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 CamelExecutionException (org.apache.camel.CamelExecutionException)10 Exchange (org.apache.camel.Exchange)8 CamelExchangeException (org.apache.camel.CamelExchangeException)6 File (java.io.File)5 ExecutorService (java.util.concurrent.ExecutorService)5 AsyncProcessor (org.apache.camel.AsyncProcessor)5 Producer (org.apache.camel.Producer)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Future (java.util.concurrent.Future)4 AsyncCallback (org.apache.camel.AsyncCallback)4 Endpoint (org.apache.camel.Endpoint)4 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)4 NotifyBuilder (org.apache.camel.builder.NotifyBuilder)4 Date (java.util.Date)3 GenericFile (org.apache.camel.component.file.GenericFile)3