Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Pattern;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.ShutdownRunningTask;
import org.apache.camel.impl.ScheduledBatchPollingConsumer;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Base class for file consumers.
*/
public abstract class GenericFileConsumer extends ScheduledBatchPollingConsumer {
protected final Logger log = LoggerFactory.getLogger(getClass());
protected GenericFileEndpoint endpoint;
protected GenericFileOperations operations;
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;
private final Pattern includePattern;
private final Pattern excludePattern;
public GenericFileConsumer(GenericFileEndpoint endpoint, Processor processor, GenericFileOperations operations) {
super(endpoint, processor);
this.endpoint = endpoint;
this.operations = operations;
if (endpoint.getInclude() != null) {
this.includePattern = Pattern.compile(endpoint.getInclude(), Pattern.CASE_INSENSITIVE);
} else {
this.includePattern = null;
}
if (endpoint.getExclude() != null) {
this.excludePattern = Pattern.compile(endpoint.getExclude(), Pattern.CASE_INSENSITIVE);
} else {
this.excludePattern = null;
}
}
public Processor getCustomProcessor() {
return customProcessor;
}
/**
* Use a custom processor to process the exchange.
*
* Only set this if you need to do custom processing, instead of the regular processing.
*
* This is for example used to browse file endpoints by leveraging the file consumer to poll
* the directory to gather the list of exchanges. But to avoid processing the files regularly
* we can use a custom processor.
*
* @param processor a custom processor
*/
public void setCustomProcessor(Processor processor) {
this.customProcessor = processor;
}
public boolean isEagerLimitMaxMessagesPerPoll() {
return eagerLimitMaxMessagesPerPoll;
}
public void setEagerLimitMaxMessagesPerPoll(boolean eagerLimitMaxMessagesPerPoll) {
this.eagerLimitMaxMessagesPerPoll = 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;
pendingExchanges = 0;
// before we poll is there anything we need to check?
// 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> files = new ArrayList>();
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) {
Collections.sort(files, endpoint.getSorter());
}
// sort using build in sorters so we can use expressions
// use a linked list so we can dequeue the exchanges
LinkedList exchanges = new LinkedList();
for (GenericFile 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) {
Collections.sort(exchanges, endpoint.getSortBy());
}
if (endpoint.isShuffle()) {
Collections.shuffle(exchanges);
}
// use a queue for the exchanges
Deque 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;
}
public int processBatch(Queue