jadex.micro.examples.mandelbrot.ServicePoolManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-micro Show documentation
Show all versions of jadex-applications-micro Show documentation
The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.
package jadex.micro.examples.mandelbrot;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import jadex.bridge.ComponentTerminatedException;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.IService;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.clock.IClockService;
import jadex.bridge.service.types.clock.ITimedObject;
import jadex.bridge.service.types.clock.ITimer;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.IResultListener;
import jadex.commons.future.IntermediateFuture;
/**
* Generic class that allows dispatching tasks to a dynamic pool of services.
*/
public class ServicePoolManager
{
//-------- attributes --------
/** The component, which manages the pool. */
protected IInternalAccess component;
/** The services name. */
protected String name;
/** The handler for service creation, selection and invocation. */
protected IServicePoolHandler handler;
/** The maximum number of services (-1 for unlimited). */
protected int max;
/** The collection of free services (id->service). */
protected Map free;
/** The collection of busy services (id->service). */
protected Map busy;
/** The open tasks with their corresponding allocation data. */
protected Map tasks;
/** Flag to indicate an ongoing search. */
protected boolean searching;
/** Flag to indicate an ongoing creation. */
protected boolean creating;
/** The search timeout timer. */
protected ITimer timer;
//-------- constructors --------
/**
* Create a new service pool manager.
* @param name The services name.
* @param handler The code for service invocation.
*/
public ServicePoolManager(IInternalAccess component, String name, IServicePoolHandler handler, int max)
{
this.component = component;
this.name = name;
this.handler = handler;
this.max = max;
this.free = new HashMap();
this.busy = new HashMap();
this.tasks = new HashMap();
}
//-------- methods --------
/**
* Perform the given tasks using available or newly created services.
* @param tasks The set of tasks to be performed.
* @param retry True, when failed tasks should be retried.
* @param user User data that is provided for service selection, creation, invocation (if any).
* @return A future with intermediate and final results.
*/
public IIntermediateFuture performTasks(Set tasks, boolean retry, Object user)
{
// System.out.println("Peforming "+tasks.size()+" tasks");
// System.out.println("Performing tasks: busy="+busy.size()+", free="+free.size());
// Allocation data binds tasks together to a single result future.
AllocationData ad = new AllocationData(tasks.size(), retry, user);
boolean allassigned = true;
for(Iterator it=tasks.iterator(); it.hasNext(); )
{
boolean assigned = retryTask(it.next(), ad);
allassigned = allassigned && assigned;
}
// Search for new services if not all tasks could be assigned.
if(!allassigned && busy.size()+free.size()()
{
public IFuture execute(IInternalAccess ia)
{
timer = null;
// Create new services when there are remaining tasks.
createServices();
return IFuture.DONE;
}
public String toString()
{
return "Search timeout for: "+name;
}
});
}
});
}
}
public void exceptionOccurred(Exception exception)
{
// No timeout supported; ignore
}
});
}
// Find new available service(s).
if(!searching)
{
// System.out.println("searching services");
searching = true;
// System.out.println("wurksn0");
component.getComponentFeature(IRequiredServicesFeature.class).getRequiredServices(name).addResultListener(
new IIntermediateResultListener