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 org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.platform.importer.mqueues.message.DocumentMessage;
030import org.nuxeo.ecm.platform.importer.mqueues.mqueues.CQMQueues;
031import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQueues;
032import org.nuxeo.ecm.platform.importer.mqueues.producer.ProducerPool;
033import org.nuxeo.ecm.platform.importer.mqueues.producer.RandomDocumentMessageProducerFactory;
034
035import java.io.File;
036import java.nio.file.Paths;
037
038/**
039 * @since 9.1
040 */
041@Operation(id = RandomDocumentProducers.ID, category = Constants.CAT_SERVICES, label = "Produces random blobs", since = "9.1",
042        description = "Produces random blobs in a mqueues.")
043public class RandomDocumentProducers {
044    private static final Log log = LogFactory.getLog(RandomDocumentProducers.class);
045    public static final String ID = "MQImporter.runRandomDocumentProducers";
046    public static final String DEFAULT_DOC_QUEUE_NAME = "mq-doc";
047
048    @Context
049    protected OperationContext ctx;
050
051    @Param(name = "nbDocuments")
052    protected Integer nbDocuments;
053
054    @Param(name = "nbThreads", required = false)
055    protected Integer nbThreads = 8;
056
057    @Param(name = "avgBlobSizeKB", required = false)
058    protected Integer avgBlobSizeKB = 1;
059
060    @Param(name = "lang", required = false)
061    protected String lang = "en_US";
062
063    @Param(name = "queuePath", required = false)
064    protected String queuePath;
065
066    @Param(name = "blobInfoPath", required = false)
067    protected String blobInfoPath;
068
069    @OperationMethod
070    public void run() {
071        RandomBlobProducers.checkAccess(ctx);
072        queuePath = getQueuePath();
073        try (MQueues<DocumentMessage> mQueues = new CQMQueues<>(new File(queuePath), nbThreads)) {
074            ProducerPool<DocumentMessage> producers;
075            if (blobInfoPath != null) {
076                producers = new ProducerPool<>(mQueues,
077                        new RandomDocumentMessageProducerFactory(nbDocuments, lang, Paths.get(blobInfoPath)), nbThreads);
078            } else {
079                producers = new ProducerPool<>(mQueues,
080                        new RandomDocumentMessageProducerFactory(nbDocuments, lang, avgBlobSizeKB), nbThreads);
081            }
082            producers.start().get();
083        } catch (Exception e) {
084            log.error(e.getMessage(), e);
085        }
086    }
087
088    private String getQueuePath() {
089        if (queuePath != null && !queuePath.isEmpty()) {
090            return queuePath;
091        }
092        return getDefaultDocumentQueuePath();
093    }
094
095    public static String getDefaultDocumentQueuePath() {
096        return RandomBlobProducers.getDefaultQueuePath(DEFAULT_DOC_QUEUE_NAME);
097    }
098
099}