Package org.gearman.worker
Class GearmanWorkerImpl
- java.lang.Object
-
- org.gearman.worker.GearmanWorkerImpl
-
- All Implemented Interfaces:
GearmanSessionEventHandler
,GearmanWorker
public class GearmanWorkerImpl extends Object implements GearmanWorker, GearmanSessionEventHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GearmanWorkerImpl.State
-
Constructor Summary
Constructors Constructor Description GearmanWorkerImpl()
GearmanWorkerImpl(ExecutorService executorService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addServer(String host, int port)
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.void
handleSessionEvent(GearmanSessionEvent event)
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
registerFunction(String function)
void
registerFunction(String function, long timeout)
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.String
toString()
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.
-
-
-
Constructor Detail
-
GearmanWorkerImpl
public GearmanWorkerImpl()
-
GearmanWorkerImpl
public GearmanWorkerImpl(ExecutorService executorService)
-
-
Method Detail
-
work
public void work()
Description copied from interface:GearmanWorker
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.- Specified by:
work
in interfaceGearmanWorker
-
handleSessionEvent
public void handleSessionEvent(GearmanSessionEvent event) throws IllegalArgumentException, IllegalStateException
- Specified by:
handleSessionEvent
in interfaceGearmanSessionEventHandler
- Throws:
IllegalArgumentException
IllegalStateException
-
addServer
public boolean addServer(String host, int port)
-
addServer
public boolean addServer(GearmanJobServerConnection conn) throws IllegalArgumentException, IllegalStateException
Description copied from interface:GearmanWorker
Register a new Gearman Job Server with the worker.- Specified by:
addServer
in interfaceGearmanWorker
- 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
public boolean hasServer(GearmanJobServerConnection conn)
Description copied from interface:GearmanWorker
Has a connection to the specified Gearman Job Server been registered with this worker.- Specified by:
hasServer
in interfaceGearmanWorker
- 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
public String echo(String text, GearmanJobServerConnection conn)
Description copied from interface:GearmanWorker
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.- Specified by:
echo
in interfaceGearmanWorker
- 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
public void registerFunction(String function, long timeout)
-
registerFunction
public void registerFunction(String function)
-
registerFunction
public void registerFunction(Class<? extends GearmanFunction> function)
Description copied from interface:GearmanWorker
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.- Specified by:
registerFunction
in interfaceGearmanWorker
- Parameters:
function
- The function being registered with the Worker.
-
registerFunction
public void registerFunction(Class<? extends GearmanFunction> function, long timeout)
Description copied from interface:GearmanWorker
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.- Specified by:
registerFunction
in interfaceGearmanWorker
- 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.
-
registerFunctionFactory
public void registerFunctionFactory(GearmanFunctionFactory factory)
Description copied from interface:GearmanWorker
Registers aGearmanFunctionFactory
with the worker.- Specified by:
registerFunctionFactory
in interfaceGearmanWorker
- 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.
-
registerFunctionFactory
public void registerFunctionFactory(GearmanFunctionFactory factory, long timeout)
Description copied from interface:GearmanWorker
Registers aGearmanFunctionFactory
with the worker and specifies the timeout associated with any functions generated by this factory.- Specified by:
registerFunctionFactory
in interfaceGearmanWorker
- 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.
-
getRegisteredFunctions
public Set<String> getRegisteredFunctions()
Description copied from interface:GearmanWorker
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.- Specified by:
getRegisteredFunctions
in interfaceGearmanWorker
- Returns:
- The name of all registered functions.
-
setWorkerID
public void setWorkerID(String id) throws IllegalArgumentException
Description copied from interface:GearmanWorker
Set the ID for a particular worker instance. This enables monitoring and reporting commands to uniquely identify specific workers.- Specified by:
setWorkerID
in interfaceGearmanWorker
- Parameters:
id
- The ID.- Throws:
IllegalArgumentException
-
setWorkerID
public void setWorkerID(String id, GearmanJobServerConnection conn)
Description copied from interface:GearmanWorker
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.- Specified by:
setWorkerID
in interfaceGearmanWorker
- Parameters:
id
- The ID.conn
- The Gearman Job Server to which this worker ID applies.
-
getWorkerID
public String getWorkerID()
Description copied from interface:GearmanWorker
Retrieves the ID used by this worker instance.- Specified by:
getWorkerID
in interfaceGearmanWorker
- Returns:
- worker ID
-
getWorkerID
public String getWorkerID(GearmanJobServerConnection conn)
Description copied from interface:GearmanWorker
Retrieves the ID this worker uses to identify itself with a specific connection.- Specified by:
getWorkerID
in interfaceGearmanWorker
- Returns:
- worker ID
-
unregisterFunction
public void unregisterFunction(String functionName)
Description copied from interface:GearmanWorker
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.- Specified by:
unregisterFunction
in interfaceGearmanWorker
-
unregisterAll
public void unregisterAll()
Description copied from interface:GearmanWorker
Unregisters allGearmanFunction
from the worker. The effect of which is that the worker will not execute any new jobs.- Specified by:
unregisterAll
in interfaceGearmanWorker
-
stop
public void stop()
Description copied from interface:GearmanWorker
Stops the work loop and requests a shutdown.- Specified by:
stop
in interfaceGearmanWorker
-
shutdown
public List<Exception> shutdown()
Description copied from interface:GearmanWorker
Stops the work loop and closes all open connections.- Specified by:
shutdown
in interfaceGearmanWorker
- Returns:
- a List of any Exceptions thrown when closing connections
-
isRunning
public boolean isRunning()
Description copied from interface:GearmanWorker
Determine if the worker is currently executing the work loop.- Specified by:
isRunning
in interfaceGearmanWorker
- Returns:
- True if the work loop is executing, else false.
-
setJobUniqueIdRequired
public void setJobUniqueIdRequired(boolean requiresJobUUID)
Description copied from interface:GearmanWorker
Should worker only ask for jobs which contain a Unique Id.- Specified by:
setJobUniqueIdRequired
in interfaceGearmanWorker
- Parameters:
requiresJobUUID
- True if should worker only ask for jobs which contain a Unique Id.
-
isJobUniqueIdRequired
public boolean isJobUniqueIdRequired()
Description copied from interface:GearmanWorker
Does worker only ask for jobs which contain a Unique Id.- Specified by:
isJobUniqueIdRequired
in interfaceGearmanWorker
- Returns:
- true if worker only asks for jobs which contain a Unique Id, else false.
-
-