|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.clients.jobagent.Agent
public class Agent
This is the actual job agent application. It delegates the actual
checking of the job queue to the JobQueueChecker
class.
It includes a listener service that can be used for remote control
of the agent. It is for example possible to send start
,
stop
and pause
requests.
This class is responsible for creating a JobExecutor
object and to delegate the actual execution of a job to it. The agent keep
track of the running jobs and makes sure that only the configured
number of jobs are running at the same time.
The agent is configured at construction time with parameters from
a Properties
object.
Parameter | Default value | Description |
---|---|---|
agent.user | - | The username to use for logging in to BASE (required) |
agent.password | - | The password to use for logging in to BASE (required) |
agent.id | - |
The external ID (required), name and description properties of the
corresponding JobAgent item in the BASE database
|
agent.name | - | |
agent.description | - | |
agent.port | 47822 | The port the remote control service listener is listening to |
agent.remotecontrol | - | A comma-separated list of computer that are allowed remote control. It is recommended that the web server is put in this list. The local host is always allowed control and doesn't have to be in this list. |
agent.allowremote.stop | false |
If stop requests should be allowed from remote hosts or not.
Note! A stop request shuts down the agent making it impossible to start it
again using remote control.
|
agent.allowremote.start | true | If start requests should be allowed from remote hosts or not |
agent.allowremote.pause | true | If pause requests should be allowed from remote hosts or not |
agent.request-handler.* | - |
One or more entries that can be used to register custom remote control request handlers.
The * should be replaced with the name of the protocol which means that requests
should take the form: protocol://custom-data....
The value is the name of a class that implements the |
agent.executor.class | ProcessJobExecutor |
The name of a class that is responsible for starting the a job once
the agent has determined that is allowed to be exected. The class must
implement the JobExecutor interface and provide a public noargument
constructor. Note that only one instance of this class exists for an agent.
It must be thread-safe since the jobs are executed in parallel threads.
|
agent.checkinterval | 30 | Number of seconds between checks to the database for new jobs. |
agent.shortest.slots | 1 | Number of slots to reserve for jobs that take < 1 minute to execute |
agent.shortest.priority | 4 | The thread priority of jobs in this slot. See Thread.setPriority(int) . |
agent.short.slots | 1 | Number of slots to reserve for jobs that take < 10 minute to execute |
agent.short.priority | 4 | The thread priority of jobs in this slot. See Thread.setPriority(int) . |
agent.medium.slots | 2 | Number of slots to reserve for jobs that take < 1 hour to execute |
agent.medium.priority | 3 | The thread priority of jobs in this slot. See Thread.setPriority(int) . |
agent.long.slots | 2 | Number of slots to reserve for jobs that take > 1 hour to execute |
agent.long.priority | 3 | The thread priority of jobs in this slot. See Thread.setPriority(int) . |
Field Summary | |
---|---|
private Set<JobInfo> |
activeJobs
|
private boolean |
allowRemotePause
|
private boolean |
allowRemoteStart
|
private boolean |
allowRemoteStop
|
private long |
checkInterval
|
private int |
closeTimeout
Timeout to wait for jobs to act on the ABORT signal when stopping. |
private Map<String,Class<? extends CustomRequestHandler>> |
customRequestHandlerClasses
|
static int |
DEFAULT_CHECK_INTERVAL
The default check interval in seconds. |
static Class<? extends JobExecutor> |
DEFAULT_JOB_EXECUTOR
The default job executor to use if none has been specified in the configuration file. |
static Class<? extends SlotManager> |
DEFAULT_SLOT_MANAGER
The default slot manager to use if none has been specified in the configuration file. |
private String |
description
|
private Class<? extends JobExecutor> |
executorClass
|
private String |
externalId
|
private boolean |
isRunning
|
private JobExecutor |
jobExecutor
|
private TimerTask |
jobQueueChecker
|
private static Logger |
log
Log job agent events. |
private String |
login
|
private static Logger |
logServer
Log job agent server events. |
private String |
name
|
private String |
password
|
private Integer |
port
|
private Map<Job.ExecutionTime,Integer> |
priorities
The thread priority to use when executing jobs in each slot. |
private Properties |
properties
|
private Set<InetAddress> |
remote
|
private MultiProtocolRequestHandler |
requestHandler
|
private ThreadGroup |
runnersGroup
The group were all job runners are placed. |
private SessionControl |
sc
|
private JobAgentServerConnection |
server
|
private InetAddress |
serverAddress
|
private AgentSignalReceiver |
signalReceiver
|
private SlotManager |
slotManager
|
private Class<? extends SlotManager> |
slotManagerClass
|
Constructor Summary | |
---|---|
Agent(Properties properties)
Create a new job agent. |
Method Summary | |
---|---|
private void |
closeJobExecutor()
Close the job executor. |
private void |
closeJobQueueChecker()
Close the job queue checker. |
private void |
closeServer()
Close the service listener. |
private void |
closeSlotManager()
Close the slot manager. |
private JobExecutor |
createJobExecutor()
Create a job executor and initialise it. |
private TimerTask |
createJobQueueChecker()
Create a job queue checker. and register it with the BASE core scheduler. |
private SlotManager |
createSlotManager()
Create a slot manager and initialize it. |
private Map<String,Class<? extends CustomRequestHandler>> |
getCustomRequestHandlerClasses()
|
String |
getDescription()
Get the agent.description configuration value. |
String |
getId()
Get the agent.id configuration value. |
SessionControl |
getImpersonatedSessionControl(Job job)
Get a session control where the owner of the job has been impersonated and the active project has been set if needed. |
JobAgent |
getJobAgent(DbControl dc)
Get the JobAgent item corresponding to this agent. |
private Class<? extends JobExecutor> |
getJobExecutorClass(String className)
Get the class object for the configured job executor. |
String |
getLogin()
Get the agent.user configuration value. |
String |
getName()
Get the agent.name configuration value. |
String |
getPassword()
Get the agent.password configuration value. |
int |
getPort()
Get the agent.port configuration value. |
String |
getProperty(String key)
Get a configuration property. |
private Set<InetAddress> |
getRemoteAddresses(String config)
Split the string at commas and try to create an InetAddress
for each part. |
Set<JobInfo> |
getRunningJobs()
Get a set containing the ID:s of the jobs that are currently beeing executed by this job agent. |
String |
getServerName()
Get the host name of the server where the job agent is running. |
SessionControl |
getSessionControl()
Get a session control with the configured user logged in. |
AgentSignalReceiver |
getSignalReceiver()
Get the signal receiver that is processing signal messages on behalf of this job agent. |
(package private) Slot |
getSlot(Job job)
Find a free slot for executing a job. |
private Class<? extends SlotManager> |
getSlotManagerClass(String className)
Get the class object for the configured slot manager. |
int |
getThreadPriority(Job.ExecutionTime slot)
Get the thread priority to use for the specified execution time slot. |
boolean |
isAllowedControl(InetAddress remote,
String cmd)
Check if the computer specified by the given address is allowed to control this job agent. |
boolean |
isRunning()
Check if the job agent is running or not. |
(package private) void |
jobDone(Job job,
Slot usedSlot)
Used by JobRunner to tell that a job has finished executing and
that the used slot should be released. |
private void |
maybeStopRunningJobs()
Try to stop running jobs by interrupting the threads they are executing in. |
void |
pause()
Pause the job agent. |
private void |
registerCustomRequestHandlers(MultiProtocolRequestHandler master)
|
void |
registerRequestHandler(RequestHandler handler,
String... protocols)
Register a request handler for one or more protocols. |
void |
service(RequestHandler defaultHandler)
Start the listener service that listens for control commands such as start , stop , pause and
info . |
void |
start()
Start the job agent. |
void |
startJob(Job job,
JobAgentSettings settings)
Start a job. |
void |
stop()
Stop the job agent. |
void |
unregisterRequestHandler(String... protocols)
Unregister one or more protocols. |
private void |
validateConfiguration()
Validate that all required configuration parameters have been specified. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Class<? extends JobExecutor> DEFAULT_JOB_EXECUTOR
public static final Class<? extends SlotManager> DEFAULT_SLOT_MANAGER
public static final int DEFAULT_CHECK_INTERVAL
private static final Logger log
private static final Logger logServer
private final Properties properties
private final String login
private final String password
private final String externalId
private final String name
private final String description
private final Integer port
private final Set<InetAddress> remote
private final boolean allowRemoteStop
private final boolean allowRemotePause
private final boolean allowRemoteStart
private final Map<String,Class<? extends CustomRequestHandler>> customRequestHandlerClasses
private final long checkInterval
private final Class<? extends JobExecutor> executorClass
private final Class<? extends SlotManager> slotManagerClass
private InetAddress serverAddress
private JobAgentServerConnection server
private MultiProtocolRequestHandler requestHandler
private AgentSignalReceiver signalReceiver
private JobExecutor jobExecutor
private SlotManager slotManager
private TimerTask jobQueueChecker
private boolean isRunning
private SessionControl sc
private final Map<Job.ExecutionTime,Integer> priorities
private final Set<JobInfo> activeJobs
private int closeTimeout
private final ThreadGroup runnersGroup
Constructor Detail |
---|
public Agent(Properties properties)
properties
- A properties object containing the configuration
parameters for the agentMethod Detail |
---|
public String getProperty(String key)
key
- The key
public String getId()
agent.id
configuration value.
public String getLogin()
agent.user
configuration value.
public String getPassword()
agent.password
configuration value.
public String getName()
agent.name
configuration value.
public String getDescription()
agent.description
configuration value.
public int getPort()
agent.port
configuration value.
public int getThreadPriority(Job.ExecutionTime slot)
slot
-
public String getServerName()
SocketUtil.getPublicLocalHost()
private Set<InetAddress> getRemoteAddresses(String config)
InetAddress
for each part. Parts that turn out to be invalid are skipped and
a warning message is logged.
config
- The string to split
InetAdress
objectprivate Map<String,Class<? extends CustomRequestHandler>> getCustomRequestHandlerClasses()
private void registerCustomRequestHandlers(MultiProtocolRequestHandler master)
private Class<? extends JobExecutor> getJobExecutorClass(String className)
JobExecutor
interface a warning message is logged and
the DEFAULT_JOB_EXECUTOR
is used instead.
className
- The name of the job executor class
private Class<? extends SlotManager> getSlotManagerClass(String className)
SlotManager
interface a warning message is logged and
the DEFAULT_SLOT_MANAGER
is used instead.
className
- The name of the slot manager class
private void validateConfiguration() throws InvalidDataException
InvalidDataException
- If parameters are missing or have incorrect valuespublic void service(RequestHandler defaultHandler) throws IOException
start
, stop
, pause
and
info
. The listener service is only required if the job
agent needs to respond to remote control commands.
Note! The AgentController
which is the default controller for
agents always uses the listener service for communicating with the job
agent.
Note! The listener service is started in a separate thread and this method returns as soon as the network connections are set up.
Note! The default handler supplied as an argument is used as a fallback
handler for unregistered protocols. Additional request handlers can be
set up by calling registerRequestHandler(RequestHandler, String...)
.
defaultHandler
- A RequestHandler
that handles the
incoming requsts, or null to use the DefaultRequestHandler
IOException
- If there is an error when starting the servicepublic void start()
JobQueueChecker
object with the BASE core scheduler Application.getScheduler()
. The
timer will call the JobQueueChecker.run()
method at intervals
specified by the agent.checkinterval
configuration settings.
Note! The JobQueueChecker
will run in a separate thread
and this method return immediately after registering the object with the
scheduler.
Note! This method also creats a single instance of a JobExecutor
.
The actual class to use is specified by the agent.jobexecutor.class
configuration setting. The default job executor is ProcessJobExecutor
.
public void stop()
JobQueueChecker
that was registered with the BASE
core scheduler by the start()
method.
JobExecutor.close()
method on the job executor
created by the start()
method.
service(RequestHandler)
method.
Thread.interrupt()
on all job threads.
public void pause()
JobQueueChecker
that was registered with the BASE
core scheduler by the start()
method.
JobExecutor.close()
method on the job executor
created by the start()
method.
Application
.
start()
again will start the job
agent again.
stop()
,
start()
public boolean isRunning()
public Set<JobInfo> getRunningJobs()
public boolean isAllowedControl(InetAddress remote, String cmd)
agent.remotecontrol
property. This method is called from
the DefaultRequestHandler.handleCmd(java.net.Socket, String)
method
to determine if a service request should be accepted or not.
Note! The stop
, start
and pause
commands are only allowed from the local host unless the agent.allowremote.stop
,
agent.allowremote.start
and agent.allowremote.pause
are set to a true values.
Note! The local host doesn't have to be listed in the agent.remotecontrol
property. Requests are always allowed from the local host.
remote
- The address to the remote computercmd
- The command the remote computer wants to execute
public void registerRequestHandler(RequestHandler handler, String... protocols)
MultiProtocolRequestHandler
for more information.
Calls to this method is ignored if the job agent has no listener
service. See service(RequestHandler)
.
handler
- The request handlerprotocols
- The name of the protocols the handler should handlepublic void unregisterRequestHandler(String... protocols)
MultiProtocolRequestHandler
for more information.
Calls to this method is ignored if the job agent has no listener
service. See service(RequestHandler)
.
protocols
- The name of the protocols the handler should handlepublic AgentSignalReceiver getSignalReceiver()
public SessionControl getSessionControl()
public SessionControl getImpersonatedSessionControl(Job job)
job
- The job to get the impersonated session control for
public JobAgent getJobAgent(DbControl dc)
JobAgent
item corresponding to this agent. The
job agent is looked up by the JobAgent.getByExternalId(DbControl, String)
method where the external ID is given by the agent.id
property.
dc
- The DbControl to use for database acces
Slot getSlot(Job job)
Note! This method reserves the slot for the job. It is important that
the jobDone(Job, Slot)
method is called once
the job has completed to return the slot to the pool. Failure to do
so may result in that the agent thinks that all slots are
used when they are not.
job
- The job that wants a slot
public void startJob(Job job, JobAgentSettings settings)
job
- The job to startsettings
- Settings for the job agent to use.void jobDone(Job job, Slot usedSlot)
JobRunner
to tell that a job has finished executing and
that the used slot should be released.
job
- The job that has finishedusedSlot
- The slot that was usedprivate void closeServer()
private TimerTask createJobQueueChecker()
Application.getScheduler()
,
JobQueueChecker
private void closeJobQueueChecker()
private JobExecutor createJobExecutor()
private void closeJobExecutor()
private SlotManager createSlotManager()
private void closeSlotManager()
private void maybeStopRunningJobs()
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |