Package org.gearman.worker
Interface GearmanWorker
-
- All Known Implementing Classes:
GearmanWorkerImpl
public interface GearmanWorker
A Gearman Worker is responsible for executing jobs it receives from the Job Server. A Worker registers with the Job Server the types of jobs that it may execute, the server will use this information, along with other attributes, to determine which Worker will execute a particular job request. As data is generated or as a job's state changes, the worker passes this information back to the Job Server.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description boolean
addServer(GearmanJobServerConnection conn)
Register a new Gearman Job Server with the worker.String
echo(String text, GearmanJobServerConnection conn)
Send an echo request to the specifiedGearmanJobServerConnection
, the Gearman Job Server should echo the contents of the echo request back to the Worker.Set<String>
getRegisteredFunctions()
Retrieve the names of all functions that have been registered with this worker.String
getWorkerID()
Retrieves the ID used by this worker instance.String
getWorkerID(GearmanJobServerConnection conn)
Retrieves the ID this worker uses to identify itself with a specific connection.boolean
hasServer(GearmanJobServerConnection conn)
Has a connection to the specified Gearman Job Server been registered with this worker.boolean
isJobUniqueIdRequired()
Does worker only ask for jobs which contain a Unique Id.boolean
isRunning()
Determine if the worker is currently executing the work loop.void
registerFunction(Class<? extends GearmanFunction> function)
Registers a particularGearmanFunction
with the worker.void
registerFunction(Class<? extends GearmanFunction> function, long timeout)
Registers a particularGearmanFunction
with the worker.void
registerFunctionFactory(GearmanFunctionFactory factory)
Registers aGearmanFunctionFactory
with the worker.void
registerFunctionFactory(GearmanFunctionFactory factory, long timeout)
Registers aGearmanFunctionFactory
with the worker and specifies the timeout associated with any functions generated by this factory.void
setJobUniqueIdRequired(boolean requiresJobUUID)
Should worker only ask for jobs which contain a Unique Id.void
setWorkerID(String id)
Set the ID for a particular worker instance.void
setWorkerID(String id, GearmanJobServerConnection conn)
Set the ID used by a particular Gearman Job Server to identify a particular Worker.List<Exception>
shutdown()
Stops the work loop and closes all open connections.void
stop()
Stops the work loop and requests a shutdown.void
unregisterAll()
Unregisters allGearmanFunction
from the worker.void
unregisterFunction(String functionName)
Unregisters a particularGearmanFunction
from the worker.void
work()
This method is executed once the worker is ready to accept jobs from the Gearman Job Server.
-
-
-
Method Detail
-
work
void work()
This method is executed once the worker is ready to accept jobs from the Gearman Job Server. It should be called after all theGearmanJobServerConnection
have been added to the worker and all the applicableGearmanFunction
have been registered with the worker. After this method is executed, the Worker should be capable of receiving jobs from the Gearman Job Server and executing thus jobs.
-
addServer
boolean addServer(GearmanJobServerConnection conn)
Register a new Gearman Job Server with the worker.- Parameters:
conn
-GearmanJobServerConnection
connected to the Gearman Job Server- Returns:
- returns true if a connection to the server was established and the server was added to the worker, else false.
- Throws:
IllegalArgumentException
- If an invalid connection has been specified.IllegalStateException
- If the worker has already been stopped.
-
hasServer
boolean hasServer(GearmanJobServerConnection conn)
Has a connection to the specified Gearman Job Server been registered with this worker.- Parameters:
conn
- The connection to the specified Gearman Job Server.- Returns:
- True if the Gearman Job Server has been registered with the worker, otherwise false.
-
echo
String echo(String text, GearmanJobServerConnection conn)
Send an echo request to the specifiedGearmanJobServerConnection
, the Gearman Job Server should echo the contents of the echo request back to the Worker. This is can be used to verify the connection between a Worker and a Server.- Parameters:
text
- the text to include in the echo request.conn
- A connection to Gearman Job Server to which the request should be sent.- Returns:
- a string which should be equal to text sent to the server.
-
registerFunction
void registerFunction(Class<? extends GearmanFunction> function, long timeout)
Registers a particularGearmanFunction
with the worker. Once a function has been registered with a worker, the worker is capable of executing anyGearmanJob
that matches the registered function. Upon registering a function, the Worker notifies all Gearman Job Servers that it can accept any job that contains the applicable function.- Parameters:
function
- The function being registered with the Worker.timeout
- The amount of time the Worker will allow a job to execute the particular function. If the execution of the function exceeds the timeout value, the job will be marked as failed.
-
registerFunction
void registerFunction(Class<? extends GearmanFunction> function)
Registers a particularGearmanFunction
with the worker. Once a function has been registered with a worker, the worker is capable of executing anyGearmanJob
that matches the registered function. Upon registering a function, the Worker notifies all Gearman Job Servers that is can accept any job that contains the applicable function.- Parameters:
function
- The function being registered with the Worker.
-
registerFunctionFactory
void registerFunctionFactory(GearmanFunctionFactory factory, long timeout)
Registers aGearmanFunctionFactory
with the worker and specifies the timeout associated with any functions generated by this factory.- Parameters:
factory
- A function factory is factory that the worker will use to create the instance of a function when the worker needs a function of the type specified by the factory.timeout
- The amount of time the Worker will allow a job to execute the particular function. If the execution of the function exceeds the timeout value, the job will be marked as failed.
-
registerFunctionFactory
void registerFunctionFactory(GearmanFunctionFactory factory)
Registers aGearmanFunctionFactory
with the worker.- Parameters:
factory
- A function factory is factory that the worker will use to create the instance of a function when the worker needs a function of the type specified by the factory.
-
getRegisteredFunctions
Set<String> getRegisteredFunctions()
Retrieve the names of all functions that have been registered with this worker. If no functions have been registered, any empty set should be returned.- Returns:
- The name of all registered functions.
-
setWorkerID
void setWorkerID(String id)
Set the ID for a particular worker instance. This enables monitoring and reporting commands to uniquely identify specific workers.- Parameters:
id
- The ID.
-
setWorkerID
void setWorkerID(String id, GearmanJobServerConnection conn)
Set the ID used by a particular Gearman Job Server to identify a particular Worker. This enables monitoring and reporting commands to uniquely identify specific workers.- Parameters:
id
- The ID.conn
- The Gearman Job Server to which this worker ID applies.
-
getWorkerID
String getWorkerID()
Retrieves the ID used by this worker instance.- Returns:
- worker ID
-
getWorkerID
String getWorkerID(GearmanJobServerConnection conn)
Retrieves the ID this worker uses to identify itself with a specific connection.- Parameters:
conn
-- Returns:
- worker ID
-
unregisterFunction
void unregisterFunction(String functionName)
Unregisters a particularGearmanFunction
from the worker. Once a function has been unregistered from the Worker, a Worker will no longer accept jobs which require the execution of the unregistered function.- Parameters:
functionName
-
-
unregisterAll
void unregisterAll()
Unregisters allGearmanFunction
from the worker. The effect of which is that the worker will not execute any new jobs.
-
stop
void stop()
Stops the work loop and requests a shutdown.
-
shutdown
List<Exception> shutdown()
Stops the work loop and closes all open connections.- Returns:
- a List of any Exceptions thrown when closing connections
-
isRunning
boolean isRunning()
Determine if the worker is currently executing the work loop.- Returns:
- True if the work loop is executing, else false.
-
setJobUniqueIdRequired
void setJobUniqueIdRequired(boolean requiresJobUUID)
Should worker only ask for jobs which contain a Unique Id.- Parameters:
requiresJobUUID
- True if should worker only ask for jobs which contain a Unique Id.
-
isJobUniqueIdRequired
boolean isJobUniqueIdRequired()
Does worker only ask for jobs which contain a Unique Id.- Returns:
- true if worker only asks for jobs which contain a Unique Id, else false.
-
-