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.chronicle.ChronicleConfig; 030import org.nuxeo.ecm.platform.importer.mqueues.kafka.KafkaConfigService; 031import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQManager; 032import org.nuxeo.ecm.platform.importer.mqueues.mqueues.chronicle.ChronicleMQManager; 033import org.nuxeo.ecm.platform.importer.mqueues.mqueues.kafka.KafkaMQManager; 034import org.nuxeo.ecm.platform.importer.mqueues.pattern.message.DocumentMessage; 035import org.nuxeo.ecm.platform.importer.mqueues.pattern.producer.ProducerPool; 036import org.nuxeo.ecm.platform.importer.mqueues.pattern.producer.RandomDocumentMessageProducerFactory; 037import org.nuxeo.runtime.api.Framework; 038 039import java.nio.file.Paths; 040 041/** 042 * @since 9.1 043 */ 044@Operation(id = RandomDocumentProducers.ID, category = Constants.CAT_SERVICES, label = "Produces random blobs", since = "9.1", 045 description = "Produces random blobs in a mqueues.") 046public class RandomDocumentProducers { 047 private static final Log log = LogFactory.getLog(RandomDocumentProducers.class); 048 public static final String ID = "MQImporter.runRandomDocumentProducers"; 049 public static final String DEFAULT_MQ_NAME = "mq-doc"; 050 051 @Context 052 protected OperationContext ctx; 053 054 @Param(name = "nbDocuments") 055 protected Integer nbDocuments; 056 057 @Param(name = "nbThreads", required = false) 058 protected Integer nbThreads = 8; 059 060 @Param(name = "avgBlobSizeKB", required = false) 061 protected Integer avgBlobSizeKB = 1; 062 063 @Param(name = "lang", required = false) 064 protected String lang = "en_US"; 065 066 @Param(name = "mqName", required = false) 067 protected String mqName; 068 069 @Param(name = "mqSize", required = false) 070 protected Integer mqSize; 071 072 @Param(name = "blobInfoPath", required = false) 073 protected String blobInfoPath; 074 075 @Param(name = "kafkaConfig", required = false) 076 protected String kafkaConfig; 077 078 @OperationMethod 079 public void run() { 080 RandomBlobProducers.checkAccess(ctx); 081 try (MQManager<DocumentMessage> manager = getManager()) { 082 manager.createIfNotExists(getMQName(), getMQSize()); 083 ProducerPool<DocumentMessage> producers; 084 if (blobInfoPath != null) { 085 producers = new ProducerPool<>(getMQName(), manager, 086 new RandomDocumentMessageProducerFactory(nbDocuments, lang, Paths.get(blobInfoPath)), nbThreads.shortValue()); 087 } else { 088 producers = new ProducerPool<>(getMQName(), manager, 089 new RandomDocumentMessageProducerFactory(nbDocuments, lang, avgBlobSizeKB), nbThreads.shortValue()); 090 } 091 producers.start().get(); 092 producers.close(); 093 } catch (Exception e) { 094 log.error(e.getMessage(), e); 095 } 096 } 097 098 protected int getMQSize() { 099 if (mqSize != null && mqSize > 0) { 100 return mqSize; 101 } 102 return nbThreads; 103 } 104 105 protected String getMQName() { 106 if (mqName != null) { 107 return mqName; 108 } 109 return DEFAULT_MQ_NAME; 110 } 111 112 protected MQManager<DocumentMessage> getManager() { 113 if (kafkaConfig == null || kafkaConfig.isEmpty()) { 114 return new ChronicleMQManager<>(ChronicleConfig.getBasePath("import"), 115 ChronicleConfig.getRetentionDuration()); 116 } 117 KafkaConfigService service = Framework.getService(KafkaConfigService.class); 118 return new KafkaMQManager<>(service.getZkServers(kafkaConfig), 119 service.getTopicPrefix(kafkaConfig), 120 service.getProducerProperties(kafkaConfig), 121 service.getConsumerProperties(kafkaConfig)); 122 } 123 124}