Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CAMEL-5590: Moved the prepareOnStatup task to first poll in file/ftp …

…consumer, to avoid blocking starting Camel if the task takes a long time.

git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@1435271 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 9dcf31d24795bec4b110b507aa561ef2d221ad1d 1 parent 26195f1
@davsclaus davsclaus authored
View
17 camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -42,12 +42,13 @@
protected final transient Logger log = LoggerFactory.getLogger(getClass());
protected GenericFileEndpoint<T> endpoint;
protected GenericFileOperations<T> operations;
- protected boolean loggedIn;
+ protected volatile boolean loggedIn;
protected String fileExpressionResult;
protected volatile ShutdownRunningTask shutdownRunningTask;
protected volatile int pendingExchanges;
protected Processor customProcessor;
protected boolean eagerLimitMaxMessagesPerPoll = true;
+ protected volatile boolean prepareOnStartup;
public GenericFileConsumer(GenericFileEndpoint<T> endpoint, Processor processor, GenericFileOperations<T> operations) {
super(endpoint, processor);
@@ -86,6 +87,13 @@ public void setEagerLimitMaxMessagesPerPoll(boolean eagerLimitMaxMessagesPerPoll
* 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;
@@ -544,8 +552,11 @@ private void evaluateFileExpression() {
@Override
protected void doStart() throws Exception {
super.doStart();
+ }
- // prepare on startup
- endpoint.getGenericFileProcessStrategy().prepareOnStartup(operations, endpoint);
+ @Override
+ protected void doStop() throws Exception {
+ prepareOnStartup = false;
+ super.doStop();
}
}
View
4 camel-core/src/main/java/org/apache/camel/component/file/GenericFileProcessStrategy.java
@@ -24,12 +24,12 @@
public interface GenericFileProcessStrategy<T> {
/**
- * Allows custom logic to be run on startup preparing the strategy,
+ * Allows custom logic to be run on first poll preparing the strategy,
* such as removing old lock files etc.
*
* @param operations file operations
* @param endpoint the endpoint
- * @throws Exception can be thrown in case of errors which causes startup to fail
+ * @throws Exception can be thrown in case of errors which causes poll to fail
*/
void prepareOnStartup(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint) throws Exception;
View
7 camel-core/src/main/java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategy.java
@@ -25,6 +25,7 @@
import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
import org.apache.camel.component.file.GenericFileOperations;
import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,7 +42,13 @@ public void prepareOnStartup(GenericFileOperations<File> operations, GenericFile
LOG.debug("Prepare on startup by deleting orphaned lock files from: {}", dir);
+ StopWatch watch = new StopWatch();
deleteLockFiles(file, endpoint.isRecursive());
+
+ // log anything that takes more than a second
+ if (watch.taken() > 1000) {
+ LOG.info("Prepared on startup by deleting orphaned lock files from: {} took {} millis to complete.", dir, watch.taken());
+ }
}
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations,
View
81 ...java/org/apache/camel/component/file/strategy/MarkerFileExclusiveReadLockStrategyReadLockFailedTest.java
@@ -1,81 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.file.strategy;
-
-import java.io.File;
-
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
-
-/**
- * Tests the MarkerFileExclusiveReadLockStrategy in a multi-threaded scenario.
- */
-public class MarkerFileExclusiveReadLockStrategyReadLockFailedTest extends ContextTestSupport {
-
- @Override
- protected void setUp() throws Exception {
- deleteDirectory("target/readlock/");
- createDirectory("target/readlock/in");
- super.setUp();
- }
-
- public void testReadLockFailed() throws Exception {
- // should only pickup the 2nd file, as we have a marker file for the first file
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedBodiesReceived("Bye World");
-
- writeFiles();
-
- // wait for files to be fully done using oneExchangeDone
- assertMockEndpointsSatisfied();
- assertTrue(oneExchangeDone.matchesMockWaitTime());
-
- // we should generate an output for the 2nd file
- assertFileExists("target/readlock/out/file2.dat");
-
- // and the marker file from the 1st file is still there, and the 1st file as well
- assertFileExists("target/readlock/in/file1.dat.camelLock");
- assertFileExists("target/readlock/in/file1.dat");
- }
-
- private void writeFiles() throws Exception {
- log.debug("Writing files...");
-
- // create a camelLock file first, so file1 will not be picked up
- File lock = new File("target/readlock/in/file1.dat.camelLock");
- lock.createNewFile();
-
- template.sendBodyAndHeader("file:target/readlock/in", "Hello World", Exchange.FILE_NAME, "file1.dat");
- template.sendBodyAndHeader("file:target/readlock/in", "Bye World", Exchange.FILE_NAME, "file2.dat");
- }
-
- @Override
- protected RouteBuilder createRouteBuilder() throws Exception {
- return new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- from("file:target/readlock/in?readLock=markerFile")
- .to("file:target/readlock/out")
- .convertBodyTo(String.class)
- .to("mock:result");
- }
- };
- }
-
-}
Please sign in to comment.
Something went wrong with that request. Please try again.