/*
* Copyright (C) 2010 Alexander Kolosov
*
* This file is part of FlowBrook.
*
* FlowBrook is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* FlowBrook is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FlowBrook. If not, see <http://www.gnu.org/licenses/>
*/
package ru.petrsu.akolosov.flowbrook.processing;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.petrsu.akolosov.flowbrook.FlowRecordIterator;
import ru.petrsu.akolosov.flowbrook.FlowRecordPool;
import ru.petrsu.akolosov.flowbrook.FlowSourceException;
import ru.petrsu.akolosov.flowbrook.FlowSourceSet;
/**
*
* @author kas
*/
public class ConcurrentFlowSourceSetHandler extends FlowSourceSetHandler {
private final ExecutorService executor;
private final SortedMap<Date, Future< List<FlowRecordHandler>> > result =
new TreeMap<Date, Future< List<FlowRecordHandler>> >();
private final FlowRecordPool pool = new FlowRecordPool();
public ConcurrentFlowSourceSetHandler(FlowSourceSet sourcesSet,
Collection<FlowDataProcessingRule> rules) {
this(sourcesSet, rules, Runtime.getRuntime().availableProcessors());
}
public ConcurrentFlowSourceSetHandler(FlowSourceSet sourcesSet,
Collection<FlowDataProcessingRule> rules,
int threadPoolSize) {
super(sourcesSet, rules);
executor = Executors.newFixedThreadPool(threadPoolSize);
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Using " + threadPoolSize + " threads");
}
@Override
protected void processAction(FlowSourceSet sourcesSet, final List<FlowDataProcessingRule> rules) throws FlowProcessingException{
final FlowRecordIterator iterator;
try {
iterator = sourcesSet.getRecordIterator(pool);
} catch (FlowSourceException ex) {
throw new FlowProcessingException(ex);
}
final Date startDate = sourcesSet.getStartDate();
final Date endDate = sourcesSet.getEndDate();
Future< List<FlowRecordHandler> > future =
executor.submit(new Callable< List<FlowRecordHandler> >() {
public List<FlowRecordHandler> call() throws Exception {
FlowSourceHandler sourceHandler = new FlowSourceHandler(rules);
return sourceHandler.process(iterator, startDate, endDate);
}
});
result.put(startDate, future);
}
@Override
protected void postProcessAction() {
executor.shutdown();
for (Future< List<FlowRecordHandler> > f : result.values()) {
try {
for (FlowRecordHandler handler : f.get()) {
try {
handler.handleResult();
} catch (Exception ex) {
Logger.getLogger(ConcurrentFlowSourceSetHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (InterruptedException ex) {
Logger.getLogger(ConcurrentFlowSourceSetHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(ConcurrentFlowSourceSetHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}