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.lib.stream.pattern.producer;
020
021import java.util.List;
022import java.util.concurrent.Callable;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.lib.stream.log.LogManager;
027import org.nuxeo.lib.stream.pattern.Message;
028import org.nuxeo.lib.stream.pattern.consumer.internals.AbstractCallablePool;
029import org.nuxeo.lib.stream.pattern.producer.internals.ProducerRunner;
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
039    protected final LogManager manager;
040
041    protected final ProducerFactory<M> factory;
042
043    protected final String logName;
044
045    public ProducerPool(String logName, LogManager manager, ProducerFactory<M> factory, short nbThreads) {
046        super(nbThreads);
047        this.logName = logName;
048        this.manager = manager;
049        this.factory = factory;
050    }
051
052    @Override
053    protected ProducerStatus getErrorStatus() {
054        return new ProducerStatus(0, 0, 0, 0, true);
055    }
056
057    @Override
058    protected Callable<ProducerStatus> getCallable(int i) {
059        return new ProducerRunner<>(factory, manager.getAppender(logName), i);
060    }
061
062    @Override
063    protected String getThreadPrefix() {
064        return "Nuxeo-Producer";
065    }
066
067    @Override
068    protected void afterCall(List<ProducerStatus> ret) {
069        ret.forEach(log::info);
070        log.warn(ProducerStatus.toString(ret));
071    }
072
073}