
org.apache.camel.component.aws.s3.S3Consumer Maven / Gradle / Ivy
/**
* 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.aws.s3;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.NoFactoryAvailableException;
import org.apache.camel.Processor;
import org.apache.camel.impl.ScheduledBatchPollingConsumer;
import org.apache.camel.spi.Synchronization;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.URISupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Consumer of messages from the Amazon Web Service Simple Storage Service
* AWS S3
*
*/
public class S3Consumer extends ScheduledBatchPollingConsumer {
private static final transient Logger LOG = LoggerFactory.getLogger(S3Consumer.class);
public S3Consumer(S3Endpoint endpoint, Processor processor) throws NoFactoryAvailableException {
super(endpoint, processor);
}
@Override
protected int poll() throws Exception {
// must reset for each poll
shutdownRunningTask = null;
pendingExchanges = 0;
String bucketName = getConfiguration().getBucketName();
LOG.trace("Queueing objects in bucket [{}]...", bucketName);
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
listObjectsRequest.setBucketName(bucketName);
listObjectsRequest.setPrefix(getConfiguration().getPrefix());
listObjectsRequest.setMaxKeys(maxMessagesPerPoll);
ObjectListing listObjects = getAmazonS3Client().listObjects(listObjectsRequest);
if (LOG.isTraceEnabled()) {
LOG.trace("Found {} objects in bucket [{}]...", listObjects.getObjectSummaries().size(), bucketName);
}
Queue exchanges = createExchanges(listObjects.getObjectSummaries());
return processBatch(CastUtils.cast(exchanges));
}
protected Queue createExchanges(List s3ObjectSummaries) {
if (LOG.isTraceEnabled()) {
LOG.trace("Received {} messages in this poll", s3ObjectSummaries.size());
}
Queue answer = new LinkedList();
for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
S3Object s3Object = getAmazonS3Client().getObject(s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey());
Exchange exchange = getEndpoint().createExchange(s3Object);
answer.add(exchange);
}
return answer;
}
public int processBatch(Queue
© 2015 - 2025 Weber Informatics LLC | Privacy Policy