You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/06/07 10:21:05 UTC

svn commit: r782345 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ main/java/org/apache/camel/component/file/strategy/ test/java/org/apache/camel/component/file/stress/ test/resources/

Author: davsclaus
Date: Sun Jun  7 08:21:05 2009
New Revision: 782345

URL: http://svn.apache.org/viewvc?rev=782345&view=rev
Log:
CAMEL-1670: First cut of a fix for file consumer detecting in progress files.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
    camel/trunk/camel-core/src/test/resources/log4j.properties

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?rev=782345&r1=782344&r2=782345&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java Sun Jun  7 08:21:05 2009
@@ -64,8 +64,12 @@
                 }
             } else if (file.isFile()) {
                 if (isValidFile(gf, false)) {
-                    // matched file so add
-                    fileList.add(gf);
+                    if (isInProgress(file, files)) {
+                        log.trace("Skipping as file is already in progress: " + file);
+                    } else {
+                        // matched file so add
+                        fileList.add(gf);
+                    }
                 }
             } else {
                 log.debug("Ignoring unsupported file type for file: " + file);
@@ -74,6 +78,27 @@
     }
 
     /**
+     * Is the given file already in progress.
+     *
+     * @param target  the target file
+     * @param files   the list of files found in the current directory
+     * @return <tt>true</tt> if the file is already in progress
+     */
+    protected boolean isInProgress(File target, File[] files) {
+        for (File file : files) {
+            String name = file.getName();
+            if (name.endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
+                String before = ObjectHelper.before(name, FileComponent.DEFAULT_LOCK_FILE_POSTFIX);
+                if (target.getName().equals(before)) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    /**
      * Creates a new GenericFile<File> based on the given file.
      *
      * @param endpointPath the starting directory the endpoint was configued with

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java?rev=782345&r1=782344&r2=782345&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java Sun Jun  7 08:21:05 2009
@@ -121,12 +121,6 @@
         } finally {
             // must close channel first
             ObjectHelper.close(channel, "while acquiring exclusive read lock for file: " + lockFileName, LOG);
-            // need to delete the lock file
-            File lockfile = new File(lockFileName);
-            boolean deleted = lockfile.delete();
-            if (LOG.isTraceEnabled()) {
-                LOG.trace("Exclusive lock file: " + lockFileName + " was deleted: " + deleted);
-            }
         }
     }
 

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java?rev=782345&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java Sun Jun  7 08:21:05 2009
@@ -0,0 +1,64 @@
+/**
+ * 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.stress;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class FileAsyncStressFileDropper extends ContextTestSupport {
+
+    private static int counter;
+
+    public static String getFilename() {
+        return "" + counter++ + ".txt";
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        deleteDirectory("target/filestress");
+    }
+
+    public void testDropInNewFiles() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(250);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // generate a new file continously
+                from("timer:foo?period=50")
+                    .setHeader(Exchange.FILE_NAME, bean(FileAsyncStressFileDropper.class, "getFilename"))
+                    .setBody(constant("Hello World"))
+                    .to("file:target/filestress")
+                    .to("mock:result");
+
+            }
+        };
+    }
+
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressFileDropper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java?rev=782345&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java Sun Jun  7 08:21:05 2009
@@ -0,0 +1,60 @@
+/**
+ * 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.stress;
+
+import java.util.Random;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class FileAsyncStressManually extends ContextTestSupport {
+
+    public void testAsyncStress() throws Exception {
+        // test by starting the unit test FileAsyncStressFileDropper in another JVM
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(250);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/filestress?readLock=markerFile&maxMessagesPerPoll=25&move=backup")
+                    .threads(10)
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            // simulate some work with random time to complete
+                            Random ran = new Random();
+                            int delay = ran.nextInt(500) + 10;
+                            Thread.sleep(delay);
+                        }
+                    }).to("mock:result");
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/stress/FileAsyncStressManually.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/log4j.properties?rev=782345&r1=782344&r2=782345&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/resources/log4j.properties (original)
+++ camel/trunk/camel-core/src/test/resources/log4j.properties Sun Jun  7 08:21:05 2009
@@ -23,6 +23,7 @@
 log4j.logger.org.apache.activemq.spring=WARN
 #log4j.logger.org.apache.camel=DEBUG
 #log4j.logger.org.apache.camel.component=TRACE
+#log4j.logger.org.apache.camel.component.file=TRACE
 log4j.logger.org.apache.camel.impl.converter=WARN
 log4j.logger.org.apache.camel.management=WARN
 log4j.logger.org.apache.camel.impl.DefaultPackageScanClassResolver=WARN