com.tenduke.command
Class CommandScheduler

java.lang.Object
  extended by com.tenduke.command.CommandScheduler

public class CommandScheduler
extends java.lang.Object

Top level command scheduler that acts as the main provider for scheduling and executing commands. This class is intended for use from client facing end points and business logical application code that wish to execute and schedule commands.

CommandScheduler takes care of managing resource allocation for invoking and executing Commands. This class also manages the distribution of Commands based on Command execution CommandResult objects.

Distribution is based on using DistributionRequestFactory by calling getDistributionRequestFactory(), AbstractDistributionRequest objects and submitting requests to ExecutionManagers.getConcurrentExecutionManager()'s implementation of ConcurrentExecutionManager.submit(java.util.concurrent.Callable, java.lang.String). Submitting requests for asynchronous calling for ConcurrentExecutionManager is done using getClass().getSimpleName() for the caller name (study configuration of ConcurrentExecutionManager thread pooling).

It is key that Batch (or similar implementation) is used to execute a set of Command objects where execution order must remain same on all nodes in a distributed system. If commands are executed separately there is no guarantee that execution order remains the same for what was a ordered sequence on one node.


Constructor Summary
protected CommandScheduler()
          Prevents a new instance of the CommandScheduler class from being created.
 
Method Summary
protected  void addCommandDistributionResponseFutureHandler(java.util.concurrent.Future<? extends AbstractDistributionRequestResult> commandDistributionResponseFuture)
           Creates a handler for dealing with a future that gives access to a command distribution response once it is available.
protected  void distribute(Command command, CommandExecutionContext commandContext, CommandResult commandResult)
          Distributes a command.
protected  DistributionRequestFactory getDistributionRequestFactory()
           Gets DistributionRequestFactory to use for creating distribution requests.
protected  javax.security.auth.Subject impersonate(javax.security.auth.Subject authenticatedActor, java.lang.String commandsRequiredActorId)
          Impersonate an other Subject if command requires to be executed by an other actor.
static CommandScheduler instance()
          Get singleton instance of CommandScheduler.
 OperationResult invoke(Command command, CommandExecutionContext commandContext)
          Invoke execute for one command.
 OperationResult invoke(java.lang.String commandXml, CommandExecutionContext commandContext)
          Invoke execute for one command.
 java.util.List<OperationResult> invokeAll(java.util.List<Command> commands, CommandExecutionContext commandContext)
          Invoke execution on list of commands.
 java.util.concurrent.Future<OperationResult> invokeAsync(Command command, CommandExecutionContext commandContext)
          Asynchronously invoke execute for one command.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandScheduler

protected CommandScheduler()
Prevents a new instance of the CommandScheduler class from being created.

Method Detail

addCommandDistributionResponseFutureHandler

protected void addCommandDistributionResponseFutureHandler(java.util.concurrent.Future<? extends AbstractDistributionRequestResult> commandDistributionResponseFuture)

Creates a handler for dealing with a future that gives access to a command distribution response once it is available. This method is called for every command instance that is distributed.

Parameters:
commandDistributionResponseFuture - The response future to handle.

distribute

protected void distribute(Command command,
                          CommandExecutionContext commandContext,
                          CommandResult commandResult)
Distributes a command.

Parameters:
command - Command to distribute.
commandContext - The context object from Command execution.
commandResult - CommandResult object.

getDistributionRequestFactory

protected DistributionRequestFactory getDistributionRequestFactory()

Gets DistributionRequestFactory to use for creating distribution requests. Implementation is a call through to DistributionRequestFactories.getDistributionRequestFactory().

Returns:
DistributionRequestFactory to use for creating distribution requests.

impersonate

protected javax.security.auth.Subject impersonate(javax.security.auth.Subject authenticatedActor,
                                                  java.lang.String commandsRequiredActorId)
                                           throws java.lang.SecurityException
Impersonate an other Subject if command requires to be executed by an other actor.

Parameters:
authenticatedActor - The authenticated actor requesting to impersonate an other actor.
commandsRequiredActorId - Actor's id for the actor to impersonate.
Returns:
Subject that command is to be executed as or null if permission for authenticatedActor is not granted.
Throws:
java.lang.SecurityException - Thrown if impersonation fails.

instance

public static CommandScheduler instance()
Get singleton instance of CommandScheduler.

Returns:
singleton instance of CommandScheduler.

invoke

public OperationResult invoke(Command command,
                              CommandExecutionContext commandContext)
                       throws java.lang.SecurityException
Invoke execute for one command.

Parameters:
command - Command to invoke.
commandContext - The context object for Command execution.
Returns:
Commands execution result.
Throws:
java.lang.SecurityException - Thrown if failed to execute the command as the actor required by the command because impersonation fails.

invoke

public OperationResult invoke(java.lang.String commandXml,
                              CommandExecutionContext commandContext)
Invoke execute for one command.

Parameters:
commandXml - Command as XML in XmlSerializer format.
commandContext - The context object for Command execution.
Returns:
Commands execution result.

invokeAll

public java.util.List<OperationResult> invokeAll(java.util.List<Command> commands,
                                                 CommandExecutionContext commandContext)
Invoke execution on list of commands. Note that commands in the list are invoked separately and distirbuted separately. Execution order can not be guaranteed on remote nodes to which commands are distributed to. Use Batch command for cases where execution order needs to remain constant on all nodes in distribution.

Parameters:
commands - The list of Commands to execute.
commandContext - The context object for Command execution.
Returns:
List of commands execution results.

invokeAsync

public java.util.concurrent.Future<OperationResult> invokeAsync(Command command,
                                                                CommandExecutionContext commandContext)
                                                         throws java.lang.SecurityException
Asynchronously invoke execute for one command.

Parameters:
command - Command to invoke.
commandContext - The context object for Command execution.
Returns:
Future for commands execution result.
Throws:
java.lang.SecurityException - Thrown if failed to execute the command as the actor required by the command because impersonation fails.