org.omnaest.utils.operation.battery
Class OperationBattery<RESULT,PARAMETER>

java.lang.Object
  extended by org.omnaest.utils.operation.battery.OperationBattery<RESULT,PARAMETER>
Type Parameters:
RESULT -
PARAMETER -
All Implemented Interfaces:
Operation<RESULT,PARAMETER>
Direct Known Subclasses:
OperationBatteryRoundRobin

public abstract class OperationBattery<RESULT,PARAMETER>
extends Object
implements Operation<RESULT,PARAMETER>

An OperationBattery allows to use a single operation facade to access a pool of object instances which offers one and the same method in a multithreaded environment.

The underlying operation instances will be resolved by a single OperationFactory initially.
These instances do not have to be thread safe, if setUsingReentrantLock(boolean) is set to true. In that case the OperationBattery will decorate calls to the instances with a ReentrantLock, so that in the case there are more invocations than unused instances available, that the incoming invocations will be blocked until an instance of an underlying Operation gets unlocked. This ensures the thread safetyness of the OperationBattery.

An OperationBattery should be given an initial capacity of instances in that way, that the required throughput can be achieved by the throughput of a single Operation instance multiplied with the capacity number. E.g. if a single Operation can handle 100 requests per second and 1000 requests per seconds is the requirement, an OperationBattery with a capacity of 10 instances should narrow down the required throughput very well.

But be aware that the scaling behavior is drained rapidly, if a single Operation.execute(Object) invocation is much faster, than the internal distribution algorithm. So the scaling will be much better for Operations with an invocation duration of at least 1 millisecond or more.

Author:
Omnaest
See Also:
DEFAULT_INITIAL_BATTERY_CAPACITY

Field Summary
static int DEFAULT_INITIAL_BATTERY_CAPACITY
           
 
Constructor Summary
OperationBattery(OperationFactory<RESULT,PARAMETER> operationFactory, boolean usingReentrantLock)
           
OperationBattery(OperationFactory<RESULT,PARAMETER> operationFactory, boolean usingReentrantLock, int initialBatteryCapacity)
           
 
Method Summary
protected abstract  void initializeOperationBattery(int initialBatteryCapacity)
          Initialize the OperationBattery before the constructor call ends.
 boolean isUsingReentrantLock()
          If this returns true, the OperationBattery will ensure thread safetyness using a ReentrantLock on Operation instances.
protected  Operation<RESULT,PARAMETER> resolveNewOperationInstanceFromOperationFactory()
           
 void setOperationFactory(OperationFactory<RESULT,PARAMETER> operationFactory)
           
 void setUsingReentrantLock(boolean usingReentrantLock)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.omnaest.utils.operation.Operation
execute
 

Field Detail

DEFAULT_INITIAL_BATTERY_CAPACITY

public static final int DEFAULT_INITIAL_BATTERY_CAPACITY
See Also:
Constant Field Values
Constructor Detail

OperationBattery

public OperationBattery(OperationFactory<RESULT,PARAMETER> operationFactory,
                        boolean usingReentrantLock,
                        int initialBatteryCapacity)
Parameters:
operationFactory -
usingReentrantLock -
See Also:
OperationBattery

OperationBattery

public OperationBattery(OperationFactory<RESULT,PARAMETER> operationFactory,
                        boolean usingReentrantLock)
Parameters:
operationFactory -
usingReentrantLock -
See Also:
OperationBattery
Method Detail

initializeOperationBattery

protected abstract void initializeOperationBattery(int initialBatteryCapacity)
Initialize the OperationBattery before the constructor call ends. This should be used to for example fill a container with a given amount of Operation instances resolved by the resolveNewOperationInstanceFromOperationFactory() method.

Parameters:
initialBatteryCapacity -

resolveNewOperationInstanceFromOperationFactory

protected Operation<RESULT,PARAMETER> resolveNewOperationInstanceFromOperationFactory()
Returns:

setOperationFactory

public void setOperationFactory(OperationFactory<RESULT,PARAMETER> operationFactory)
Parameters:
operationFactory -
See Also:
OperationFactory

isUsingReentrantLock

public boolean isUsingReentrantLock()
If this returns true, the OperationBattery will ensure thread safetyness using a ReentrantLock on Operation instances.

Returns:
See Also:
ReentrantLock

setUsingReentrantLock

public void setUsingReentrantLock(boolean usingReentrantLock)
Parameters:
usingReentrantLock -
See Also:
isUsingReentrantLock()


Copyright © 2013. All Rights Reserved.