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.chronicle.ChronicleConfig;
031import org.nuxeo.ecm.platform.importer.mqueues.kafka.KafkaConfigService;
032import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQManager;
033import org.nuxeo.ecm.platform.importer.mqueues.mqueues.chronicle.ChronicleMQManager;
034import org.nuxeo.ecm.platform.importer.mqueues.mqueues.kafka.KafkaMQManager;
035import org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer.BatchPolicy;
036import org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer.BlobMessageConsumerFactory;
037import org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer.ConsumerPolicy;
038import org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer.ConsumerPool;
039import org.nuxeo.ecm.platform.importer.mqueues.pattern.message.BlobMessage;
040import org.nuxeo.runtime.api.Framework;
041
042import java.nio.file.Paths;
043import java.time.Duration;
044import java.util.concurrent.TimeUnit;
045
046/**
047 * @since 9.1
048 */
049@Operation(id = BlobConsumers.ID, category = Constants.CAT_SERVICES, label = "Import blobs", since = "9.1",
050        description = "Import mqueues blob into the binarystore.")
051public class BlobConsumers {
052    private static final Log log = LogFactory.getLog(BlobConsumers.class);
053    public static final String ID = "MQImporter.runBlobConsumers";
054
055    @Context
056    protected OperationContext ctx;
057
058    @Param(name = "nbThreads", required = false)
059    protected Integer nbThreads;
060
061    @Param(name = "blobInfoPath")
062    protected String blobInfoPath;
063
064    @Param(name = "blobProviderName", required = false)
065    protected String blobProviderName = "default";
066
067    @Param(name = "batchSize", required = false)
068    protected Integer batchSize = 10;
069
070    @Param(name = "batchThresholdS", required = false)
071    protected Integer batchThresholdS = 20;
072
073    @Param(name = "retryMax", required = false)
074    protected Integer retryMax = 3;
075
076    @Param(name = "retryDelayS", required = false)
077    protected Integer retryDelayS = 2;
078
079    @Param(name = "mqName", required = false)
080    protected String mqName;
081
082    @Param(name = "kafkaConfig", required = false)
083    protected String kafkaConfig;
084
085    @OperationMethod
086    public void run() {
087        RandomBlobProducers.checkAccess(ctx);
088        ConsumerPolicy consumerPolicy = ConsumerPolicy.builder()
089                .name(ID)
090                .batchPolicy(BatchPolicy.builder().capacity(batchSize)
091                        .timeThreshold(Duration.ofSeconds(batchThresholdS)).build())
092                .retryPolicy(new RetryPolicy().withMaxRetries(retryMax).withDelay(retryDelayS, TimeUnit.SECONDS))
093                .maxThreads(getNbThreads())
094                .build();
095        try (MQManager<BlobMessage> manager = getManager();
096             ConsumerPool<BlobMessage> consumers = new ConsumerPool<>(getMQName(), manager,
097                     new BlobMessageConsumerFactory(blobProviderName, Paths.get(blobInfoPath)),
098                     consumerPolicy)) {
099            consumers.start().get();
100        } catch (Exception e) {
101            log.error(e.getMessage(), e);
102        }
103    }
104
105    protected short getNbThreads() {
106        if (nbThreads != null) {
107            return nbThreads.shortValue();
108        }
109        return 0;
110    }
111
112    protected String getMQName() {
113        if (mqName != null) {
114            return mqName;
115        }
116        return RandomBlobProducers.DEFAULT_MQ_NAME;
117    }
118
119    protected MQManager<BlobMessage> getManager() {
120        if (kafkaConfig == null || kafkaConfig.isEmpty()) {
121            return new ChronicleMQManager<>(ChronicleConfig.getBasePath("import"),
122                    ChronicleConfig.getRetentionDuration());
123        }
124        KafkaConfigService service = Framework.getService(KafkaConfigService.class);
125        return new KafkaMQManager<>(service.getZkServers(kafkaConfig),
126                service.getTopicPrefix(kafkaConfig),
127                service.getProducerProperties(kafkaConfig),
128                service.getConsumerProperties(kafkaConfig));
129    }
130
131}