All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.hajdbc.invocation.AllResultsCollector Maven / Gradle / Ivy

There is a newer version: 3.6.61
Show newest version
/*
 * HA-JDBC: High-Availability JDBC
 * Copyright (C) 2012  Paul Ferraro
 *
 * This program 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
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see .
 */
package net.sf.hajdbc.invocation;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.AbstractMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import net.sf.hajdbc.Database;
import net.sf.hajdbc.DatabaseCluster;
import net.sf.hajdbc.ExceptionFactory;
import net.sf.hajdbc.Messages;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
import net.sf.hajdbc.sql.ProxyFactory;
import net.sf.hajdbc.util.Tracer;

/**
 * @author Paul Ferraro
 */
public class AllResultsCollector implements InvokeOnManyInvocationStrategy.ResultsCollector
{
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	public static interface ExecutorProvider
	{
		> ExecutorService getExecutor(DatabaseCluster cluster);
	}
	
	private final ExecutorProvider provider;
	
	public AllResultsCollector(ExecutorProvider provider)
	{
		this.provider = provider;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public , T, R, E extends Exception> Map.Entry, SortedMap> collectResults(ProxyFactory factory, final Invoker invoker)
	{
		DatabaseCluster cluster = factory.getDatabaseCluster();
		ExceptionFactory exceptionFactory = factory.getExceptionFactory();
		Set databaseSet = cluster.getBalancer();
		if(Tracer.invoke.isTrace()) {
			logger.log(Level.INFO, "databaseSet={0}", databaseSet);
		}
		if (databaseSet.isEmpty())
		{
			exceptionFactory.createException(Messages.NO_ACTIVE_DATABASES.getMessage(cluster));
		}

		int size = databaseSet.size();
		
		List> invocationList = new ArrayList>(size);
		
		for (D database: databaseSet)
		{
			invocationList.add(new Invocation(invoker, database, factory.get(database)));
		}
		
		try
		{
			List> futureList = this.provider.getExecutor(cluster).invokeAll(invocationList);
			
			final SortedMap resultMap = new TreeMap();
			final SortedMap exceptionMap = new TreeMap();
			
			for (int i = 0; i < invocationList.size(); ++i)
			{
				D database = invocationList.get(i).getDatabase();
				
				try
				{
					resultMap.put(database, futureList.get(i).get());
				}
				catch (ExecutionException e)
				{
					if(Tracer.invoke.isTrace()) {
						logger.log(Level.INFO, e);
					}
					// If this database was concurrently deactivated, just ignore the failure
					if (databaseSet.contains(database))
					{
						exceptionMap.put(database, exceptionFactory.createException(e.getCause()));
					}
				}
				catch (InterruptedException e)
				{
					Thread.currentThread().interrupt();
					
					exceptionMap.put(database, exceptionFactory.createException(e));
				}
			}
		
			return new AbstractMap.SimpleImmutableEntry, SortedMap>(resultMap, exceptionMap);
		}
		catch (InterruptedException e)
		{
			throw new IllegalStateException(e);
		}
	}
	
	private static class Invocation, T, R, E extends Exception> implements Callable
	{
		private final Invoker invoker;
		private final D database;
		private final T object;
		
		Invocation(Invoker invoker, D database, T object)
		{
			this.invoker = invoker;
			this.database = database;
			this.object = object;
		}
		
		D getDatabase()
		{
			return this.database;
		}
		
		/**
		 * {@inheritDoc}
		 * @see java.util.concurrent.Callable#call()
		 */
		@Override
		public R call() throws E
		{
			return this.invoker.invoke(this.database, this.object);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy