001/* 002 * (C) Copyright 2016 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 */ 017package org.nuxeo.ecm.platform.importer.queue.manager; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.concurrent.ArrayBlockingQueue; 022import java.util.concurrent.BlockingQueue; 023 024import org.nuxeo.ecm.platform.importer.log.ImporterLogger; 025import org.nuxeo.ecm.platform.importer.source.SourceNode; 026 027/** 028 * @since 8.3 029 */ 030public abstract class AbstractQueuesManager implements QueuesManager { 031 032 final List<BlockingQueue<SourceNode>> queues; 033 034 protected final int maxQueueSize; 035 036 protected final ImporterLogger log; 037 038 public AbstractQueuesManager(ImporterLogger logger, int queuesNb, int maxQueueSize) { 039 this.maxQueueSize = maxQueueSize; 040 queues = new ArrayList<BlockingQueue<SourceNode>>(queuesNb); 041 for (int i = 0; i < queuesNb; i++) { 042 queues.add(new ArrayBlockingQueue<>(maxQueueSize)); 043 } 044 log = logger; 045 } 046 047 @Override 048 public BlockingQueue<SourceNode> getQueue(int idx) { 049 return queues.get(idx); 050 } 051 052 @Override 053 public boolean isQueueEmpty(int idQueue) { 054 return queues.get(idQueue).isEmpty(); 055 } 056 057 @Override 058 public int dispatch(SourceNode bh) throws InterruptedException { 059 int idx = getTargetQueue(bh, queues.size()); 060 getQueue(idx).put(bh); 061 return idx; 062 } 063 064 protected abstract int getTargetQueue(SourceNode bh, int nbQueues); 065 066 @Override 067 public int getNBConsumers() { 068 return queues.size(); 069 } 070}