final Object parameterObject) {
Set<String> keys = new HashSet<String>();
keys.add(getDefaultDataSourceName());
keys.addAll(getCobarDataSourceService().getDataSources().keySet());
final CobarMRBase mrbase = new CobarMRBase(keys);
ExecutorService executor = createCustomExecutorService(Runtime.getRuntime()
.availableProcessors(), "batchInsertAfterReordering");
try {
final StringBuffer exceptionStaktrace = new StringBuffer();
Collection<?> paramCollection = ((BatchInsertTask) parameterObject).getEntities();
final CountDownLatch latch = new CountDownLatch(paramCollection.size());
Iterator<?> iter = paramCollection.iterator();
while (iter.hasNext()) {
final Object entity = iter.next();
Runnable task = new Runnable() {
public void run() {
try {
SortedMap<String, DataSource> dsMap = lookupDataSourcesByRouter(
statementName, entity);
if (MapUtils.isEmpty(dsMap)) {
logger
.info(
"can't find routing rule for {} with parameter {}, so use default data source for it.",
statementName, entity);
mrbase.emit(getDefaultDataSourceName(), entity);
} else {
if (dsMap.size() > 1) {
throw new IllegalArgumentException(
"unexpected routing result, found more than 1 target data source for current entity:"
+ entity);
}
mrbase.emit(dsMap.firstKey(), entity);
}
} catch (Throwable t) {
exceptionStaktrace.append(ExceptionUtils.getFullStackTrace(t));
} finally {
latch.countDown();
}
}
};
executor.execute(task);
}
try {
latch.await();
} catch (InterruptedException e) {
throw new ConcurrencyFailureException(
"unexpected interruption when re-arranging parameter collection into sub-collections ",
e);
}
if (exceptionStaktrace.length() > 0) {
throw new ConcurrencyFailureException(
"unpected exception when re-arranging parameter collection, check previous log for details.\n"
+ exceptionStaktrace);
}
} finally {
executor.shutdown();
}
List<ConcurrentRequest> requests = new ArrayList<ConcurrentRequest>();
for (Map.Entry<String, List<Object>> entity : mrbase.getResources().entrySet()) {
final List<Object> paramList = entity.getValue();
if (CollectionUtils.isEmpty(paramList)) {
continue;
}