Search in sources :

Example 61 with StopWatch

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

the class GenericFileConsumer method poll.

/**
     * Poll for files
     */
protected int poll() throws Exception {
    // must prepare on startup the very first time
    if (!prepareOnStartup) {
        // prepare on startup
        endpoint.getGenericFileProcessStrategy().prepareOnStartup(operations, endpoint);
        prepareOnStartup = true;
    }
    // must reset for each poll
    fileExpressionResult = null;
    shutdownRunningTask = null;
    pendingExchanges = 0;
    // such as are we connected to the FTP Server still?
    if (!prePollCheck()) {
        log.debug("Skipping poll as pre poll check returned false");
        return 0;
    }
    // gather list of files to process
    List<GenericFile<T>> files = new ArrayList<GenericFile<T>>();
    String name = endpoint.getConfiguration().getDirectory();
    // time how long it takes to poll
    StopWatch stop = new StopWatch();
    boolean limitHit;
    try {
        limitHit = !pollDirectory(name, files, 0);
    } catch (Exception e) {
        // during poll directory we add files to the in progress repository, in case of any exception thrown after this work
        // we must then drain the in progress files before rethrowing the exception
        log.debug("Error occurred during poll directory: " + name + " due " + e.getMessage() + ". Removing " + files.size() + " files marked as in-progress.");
        removeExcessiveInProgressFiles(files);
        throw e;
    }
    long delta = stop.stop();
    if (log.isDebugEnabled()) {
        log.debug("Took {} to poll: {}", TimeUtils.printDuration(delta), name);
    }
    // log if we hit the limit
    if (limitHit) {
        log.debug("Limiting maximum messages to poll at {} files as there were more messages in this poll.", maxMessagesPerPoll);
    }
    // sort files using file comparator if provided
    if (endpoint.getSorter() != null) {
        files.sort(endpoint.getSorter());
    }
    // sort using build in sorters so we can use expressions
    // use a linked list so we can dequeue the exchanges
    LinkedList<Exchange> exchanges = new LinkedList<Exchange>();
    for (GenericFile<T> file : files) {
        Exchange exchange = endpoint.createExchange(file);
        endpoint.configureExchange(exchange);
        endpoint.configureMessage(file, exchange.getIn());
        exchanges.add(exchange);
    }
    // sort files using exchange comparator if provided
    if (endpoint.getSortBy() != null) {
        exchanges.sort(endpoint.getSortBy());
    }
    if (endpoint.isShuffle()) {
        Collections.shuffle(exchanges);
    }
    // use a queue for the exchanges
    Deque<Exchange> q = exchanges;
    // we are not eager limiting, but we have configured a limit, so cut the list of files
    if (!eagerLimitMaxMessagesPerPoll && maxMessagesPerPoll > 0) {
        if (files.size() > maxMessagesPerPoll) {
            log.debug("Limiting maximum messages to poll at {} files as there were more messages in this poll.", maxMessagesPerPoll);
            // must first remove excessive files from the in progress repository
            removeExcessiveInProgressFiles(q, maxMessagesPerPoll);
        }
    }
    // consume files one by one
    int total = exchanges.size();
    if (total > 0) {
        log.debug("Total {} files to consume", total);
    }
    int polledMessages = processBatch(CastUtils.cast(q));
    postPollCheck(polledMessages);
    return polledMessages;
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) StopWatch(org.apache.camel.util.StopWatch) Exchange(org.apache.camel.Exchange)

Example 62 with StopWatch

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

the class GenericFileRenameExclusiveReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception {
    LOG.trace("Waiting for exclusive read lock to file: {}", file);
    // the trick is to try to rename the file, if we can rename then we have exclusive read
    // since its a Generic file we cannot use java.nio to get a RW lock
    String newName = file.getFileName() + ".camelExclusiveReadLock";
    // make a copy as result and change its file name
    GenericFile<T> newFile = file.copyFrom(file);
    newFile.changeFileName(newName);
    StopWatch watch = new StopWatch();
    boolean exclusive = false;
    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;
            }
        }
        try {
            exclusive = operations.renameFile(file.getAbsoluteFilePath(), newFile.getAbsoluteFilePath());
        } catch (GenericFileOperationFailedException ex) {
            if (ex.getCause() != null && ex.getCause() instanceof FileNotFoundException) {
                exclusive = false;
            } else {
                throw ex;
            }
        }
        if (exclusive) {
            LOG.trace("Acquired exclusive read lock to file: {}", file);
            // rename it back so we can read it
            operations.renameFile(newFile.getAbsoluteFilePath(), file.getAbsoluteFilePath());
        } else {
            boolean interrupted = sleep();
            if (interrupted) {
                // we were interrupted while sleeping, we are likely being shutdown so return false
                return false;
            }
        }
    }
    return true;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) FileNotFoundException(java.io.FileNotFoundException) StopWatch(org.apache.camel.util.StopWatch)

Example 63 with StopWatch

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

the class DirectVmBlockingProducer method awaitConsumer.

private DirectVmConsumer awaitConsumer() throws InterruptedException {
    DirectVmConsumer answer = null;
    StopWatch watch = new StopWatch();
    boolean done = false;
    while (!done) {
        // sleep a bit to give chance for the consumer to be ready
        Thread.sleep(500);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Waited {} for consumer to be ready", watch.taken());
        }
        answer = endpoint.getConsumer();
        if (answer != null) {
            return answer;
        }
        // we are done if we hit the timeout
        done = watch.taken() >= endpoint.getTimeout();
    }
    return answer;
}
Also used : StopWatch(org.apache.camel.util.StopWatch)

Example 64 with StopWatch

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

the class NettyConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    StopWatch watch = new StopWatch();
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(files).create();
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    // we access the responses Map below only inside the main thread,
    // so no need for a thread-safe Map implementation
    Map<Integer, Future<String>> responses = new HashMap<Integer, Future<String>>();
    for (int i = 0; i < files; i++) {
        final int index = i;
        Future<String> out = executor.submit(new Callable<String>() {

            public String call() throws Exception {
                String reply = template.requestBody("netty4:tcp://localhost:{{port}}", index, String.class);
                log.debug("Sent {} received {}", index, reply);
                assertEquals("Bye " + index, reply);
                return reply;
            }
        });
        responses.put(index, out);
    }
    notify.matches(2, TimeUnit.MINUTES);
    log.info("Took " + watch.taken() + " millis to process " + files + " messages using " + poolSize + " client threads.");
    assertEquals(files, responses.size());
    // get all responses
    Set<String> unique = new HashSet<String>();
    for (Future<String> future : responses.values()) {
        unique.add(future.get());
    }
    // should be 'files' unique responses
    assertEquals("Should be " + files + " unique responses", files, unique.size());
    executor.shutdownNow();
}
Also used : HashMap(java.util.HashMap) StopWatch(org.apache.camel.util.StopWatch) NotifyBuilder(org.apache.camel.builder.NotifyBuilder) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) HashSet(java.util.HashSet)

Example 65 with StopWatch

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

the class AsyncJmsInOutIT 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)

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