public class Agent
extends java.lang.Object
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) . |
Modifier and Type | Field and Description |
---|---|
private java.util.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 java.util.Map<java.lang.String,java.lang.Class<? extends CustomRequestHandler>> |
customRequestHandlerClasses |
static int |
DEFAULT_CHECK_INTERVAL
The default check interval in seconds.
|
static java.lang.Class<? extends JobExecutor> |
DEFAULT_JOB_EXECUTOR
The default job executor to use if none has been specified
in the configuration file.
|
static java.lang.Class<? extends SlotManager> |
DEFAULT_SLOT_MANAGER
The default slot manager to use if none has been specified
in the configuration file.
|
private java.lang.String |
description |
private java.lang.Class<? extends JobExecutor> |
executorClass |
private java.lang.String |
externalId |
private boolean |
isRunning |
private JobExecutor |
jobExecutor |
private java.util.TimerTask |
jobQueueChecker |
private static org.slf4j.Logger |
log
Log job agent events.
|
private java.lang.String |
login |
private static org.slf4j.Logger |
logServer
Log job agent server events.
|
private java.lang.String |
name |
private java.lang.String |
password |
private java.lang.Integer |
port |
private java.util.Map<Job.ExecutionTime,java.lang.Integer> |
priorities
The thread priority to use when executing jobs in each slot.
|
private java.util.Properties |
properties |
private java.util.Set<java.net.InetAddress> |
remote |
private MultiProtocolRequestHandler |
requestHandler |
private java.lang.ThreadGroup |
runnersGroup
The group were all job runners are placed.
|
private SessionControl |
sc |
private JobAgentServerConnection |
server |
private java.net.InetAddress |
serverAddress |
private AgentSignalReceiver |
signalReceiver |
private SlotManager |
slotManager |
private java.lang.Class<? extends SlotManager> |
slotManagerClass |
Constructor and Description |
---|
Agent(java.util.Properties properties)
Create a new job agent.
|
Modifier and Type | Method and Description |
---|---|
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 java.util.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 java.util.Map<java.lang.String,java.lang.Class<? extends CustomRequestHandler>> |
getCustomRequestHandlerClasses() |
java.lang.String |
getDescription()
Get the
agent.description configuration value. |
java.lang.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 java.lang.Class<? extends JobExecutor> |
getJobExecutorClass(java.lang.String className)
Get the class object for the configured job executor.
|
java.lang.String |
getLogin()
Get the
agent.user configuration value. |
java.lang.String |
getName()
Get the
agent.name configuration value. |
java.lang.String |
getPassword()
Get the
agent.password configuration value. |
int |
getPort()
Get the
agent.port configuration value. |
java.lang.String |
getProperty(java.lang.String key)
Get a configuration property.
|
private java.util.Set<java.net.InetAddress> |
getRemoteAddresses(java.lang.String config)
Split the string at commas and try to create an
InetAddress
for each part. |
java.util.Set<JobInfo> |
getRunningJobs()
Get a set containing the ID:s of the jobs that are currently
beeing executed by this job agent.
|
java.lang.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 java.lang.Class<? extends SlotManager> |
getSlotManagerClass(java.lang.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(java.net.InetAddress remote,
java.lang.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,
java.lang.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(java.lang.String... protocols)
Unregister one or more protocols.
|
private void |
validateConfiguration()
Validate that all required configuration parameters have been specified.
|
public static final java.lang.Class<? extends JobExecutor> DEFAULT_JOB_EXECUTOR
public static final java.lang.Class<? extends SlotManager> DEFAULT_SLOT_MANAGER
public static final int DEFAULT_CHECK_INTERVAL
private static final org.slf4j.Logger log
private static final org.slf4j.Logger logServer
private final java.util.Properties properties
private final java.lang.String login
private final java.lang.String password
private final java.lang.String externalId
private final java.lang.String name
private final java.lang.String description
private final java.lang.Integer port
private final java.util.Set<java.net.InetAddress> remote
private final boolean allowRemoteStop
private final boolean allowRemotePause
private final boolean allowRemoteStart
private final java.util.Map<java.lang.String,java.lang.Class<? extends CustomRequestHandler>> customRequestHandlerClasses
private final long checkInterval
private final java.lang.Class<? extends JobExecutor> executorClass
private final java.lang.Class<? extends SlotManager> slotManagerClass
private java.net.InetAddress serverAddress
private JobAgentServerConnection server
private MultiProtocolRequestHandler requestHandler
private AgentSignalReceiver signalReceiver
private JobExecutor jobExecutor
private SlotManager slotManager
private java.util.TimerTask jobQueueChecker
private boolean isRunning
private SessionControl sc
private final java.util.Map<Job.ExecutionTime,java.lang.Integer> priorities
private final java.util.Set<JobInfo> activeJobs
private int closeTimeout
private final java.lang.ThreadGroup runnersGroup
public Agent(java.util.Properties properties)
properties
- A properties object containing the configuration
parameters for the agentpublic java.lang.String getProperty(java.lang.String key)
key
- The keypublic java.lang.String getId()
agent.id
configuration value.public java.lang.String getLogin()
agent.user
configuration value.public java.lang.String getPassword()
agent.password
configuration value.public java.lang.String getName()
agent.name
configuration value.public java.lang.String getDescription()
agent.description
configuration value.public int getPort()
agent.port
configuration value.public int getThreadPriority(Job.ExecutionTime slot)
slot
- public java.lang.String getServerName()
SocketUtil.getPublicLocalHost()
private java.util.Set<java.net.InetAddress> getRemoteAddresses(java.lang.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 splitInetAdress
objectprivate java.util.Map<java.lang.String,java.lang.Class<? extends CustomRequestHandler>> getCustomRequestHandlerClasses()
private void registerCustomRequestHandlers(MultiProtocolRequestHandler master)
private java.lang.Class<? extends JobExecutor> getJobExecutorClass(java.lang.String className)
JobExecutor
interface a warning message is logged and
the DEFAULT_JOB_EXECUTOR
is used instead.className
- The name of the job executor classprivate java.lang.Class<? extends SlotManager> getSlotManagerClass(java.lang.String className)
SlotManager
interface a warning message is logged and
the DEFAULT_SLOT_MANAGER
is used instead.className
- The name of the slot manager classprivate void validateConfiguration() throws InvalidDataException
InvalidDataException
- If parameters are missing or have incorrect valuespublic void service(RequestHandler defaultHandler) throws java.io.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
java.io.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.public boolean isRunning()
public java.util.Set<JobInfo> getRunningJobs()
public boolean isAllowedControl(java.net.InetAddress remote, java.lang.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 executepublic void registerRequestHandler(RequestHandler handler, java.lang.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(java.lang.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 forpublic 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 accesSlot 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 slotpublic 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 java.util.TimerTask createJobQueueChecker()
Application.getScheduler()
,
JobQueueChecker
private void closeJobQueueChecker()
private JobExecutor createJobExecutor()
private void closeJobExecutor()
private SlotManager createSlotManager()
private void closeSlotManager()
private void maybeStopRunningJobs()