public abstract class XCommand<T> extends Object implements XCallable<T>
It enables by API the following pattern:
It has built in instrumentation and logging.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_LOCK_TIMEOUT |
static String |
DEFAULT_REQUEUE_DELAY |
protected boolean |
dryrun |
protected static EventHandlerService |
eventService |
protected Instrumentation |
instrumentation |
static String |
INSTRUMENTATION_GROUP |
XLog |
LOG |
Constructor and Description |
---|
XCommand(String name,
String type,
int priority)
Create a command.
|
XCommand(String name,
String type,
int priority,
boolean dryrun) |
Modifier and Type | Method and Description |
---|---|
T |
call()
Implements the XCommand life-cycle.
|
protected void |
eagerLoadState()
Load the necessary state to perform an eager precondition check.
|
protected void |
eagerVerifyPrecondition()
Verify the precondition for the command before obtaining a lock.
|
protected abstract T |
execute()
Command execution body.
|
protected void |
executeInterrupts()
Check for the existence of interrupts for the same lock key
Execute them if exist.
|
long |
getCreatedTime()
Returns the creation time of the command.
|
abstract String |
getEntityKey()
Return the entity key for the command.
|
protected Instrumentation |
getInstrumentation()
Return the
Instrumentation instance in use. |
String |
getKey()
Get command key
|
protected long |
getLockTimeOut()
Return the time out when acquiring a lock.
|
XLog |
getLog()
Get XLog log
|
String |
getName()
Return the command name.
|
int |
getPriority()
Return the priority of the command.
|
protected long |
getRequeueDelay()
Return the delay time for requeue
|
String |
getType()
Return the callable type.
|
boolean |
inInterruptMode() |
protected abstract boolean |
isLockRequired()
Indicate if the the command requires locking.
|
protected boolean |
isReQueueRequired()
Indicate if the the command requires to requeue itself if the lock is not acquired.
|
protected abstract void |
loadState()
Load the necessary state to perform the precondition check and to execute the command.
|
protected void |
queue(XCommand<?> command)
Queue a command for execution after the current command execution completes.
|
protected void |
queue(XCommand<?> command,
long msDelay)
Queue a command for delayed execution after the current command execution completes.
|
void |
resetUsed()
Set false to the used
|
void |
setInterruptMode(boolean mode)
set the mode of execution for the callable.
|
protected void |
setLogInfo()
Set the thread local logInfo with the context of this command and reset log prefix.
|
String |
toString()
String for the command - key
|
protected abstract void |
verifyPrecondition()
Verify the precondition for the command after a lock has been obtain, just before executing the command.
|
public static final String DEFAULT_LOCK_TIMEOUT
public static final String INSTRUMENTATION_GROUP
public static final String DEFAULT_REQUEUE_DELAY
protected boolean dryrun
protected Instrumentation instrumentation
protected static EventHandlerService eventService
public XCommand(String name, String type, int priority)
name
- command name.type
- command type.priority
- command priority.protected void setLogInfo()
public String getType()
The command type is used for concurrency throttling in the CallableQueueService
.
public int getPriority()
getPriority
in interface XCallable<T>
public long getCreatedTime()
getCreatedTime
in interface XCallable<T>
protected void queue(XCommand<?> command)
All commands queued during the execution of the current command will be queued for a single serial execution.
If the command execution throws an exception, no command will be effectively queued.
command
- command to queue.protected void queue(XCommand<?> command, long msDelay)
All commands queued during the execution of the current command with the same delay will be queued for a single serial execution.
If the command execution throws an exception, no command will be effectively queued.
command
- command to queue.msDelay
- delay in milliseconds.public final T call() throws CommandException
call
in interface Callable<T>
CommandException
- thrown if the command could not be executed.protected void executeInterrupts()
protected long getLockTimeOut()
The value is loaded from the Oozie configuration, the property {link #DEFAULT_LOCK_TIMEOUT}.
Subclasses should override this method if they want to use a different time out.
protected abstract boolean isLockRequired()
Subclasses should override this method if they require locking.
true/false
public abstract String getEntityKey()
getEntityKey
in interface XCallable<T>
protected boolean isReQueueRequired()
Subclasses should override this method if they don't want to requeue.
Default is true.
true/false
protected void eagerLoadState() throws CommandException
This implementation does a NOP.
Subclasses should override this method and load the state needed to do an eager precondition check.
A trivial implementation is calling {link #loadState}.
CommandException
protected void eagerVerifyPrecondition() throws CommandException, PreconditionException
This implementation does a NOP.
A trivial implementation is calling {link #verifyPrecondition}.
CommandException
- thrown if the precondition is not met.PreconditionException
protected abstract void loadState() throws CommandException
Subclasses must implement this method and load the state needed to do the precondition check and execute the command.
CommandException
protected abstract void verifyPrecondition() throws CommandException, PreconditionException
CommandException
- thrown if the precondition is not met.PreconditionException
protected abstract T execute() throws CommandException
This method will be invoked after the {link #loadState} and {link #verifyPrecondition} methods.
If the command requires locking, this method will be invoked ONLY if the lock has been acquired.
CommandException
- thrown if the command execution failed.protected Instrumentation getInstrumentation()
Instrumentation
instance in use.Instrumentation
instance in use.public void resetUsed()
protected long getRequeueDelay()
The value is loaded from the Oozie configuration, the property {link #DEFAULT_REQUEUE_DELAY}.
Subclasses should override this method if they want to use a different requeue delay time
public void setInterruptMode(boolean mode)
setInterruptMode
in interface XCallable<T>
mode
- the mode to setpublic boolean inInterruptMode()
inInterruptMode
in interface XCallable<T>
Copyright © 2018 Apache Software Foundation. All rights reserved.