001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Benoit Delbosc
018 */
019package org.nuxeo.ecm.platform.importer.mqueues.automation;
020
021import net.jodah.failsafe.RetryPolicy;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.platform.importer.mqueues.consumer.BatchPolicy;
031import org.nuxeo.ecm.platform.importer.mqueues.consumer.BlobMessageConsumerFactory;
032import org.nuxeo.ecm.platform.importer.mqueues.consumer.ConsumerPolicy;
033import org.nuxeo.ecm.platform.importer.mqueues.consumer.ConsumerPool;
034import org.nuxeo.ecm.platform.importer.mqueues.message.BlobMessage;
035import org.nuxeo.ecm.platform.importer.mqueues.mqueues.CQMQueues;
036import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQueues;
037
038import java.io.File;
039import java.nio.file.Paths;
040import java.time.Duration;
041import java.util.concurrent.TimeUnit;
042
043/**
044 * @since 9.1
045 */
046@Operation(id = BlobConsumers.ID, category = Constants.CAT_SERVICES, label = "Import blobs", since = "9.1",
047        description = "Import mqueues blob into the binarystore.")
048public class BlobConsumers {
049    private static final Log log = LogFactory.getLog(BlobConsumers.class);
050    public static final String ID = "MQImporter.runBlobConsumers";
051
052    @Context
053    protected OperationContext ctx;
054
055    @Param(name = "blobInfoPath")
056    protected String blobInfoPath;
057
058    @Param(name = "blobProviderName", required = false)
059    protected String blobProviderName = "default";
060
061    @Param(name = "batchSize", required = false)
062    protected Integer batchSize = 10;
063
064    @Param(name = "batchThresholdS", required = false)
065    protected Integer batchThresholdS = 20;
066
067    @Param(name = "retryMax", required = false)
068    protected Integer retryMax = 3;
069
070    @Param(name = "retryDelayS", required = false)
071    protected Integer retryDelayS = 2;
072
073    @Param(name = "queuePath", required = false)
074    protected String queuePath;
075
076    @OperationMethod
077    public void run() {
078        RandomBlobProducers.checkAccess(ctx);
079        queuePath = getQueuePath();
080        try (MQueues<BlobMessage> mQueues = new CQMQueues<>(new File(queuePath))) {
081            ConsumerPool<BlobMessage> consumers = new ConsumerPool<>(mQueues,
082                    new BlobMessageConsumerFactory(blobProviderName, Paths.get(blobInfoPath)),
083                    ConsumerPolicy.builder()
084                            .batchPolicy(BatchPolicy.builder().capacity(batchSize)
085                                    .timeThreshold(Duration.ofSeconds(batchThresholdS)).build())
086                            .retryPolicy(new RetryPolicy().withMaxRetries(retryMax).withDelay(retryDelayS, TimeUnit.SECONDS)).build());
087            consumers.start().get();
088        } catch (Exception e) {
089            log.error(e.getMessage(), e);
090        }
091    }
092
093    private String getQueuePath() {
094        if (queuePath != null && !queuePath.isEmpty()) {
095            return queuePath;
096        }
097        return RandomBlobProducers.getDefaultBlobQueuePath();
098    }
099
100
101}