public class WorkerImpl extends Object implements Worker
JobExecutor.State| Modifier and Type | Field and Description |
|---|---|
protected Config |
config |
protected static long |
emptyQueueSleepTime |
protected redis.clients.jedis.Jedis |
jedis |
protected WorkerListenerDelegate |
listenerDelegate |
protected String |
namespace |
protected BlockingDeque<String> |
queueNames |
protected static int |
reconnectAttempts |
protected static long |
reconnectSleepTime |
protected AtomicReference<JobExecutor.State> |
state |
ALL_QUEUES| Constructor and Description |
|---|
WorkerImpl(Config config,
Collection<String> queues,
Map<String,? extends Class<?>> jobTypes)
Creates a new WorkerImpl, which creates it's own connection to
Redis using values from the config.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addJobType(String jobName,
Class<?> jobType)
Allow the given job type to be executed.
|
void |
addListener(WorkerListener listener)
Register a WorkerListener for all WorkerEvents.
|
void |
addListener(WorkerListener listener,
WorkerEvent... events)
Register a WorkerListener for the specified WorkerEvents.
|
void |
addQueue(String queueName)
Poll the given queue.
|
protected void |
checkJobType(String jobName,
Class<?> jobType)
Determine if a job name and job type are valid.
|
protected void |
checkJobTypes(Map<String,? extends Class<?>> jobTypes)
Verify the given job types are all valid.
|
protected void |
checkPaused()
Checks to see if worker is paused.
|
protected static void |
checkQueues(Iterable<String> queues)
Verify that the given queues are all valid.
|
protected String |
createName()
Creates a unique name, suitable for use with Resque.
|
void |
end(boolean now)
Shutdown this Worker.
The worker cannot be started again; create a new worker in this case. |
protected Object |
execute(Job job,
String curQueue,
Object instance)
Executes the given job.
|
protected String |
failMsg(Exception ex,
String queue,
Job job)
Create and serialize a JobFailure.
|
protected void |
failure(Exception ex,
Job job,
String curQueue)
Update the status in Redis on failure
|
ExceptionHandler |
getExceptionHandler()
The current exception handler.
|
Map<String,Class<?>> |
getJobTypes()
The allowed job names and types that this JobExecutor will execute.
|
String |
getName()
Returns the name of this Worker.
|
Collection<String> |
getQueues()
The queues that this Worker will poll.
|
protected int |
getReconnectAttempts() |
long |
getWorkerId() |
boolean |
isPaused()
Returns whether this worker is paused.
|
boolean |
isProcessingJob()
Returns whether this JobExecutor is currently processing a job.
|
boolean |
isShutdown()
Returns whether this JobExecutor is either shutdown or in the process of shutting down.
|
static boolean |
isThreadNameChangingEnabled() |
void |
join(long millis)
Wait for this JobExecutor to complete.
|
protected String |
key(String... parts)
Builds a namespaced Redis key with the given arguments.
|
protected String |
pauseMsg()
Create and serialize a WorkerStatus for a pause event.
|
protected void |
poll()
Polls the queues for jobs and executes them.
|
protected void |
process(Job job,
String curQueue)
Materializes and executes the given job.
|
protected void |
recoverFromException(String curQueue,
Exception e)
Handle an exception that was thrown from inside
poll() |
void |
removeAllListeners()
Unregister all WorkerListeners for all WorkerEvents.
|
void |
removeAllListeners(WorkerEvent... events)
Unregister all WorkerListeners for the specified WorkerEvents.
|
void |
removeAllQueues()
Stop polling all queues.
|
void |
removeJobName(String jobName)
Disallow the job name from being executed.
|
void |
removeJobType(Class<?> jobType)
Disallow the job type from being executed.
|
void |
removeListener(WorkerListener listener)
Unregister a WorkerListener for all WorkerEvents.
|
void |
removeListener(WorkerListener listener,
WorkerEvent... events)
Unregister a WorkerListener for the specified WorkerEvents.
|
void |
removeQueue(String queueName,
boolean all)
Stop polling the given queue.
|
protected void |
renameThread(String msg)
Rename the current thread with the given message.
|
void |
run()
Starts this worker.
|
void |
setExceptionHandler(ExceptionHandler exceptionHandler)
Set this JobExecutor's exception handler to the given handler.
|
void |
setJobTypes(Map<String,? extends Class<?>> jobTypes)
Clear any current allowed job types and use the given set.
|
void |
setQueues(Collection<String> queues)
Clear any current queues and poll the given queues.
|
static void |
setThreadNameChangingEnabled(boolean enabled)
Enable/disable worker thread renaming during normal operation.
|
protected String |
statusMsg(String queue,
Job job)
Create and serialize a WorkerStatus.
|
protected void |
success(Job job,
Object runner,
Object result,
String curQueue)
Update the status in Redis on success.
|
void |
togglePause(boolean paused)
Toggle whether this worker will process any new jobs.
|
String |
toString() |
protected static final long emptyQueueSleepTime
protected static final long reconnectSleepTime
protected static final int reconnectAttempts
protected final Config config
protected final redis.clients.jedis.Jedis jedis
protected final String namespace
protected final BlockingDeque<String> queueNames
protected final WorkerListenerDelegate listenerDelegate
protected final AtomicReference<JobExecutor.State> state
public WorkerImpl(Config config, Collection<String> queues, Map<String,? extends Class<?>> jobTypes)
config - used to create a connection to Redis and the package
prefix for incoming jobsqueues - the list of queues to polljobTypes - the map of job names and types to executeIllegalArgumentException - if the config is null,
if the queues is null, or if the jobTypes is null or emptypublic static boolean isThreadNameChangingEnabled()
public static void setThreadNameChangingEnabled(boolean enabled)
enabled - whether threads' names should change during normal operationprotected static void checkQueues(Iterable<String> queues)
queues - the given queuespublic long getWorkerId()
public void run()
public void end(boolean now)
end in interface JobExecutornow - if true, an effort will be made to stop any job in progresspublic boolean isShutdown()
JobExecutorisShutdown in interface JobExecutorpublic boolean isPaused()
Workerpublic boolean isProcessingJob()
JobExecutorisProcessingJob in interface JobExecutorpublic void togglePause(boolean paused)
WorkertogglePause in interface Workerpaused - if true, the worker will not process any new jobs;
if false, the worker will process new jobspublic String getName()
Workerpublic void addListener(WorkerListener listener)
WorkerEventEmitteraddListener in interface WorkerEventEmitterlistener - the WorkerListener to registerpublic void addListener(WorkerListener listener, WorkerEvent... events)
WorkerEventEmitteraddListener in interface WorkerEventEmitterlistener - the WorkerListener to registerevents - the WorkerEvents to be notified ofpublic void removeListener(WorkerListener listener)
WorkerEventEmitterremoveListener in interface WorkerEventEmitterlistener - the WorkerListener to unregisterpublic void removeListener(WorkerListener listener, WorkerEvent... events)
WorkerEventEmitterremoveListener in interface WorkerEventEmitterlistener - the WorkerListener to unregisterevents - the WorkerEvents to no longer be notified ofpublic void removeAllListeners()
WorkerEventEmitterremoveAllListeners in interface WorkerEventEmitterpublic void removeAllListeners(WorkerEvent... events)
WorkerEventEmitterremoveAllListeners in interface WorkerEventEmitterevents - the WorkerEvents to no longer be notified ofpublic Collection<String> getQueues()
Workerpublic void addQueue(String queueName)
Workerpublic void removeQueue(String queueName, boolean all)
Workerall argument is
true, all instances of the queue will be removed, otherwise, only
one instance is removed.removeQueue in interface WorkerqueueName - the queue to stop pollingall - whether to remove all or only one of the instancespublic void removeAllQueues()
WorkerremoveAllQueues in interface Workerpublic void setQueues(Collection<String> queues)
Workerpublic Map<String,Class<?>> getJobTypes()
JobExecutorgetJobTypes in interface JobExecutorpublic void addJobType(String jobName, Class<?> jobType)
JobExecutoraddJobType in interface JobExecutorjobName - the job name as seenjobType - the job type to allowpublic void removeJobType(Class<?> jobType)
JobExecutorremoveJobType in interface JobExecutorjobType - the job type to disallowpublic void removeJobName(String jobName)
JobExecutorremoveJobName in interface JobExecutorjobName - the job name to disallowpublic void setJobTypes(Map<String,? extends Class<?>> jobTypes)
JobExecutorsetJobTypes in interface JobExecutorjobTypes - the job types to allowpublic ExceptionHandler getExceptionHandler()
JobExecutorgetExceptionHandler in interface JobExecutorpublic void setExceptionHandler(ExceptionHandler exceptionHandler)
JobExecutorsetExceptionHandler in interface JobExecutorexceptionHandler - the exception handler to usepublic void join(long millis)
throws InterruptedException
JobExecutorJobExecutor.end(boolean).join in interface JobExecutormillis - the time to wait in millisecondsInterruptedException - if any thread has interrupted the current threadprotected int getReconnectAttempts()
protected void poll()
protected void recoverFromException(String curQueue, Exception e)
poll()curQueue - the name of the queue that was being processed when the exception was throwne - the exception that was thrownprotected void checkPaused()
throws IOException
IOException - if there was an error creating the pause messageprotected void process(Job job, String curQueue)
job - the Job to processcurQueue - the queue the payload came fromprotected Object execute(Job job, String curQueue, Object instance) throws Exception
protected void success(Job job, Object runner, Object result, String curQueue)
job - the Job that succeededrunner - the materialized JobcurQueue - the queue the Job came fromprotected void failure(Exception ex, Job job, String curQueue)
ex - the Exception that occurredjob - the Job that failedcurQueue - the queue the Job came fromprotected String failMsg(Exception ex, String queue, Job job) throws IOException
ex - the Exception that occurredqueue - the queue the job came fromjob - the Job that failedIOException - if there was an error serializing the JobFailureprotected String statusMsg(String queue, Job job) throws IOException
queue - the queue the Job came fromjob - the Job currently being processedIOException - if there was an error serializing the WorkerStatusprotected String pauseMsg() throws IOException
IOException - if there was an error serializing the WorkerStatusprotected String createName()
protected String key(String... parts)
parts - the key parts to be joinedprotected void renameThread(String msg)
msg - the message to add to the thread nameprotected void checkJobTypes(Map<String,? extends Class<?>> jobTypes)
jobTypes - the given job typesIllegalArgumentException - if the job types are invalidprotected void checkJobType(String jobName, Class<?> jobType)
jobName - the name of the jobjobType - the class of the jobIllegalArgumentException - if the name and type are invalidCopyright © 2011-2013. All Rights Reserved.