001/*
002 * (C) Copyright 2017 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 *     bdelbosc
018 */
019package org.nuxeo.ecm.platform.importer.mqueues.pattern.producer;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQManager;
024import org.nuxeo.ecm.platform.importer.mqueues.pattern.Message;
025import org.nuxeo.ecm.platform.importer.mqueues.pattern.consumer.internals.AbstractCallablePool;
026import org.nuxeo.ecm.platform.importer.mqueues.pattern.producer.internals.ProducerRunner;
027
028import java.util.List;
029import java.util.concurrent.Callable;
030
031/**
032 * A Pool of ProducerRunner
033 *
034 * @since 9.1
035 */
036public class ProducerPool<M extends Message> extends AbstractCallablePool<ProducerStatus> {
037    private static final Log log = LogFactory.getLog(ProducerPool.class);
038    private final MQManager<M> manager;
039    private final ProducerFactory<M> factory;
040    private final String mqName;
041
042    public ProducerPool(String mqName, MQManager<M> manager, ProducerFactory<M> factory, short nbThreads) {
043        super(nbThreads);
044        this.mqName = mqName;
045        this.manager = manager;
046        this.factory = factory;
047    }
048
049    @Override
050    protected ProducerStatus getErrorStatus() {
051        return new ProducerStatus(0, 0, 0, 0, true);
052    }
053
054    @Override
055    protected Callable<ProducerStatus> getCallable(int i) {
056        return new ProducerRunner<>(factory, manager.getAppender(mqName), i);
057    }
058
059    @Override
060    protected String getThreadPrefix() {
061        return "Nuxeo-Producer";
062    }
063
064    @Override
065    protected void afterCall(List<ProducerStatus> ret) {
066        ret.forEach(log::info);
067        log.warn(ProducerStatus.toString(ret));
068    }
069
070
071}