[ https://issues.apache.org/jira/browse/CAMEL-11250?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16288878#comment-16288878 ]
ASF GitHub Bot commented on CAMEL-11250: ---------------------------------------- davsclaus closed pull request #2123: CAMEL-11250: File name pattern and file predicate for poll enrich use. URL: https://github.com/apache/camel/pull/2123 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java index 6110201600a..bb07d5560e5 100644 --- a/camel-core/src/main/java/org/apache/camel/Exchange.java +++ b/camel-core/src/main/java/org/apache/camel/Exchange.java @@ -141,6 +141,8 @@ String FILE_LOCK_RANDOM_ACCESS_FILE = "CamelFileLockRandomAccessFile"; String FILTER_MATCHED = "CamelFilterMatched"; String FILTER_NON_XML_CHARS = "CamelFilterNonXmlChars"; + String FILE_NAME_PATTERN = "CamelFileNamePattern"; + String FILE_PREDICATE = "CamelFilePredicate"; String GROUPED_EXCHANGE = "CamelGroupedExchange"; diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java index 619bf6b4686..25aea3b0223 100644 --- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java +++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java @@ -22,6 +22,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Queue; +import java.util.function.Predicate; import java.util.regex.Pattern; import org.apache.camel.Exchange; @@ -53,6 +54,8 @@ protected volatile boolean prepareOnStartup; private final Pattern includePattern; private final Pattern excludePattern; + private Pattern fileNamePattern; + private Predicate<GenericFile> fileNamePredicate; public GenericFileConsumer(GenericFileEndpoint<T> endpoint, Processor processor, GenericFileOperations<T> operations) { super(endpoint, processor); @@ -90,6 +93,14 @@ public void setEagerLimitMaxMessagesPerPoll(boolean eagerLimitMaxMessagesPerPoll this.eagerLimitMaxMessagesPerPoll = eagerLimitMaxMessagesPerPoll; } + public void setFileNamePattern(String fileNamePattern) { + this.fileNamePattern = Pattern.compile(fileNamePattern, Pattern.CASE_INSENSITIVE); + } + + public void setFileNamePredicate(Predicate<GenericFile> fileNamePredicate) { + this.fileNamePredicate = fileNamePredicate; + } + /** * Poll for files */ @@ -666,6 +677,18 @@ protected boolean isMatched(GenericFile<T> file, boolean isDirectory, List<T> fi } } + if (fileNamePattern != null) { + if (!fileNamePattern.matcher(name).matches()) { + return false; + } + } + + if (fileNamePredicate != null) { + if (!fileNamePredicate.test(file)) { + return false; + } + } + return true; } diff --git a/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java b/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java index 61a00d5cfb9..1e48a00501f 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java +++ b/camel-core/src/main/java/org/apache/camel/processor/PollEnricher.java @@ -16,6 +16,7 @@ */ package org.apache.camel.processor; +import java.util.function.Predicate; import org.apache.camel.AsyncCallback; import org.apache.camel.AsyncProcessor; import org.apache.camel.CamelContext; @@ -26,6 +27,8 @@ import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.PollingConsumer; +import org.apache.camel.component.file.GenericFile; +import org.apache.camel.component.file.GenericFileConsumer; import org.apache.camel.impl.BridgeExceptionHandlerToErrorHandler; import org.apache.camel.impl.ConsumerCache; import org.apache.camel.impl.DefaultConsumer; @@ -217,6 +220,18 @@ public boolean process(Exchange exchange, AsyncCallback callback) { Consumer delegate = consumer; if (consumer instanceof EventDrivenPollingConsumer) { delegate = ((EventDrivenPollingConsumer) consumer).getDelegateConsumer(); + + if (delegate instanceof GenericFileConsumer) { + String fileNamePattern = exchange.getIn().getHeader(Exchange.FILE_NAME_PATTERN, String.class); + if (fileNamePattern != null) { + ((GenericFileConsumer) delegate).setFileNamePattern(fileNamePattern); + } + + Predicate<GenericFile> fileNamePredicate = (Predicate<GenericFile>) exchange.getIn().getHeader(Exchange.FILE_PREDICATE); + if (fileNamePredicate != null) { + ((GenericFileConsumer) delegate).setFileNamePredicate(fileNamePredicate); + } + } } // is the consumer bridging the error handler? diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumePollEnrichFileNamePatternTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumePollEnrichFileNamePatternTest.java new file mode 100644 index 00000000000..6fa2dd96fb6 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumePollEnrichFileNamePatternTest.java @@ -0,0 +1,74 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +public class FileConsumePollEnrichFileNamePatternTest extends ContextTestSupport { + + @Override + protected void setUp() throws Exception { + deleteDirectory("target/enrich"); + deleteDirectory("target/enrichdata"); + super.setUp(); + } + + public void testPollEnrichFileNameFilterOut() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedBodiesReceived((Object) null); + + template.sendBody("seda:start", "Start"); + log.info("Sleeping for 1/4 sec before writing enrichdata file"); + Thread.sleep(250); + template.sendBodyAndHeader("file://target/enrichdata", "Big file", Exchange.FILE_NAME, "BBB.dat"); + log.info("... write done"); + + assertMockEndpointsSatisfied(); + } + + public void testPollEnrichFileNameFilterIn() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedBodiesReceived("Big file"); + + template.sendBody("seda:start", "Start"); + log.info("Sleeping for 1/4 sec before writing enrichdata file"); + Thread.sleep(250); + template.sendBodyAndHeader("file://target/enrichdata", "Big file", Exchange.FILE_NAME, "AAA.dat"); + log.info("... write done"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:start") + .setHeader(Exchange.FILE_NAME_PATTERN, () -> "A{3}\\.dat") + .pollEnrich("file://target/enrichdata?initialDelay=0&delay=10&move=.done", 1000) + .to("mock:result"); + } + }; + } + +} \ No newline at end of file diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumePollEnrichFilePredicateTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumePollEnrichFilePredicateTest.java new file mode 100644 index 00000000000..3b201693cb9 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumePollEnrichFilePredicateTest.java @@ -0,0 +1,75 @@ +/** + * 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; + +import java.util.function.Predicate; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +public class FileConsumePollEnrichFilePredicateTest extends ContextTestSupport { + + @Override + protected void setUp() throws Exception { + deleteDirectory("target/enrich"); + deleteDirectory("target/enrichdata"); + super.setUp(); + } + + public void testPollEnrichFileReject() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedBodiesReceived((Object) null); + + template.sendBody("seda:start", "Start"); + log.info("Sleeping for 1/4 sec before writing enrichdata file"); + Thread.sleep(250); + template.sendBodyAndHeader("file://target/enrichdata", "O", Exchange.FILE_NAME, "AAA.dat"); + log.info("... write done"); + + assertMockEndpointsSatisfied(); + } + + public void testPollEnrichFileAccept() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedBodiesReceived("Hi Camel!"); + + template.sendBody("seda:start", "Start"); + log.info("Sleeping for 1/4 sec before writing enrichdata file"); + Thread.sleep(250); + template.sendBodyAndHeader("file://target/enrichdata", "Hi Camel!", Exchange.FILE_NAME, "AAA.dat"); + log.info("... write done"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:start") + .setHeader(Exchange.FILE_PREDICATE, () -> (Predicate<GenericFile>) genericFile -> genericFile.getFileLength() > 4) + .pollEnrich("file://target/enrichdata?initialDelay=0&delay=10&move=.done", 1000) + .to("mock:result"); + } + }; + } + +} \ No newline at end of file ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > File consumer - Allow to specify a header that has the file name to poll > ------------------------------------------------------------------------ > > Key: CAMEL-11250 > URL: https://issues.apache.org/jira/browse/CAMEL-11250 > Project: Camel > Issue Type: Improvement > Components: camel-core > Reporter: Claus Ibsen > Fix For: Future > > > See SO > http://stackoverflow.com/questions/43849165/camel-pollenrich-generating-a-lot-of-timed-waiting-threads > We can have a FILE_FILTER_NAME header that matches in the > org.apache.camel.component.file.GenericFileConsumer#isMatched(org.apache.camel.component.file.GenericFile<T>, > boolean, java.util.List<T>) > so end users can set the header first and then do a pollEnrich with a static > endpoint. > The header can then use equalsIgnoreCase and reguar exp for matching, to make > it easier to match files. -- This message was sent by Atlassian JIRA (v6.4.14#64029)