Package org.eclipse.scanning.event
Class JobQueueImpl<U extends StatusBean>
java.lang.Object
org.eclipse.scanning.event.JobQueueImpl<U>
- All Implemented Interfaces:
AutoCloseable,IBeanClass<U>,IConnection,IJobQueue<U>,IURIConnection
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceprotected classprotected classThe process manager is a listener for bean changes that handles the status of a bean being set to a request state, e.g.Nested classes/interfaces inherited from interface org.eclipse.scanning.api.event.core.IJobQueue
IJobQueue.IQueueStatusListener -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected IEventConnectorServiceprotected Stringprotected Stringprotected final URI -
Constructor Summary
ConstructorsConstructorDescriptionJobQueueImpl(URI uri, String submitQueueName, String statusTopicName, String queueStatusTopicName, String commandTopicName, String commandAckTopicName, IEventConnectorService connectorService, IEventService eventService) -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidAwaits the start of the consumervoidAwaits the job queue's consumer thread having stopped.voidCleans up the set of beans for running and completed job by removing certain beans.voidRemoves all pending jobs from the submission queue.voidClears the set of beans for running and completed jobs.voidclearRunningAndCompleted(boolean bool) Clears the set of beans for running and completed jobs, with an optional argument for whether to clear the currently running scan (if there is one)protected javax.jms.QueuecreateQueue(String queueName) Creates and returns a queue of the given nameprotected IPersistentModifiableIdQueue<U> createQueue(IEventConnectorService connectorService, org.h2.mvstore.MVStore store, String queueName) voidSets the given bean (which must beStatus.SUBMITTEDto be deferred, not to be run until it is undeferred.voidCall to disconnect any resources which we no longer need.protected ObjectfindBeanAndPerformAction(StatusBean bean, JobQueueImpl.BeanAction<U> action) Class of bean usually extending StatusBeanThe topic used by the consumer to send acknowledgements for commands received on the command topic.The topic used to run commands like terminate the running process and get the consumer to stop.The underlyng service which the uri is connected usingThe UUID which uniquely defines this job queue.getName()protected javax.jms.QueueSessionReturns theQueueStatusindicating whether the job queue's consumer thread isQueueStatus.RUNNING,QueueStatus.PAUSEDorQueueStatus.STOPPED.If set, the name of the topic that the job queue's consumer topic publishesQueueStatusBeans to, to indicate that it is running.Returns theIProcessCreatorused as a factory to createIBeanProcessfor each bean as they are removed from the submission queue by the consumer thread.Return a list of beans whose jobs are either running or completed.getRunningAndCompleted(String optionalArgument) Returns either the list returned byIJobQueue.getRunningAndCompleted()or a sublist of that list, limited by the argument.protected javax.jms.SessionGet a copy of the current submission queue as a list of beans.getUri()The URI of this connection.booleanisActive()Returns if the job queue is active.booleanReturns whether this connection is connected.protected booleanisForSameObject(Object qbean, Object bean) A utility method to test if a bean from the queue represents the same bean as that given .booleanisPaused()Returns whether the job queue's consumer thread is paused.booleanReturns whether the job queue's consumer thread will start in a paused state if the submission queue is non-empty.booleanmoveBackward(U bean) Moves the given bean towards the tail of the submission queue if possible, i.e.booleanmoveForward(U bean) Moves the given bean towards the head of the submission queue if possible, i.e.voidpause()Pauses the job queue's consumer thread if running.voidIf a process for the given bean exists and is running, pauses it.protected voidprocessQueueCommand(QueueCommandBean<U> commandBean) Processes a command for this queue from the command topic.voidbooleanRemoves the bean from the submission queue if present.protected voidremoveCompleted(String beanUniqueId) booleanremoveCompleted(U bean) Remove the given bean from the set of beans for running and completed jobs (the status set).voidbooleanReplace the bean in the submission queue with the given bean, if present.voidresume()Resumes the job queue's consumer thread if paused.voidIf a process for the given bean exists and is paused, resumes it.voidrun()The main consume method for theIJobQueue's consumer thread.protected final voidCalls therun()method.voidsetBeanClass(Class<U> beanClass) Class of bean usually extending StatusBean It is not compulsory to set the bean class unless trying to deserialize messages sent by older versions of the connector service.protected voidsetConnected(boolean connected) voidvoidsetPauseOnStart(boolean pauseOnStart) Sets whether the job queue's consumer thread should start in a paused state if the submission queue is non-empty.voidsetRunner(IProcessCreator<U> runner) Set the consumer process to run for each job.voidsetStatusTopicName(String statusTopicName) voidstart()Starts the consumer in new thread and return.voidstop()Ask the consumer to stopvoidAdds the given bean to the tail of the submission queue.voidterminateJob(U bean) If the process for the given bean exists and is running or paused, terminates it.voidAllow a submitted, deferred bean to be run when it is reached again.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.eclipse.scanning.api.event.core.IConnection
close, isConnectedMethods inherited from interface org.eclipse.scanning.api.event.core.IJobQueue
getStatusTopicName, getSubmitQueueName, setStatusTopicName
-
Field Details
-
uri
-
submitQueueName
-
statusTopicName
-
service
-
-
Constructor Details
-
JobQueueImpl
public JobQueueImpl(URI uri, String submitQueueName, String statusTopicName, String queueStatusTopicName, String commandTopicName, String commandAckTopicName, IEventConnectorService connectorService, IEventService eventService) throws EventException - Throws:
EventException
-
-
Method Details
-
createQueue
protected IPersistentModifiableIdQueue<U> createQueue(IEventConnectorService connectorService, org.h2.mvstore.MVStore store, String queueName) -
getPersistentStorePath
-
addQueueStatusListener
- Specified by:
addQueueStatusListenerin interfaceIJobQueue<U extends StatusBean>
-
removeQueueStatusListener
- Specified by:
removeQueueStatusListenerin interfaceIJobQueue<U extends StatusBean>
-
publishQueueStatusBean
public void publishQueueStatusBean() -
disconnect
Description copied from interface:IConnectionCall to disconnect any resources which we no longer need. The resource may have timed out so it might not be connected, in that case it silently returns. Note, that the methodIConnection.close(), inherited fromAutoCloseablecalls this method, which means that if an object of a class implementingIConnectionis used with a try-with-resources block this method will be called automatically when exiting that block.- Specified by:
disconnectin interfaceIConnection- Throws:
EventException- if resource could not be disconnected.
-
processQueueCommand
Processes a command for this queue from the command topic.- Parameters:
commandBean-
-
getSubmissionQueue
Description copied from interface:IJobQueueGet a copy of the current submission queue as a list of beans.- Specified by:
getSubmissionQueuein interfaceIJobQueue<U extends StatusBean>- Returns:
- the submission queue
-
getRunningAndCompleted
Description copied from interface:IJobQueueReturn a list of beans whose jobs are either running or completed. The list is ordered by submission time, not necessarily the ordering of the JMS queue.- Specified by:
getRunningAndCompletedin interfaceIJobQueue<U extends StatusBean>- Returns:
- running and completed beans
- Throws:
EventException- if the set of running and completed beans cannot be returned for any reason
-
getRunningAndCompleted
Description copied from interface:IJobQueueReturns either the list returned byIJobQueue.getRunningAndCompleted()or a sublist of that list, limited by the argument. Currently the optional argument may be a non-negative integer in which case if the number of submitted + running + completed scans exceeds the argument, completed scans are removed until either none remain or the number of scans being returned is less than the argument. If a scan is running it must always be returned. If any String that would not parse to a non-negative integer is passed, then the argument is taken to be unlimited, and this method should returnIJobQueue.getRunningAndCompleted()- Specified by:
getRunningAndCompletedin interfaceIJobQueue<U extends StatusBean>- Parameters:
optionalArgument- a String representation of a non-negative integer ("0", "1", "2"...)- Returns:
- list containing the running bean and optionally some completed beans
- Throws:
EventException
-
clearQueue
Description copied from interface:IJobQueueRemoves all pending jobs from the submission queue.- Specified by:
clearQueuein interfaceIJobQueue<U extends StatusBean>- Throws:
EventException- if the queue cannot be cleared for any reason
-
clearRunningAndCompleted
Description copied from interface:IJobQueueClears the set of beans for running and completed jobs.- Specified by:
clearRunningAndCompletedin interfaceIJobQueue<U extends StatusBean>- Throws:
EventException- if the set of running and completed
-
clearRunningAndCompleted
Description copied from interface:IJobQueueClears the set of beans for running and completed jobs, with an optional argument for whether to clear the currently running scan (if there is one)- Specified by:
clearRunningAndCompletedin interfaceIJobQueue<U extends StatusBean>- Throws:
EventException- if the set of running and completed
-
cleanUpCompleted
Description copied from interface:IJobQueueCleans up the set of beans for running and completed job by removing certain beans. Specifically, the beans that are removed are those that meet one of the following criteria:- have status
Status.FAILEDorStatus.NONE; - have a status indicating that they are running (i.e.
Status.isRunning()istrue) and are older than the maximum running age (by default, two days); - have a status indicating that they are final (i.e.
Status.isFinal()istrue) and are older than the maximum complete age (by default, one week); - Additionally jobs that are not started or paused will have their status set to
Status.FAILED;
This method is intended to be called on starting the consumer.
- Specified by:
cleanUpCompletedin interfaceIJobQueue<U extends StatusBean>- Throws:
EventException
- have status
-
moveForward
Description copied from interface:IJobQueueMoves the given bean towards the head of the submission queue if possible, i.e. it will be processed sooner.- Specified by:
moveForwardin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean-- Returns:
trueif the bean could be moved,falseotherwise- Throws:
EventException
-
moveBackward
Description copied from interface:IJobQueueMoves the given bean towards the tail of the submission queue if possible, i.e. it will be processed later.- Specified by:
moveBackwardin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean-- Returns:
trueif the bean could be moved,falseotherwise- Throws:
EventException
-
submit
Description copied from interface:IJobQueueAdds the given bean to the tail of the submission queue.- Specified by:
submitin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean to add- Throws:
EventException
-
remove
Description copied from interface:IJobQueueRemoves the bean from the submission queue if present. If the bean has been moved to the set of running and completed jobs, it will not be removed.- Specified by:
removein interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean to remove- Returns:
trueif the bean was removed,falseotherwise, i.e. the bean was not present- Throws:
EventException
-
removeCompleted
Description copied from interface:IJobQueueRemove the given bean from the set of beans for running and completed jobs (the status set).- Specified by:
removeCompletedin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean to remove- Returns:
trueif the bean was removed,falseotherwise, i.e. the bean was not present- Throws:
EventException
-
replace
Description copied from interface:IJobQueueReplace the bean in the submission queue with the given bean, if present. If the bean has been moved to the set of running and completed jobs, it will not be removed. A bean will replace another if it has the same unique id as returned byIdBean.getUniqueId()- Specified by:
replacein interfaceIJobQueue<U extends StatusBean>- Parameters:
newBean- bean to remove- Returns:
trueif the bean was replaced,falseotherwise, i.e. the bean was not present- Throws:
EventException
-
setRunner
Description copied from interface:IJobQueueSet the consumer process to run for each job.- Specified by:
setRunnerin interfaceIJobQueue<U extends StatusBean>- Parameters:
runner-- Throws:
EventException- if the alive topic cannot be sent
-
start
Description copied from interface:IJobQueueStarts the consumer in new thread and return. Similar to Thread.start() You must set the runner before calling this method- Specified by:
startin interfaceIJobQueue<U extends StatusBean>- Throws:
EventException
-
awaitStart
Awaits the start of the consumer- Specified by:
awaitStartin interfaceIJobQueue<U extends StatusBean>- Throws:
InterruptedExceptionException
-
awaitStop
Description copied from interface:IJobQueueAwaits the job queue's consumer thread having stopped. Mostly useful for testing.- Specified by:
awaitStopin interfaceIJobQueue<U extends StatusBean>- Throws:
InterruptedException
-
stop
Description copied from interface:IJobQueueAsk the consumer to stop- Specified by:
stopin interfaceIJobQueue<U extends StatusBean>- Throws:
EventException
-
runAndStopIfError
protected final void runAndStopIfError() -
run
The main consume method for theIJobQueue's consumer thread. When the consumer thread is running, (typically a thread created by calling thestart()method), it will be executing this method. It can be controlled by calling methods such aspause()orresume()from other threads.- Specified by:
runin interfaceIJobQueue<U extends StatusBean>- Throws:
EventException
-
isPaused
public boolean isPaused()Description copied from interface:IJobQueueReturns whether the job queue's consumer thread is paused.- Specified by:
isPausedin interfaceIJobQueue<U extends StatusBean>- Returns:
trueif the consumer thread is paused,falseotherwise
-
pause
Description copied from interface:IJobQueuePauses the job queue's consumer thread if running. It will not process any more jobs until it resumes. Currently running jobs are not affected.- Specified by:
pausein interfaceIJobQueue<U extends StatusBean>- Throws:
EventException
-
resume
Description copied from interface:IJobQueueResumes the job queue's consumer thread if paused. It will resume processing jobs.- Specified by:
resumein interfaceIJobQueue<U extends StatusBean>- Throws:
EventException
-
findBeanAndPerformAction
protected Object findBeanAndPerformAction(StatusBean bean, JobQueueImpl.BeanAction<U> action) throws EventException - Throws:
EventException
-
removeCompleted
- Throws:
EventException
-
getQueueStatus
Description copied from interface:IJobQueueReturns theQueueStatusindicating whether the job queue's consumer thread isQueueStatus.RUNNING,QueueStatus.PAUSEDorQueueStatus.STOPPED.- Specified by:
getQueueStatusin interfaceIJobQueue<U extends StatusBean>- Returns:
- the current status of the queue.
-
pauseJob
Description copied from interface:IJobQueueIf a process for the given bean exists and is running, pauses it. If the bean is still in the submission queue, then instead defers the bean, allowing other scans to be run first.- Specified by:
pauseJobin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean whose process to pause.- Throws:
EventException
-
resumeJob
Description copied from interface:IJobQueueIf a process for the given bean exists and is paused, resumes it.- Specified by:
resumeJobin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean whose process to resume.- Throws:
EventException
-
terminateJob
Description copied from interface:IJobQueueIf the process for the given bean exists and is running or paused, terminates it. If the bean is still in the submission queue then instead when the consumer thread removes it from the queue it will set its status toStatus.TERMINATED, add the bean to the set of running and completed jobs. It will not create a process for it.- Specified by:
terminateJobin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean whose process to terminate- Throws:
EventException
-
getRunner
Description copied from interface:IJobQueueReturns theIProcessCreatorused as a factory to createIBeanProcessfor each bean as they are removed from the submission queue by the consumer thread.- Specified by:
getRunnerin interfaceIJobQueue<U extends StatusBean>- Returns:
- the process creator
-
getJobQueueId
Description copied from interface:IJobQueueThe UUID which uniquely defines this job queue.- Specified by:
getJobQueueIdin interfaceIJobQueue<U extends StatusBean>- Returns:
- job queue id
-
getName
- Specified by:
getNamein interfaceIJobQueue<U extends StatusBean>
-
setName
- Specified by:
setNamein interfaceIJobQueue<U extends StatusBean>
-
isActive
public boolean isActive()Description copied from interface:IJobQueueReturns if the job queue is active. This istrueas long as the consumer thread is notQueueStatus.STOPPED, i.e. it isQueueStatus.RUNNINGorQueueStatus.PAUSED.- Specified by:
isActivein interfaceIJobQueue<U extends StatusBean>- Returns:
- true if the consumer is active
-
isPauseOnStart
public boolean isPauseOnStart()Description copied from interface:IJobQueueReturns whether the job queue's consumer thread will start in a paused state if the submission queue is non-empty. If the submission queue is empty this flag will have no effect.- Specified by:
isPauseOnStartin interfaceIJobQueue<U extends StatusBean>- Returns:
trueif the consumer thread should start paused with a non-empty submission queue,falseotherwise
-
setPauseOnStart
public void setPauseOnStart(boolean pauseOnStart) Description copied from interface:IJobQueueSets whether the job queue's consumer thread should start in a paused state if the submission queue is non-empty. If the submission queue is empty this flag will have no effect. Note this must be called before the consumer thread is stared.- Specified by:
setPauseOnStartin interfaceIJobQueue<U extends StatusBean>- Parameters:
pauseOnStart-trueif the consumer thread should start paused with a non-empty submission queue,falseotherwise
-
getQueueStatusTopicName
Description copied from interface:IJobQueueIf set, the name of the topic that the job queue's consumer topic publishesQueueStatusBeans to, to indicate that it is running.- Specified by:
getQueueStatusTopicNamein interfaceIJobQueue<U extends StatusBean>- Returns:
- the queue status topic name, may be
null
-
getCommandTopicName
Description copied from interface:IJobQueueThe topic used to run commands like terminate the running process and get the consumer to stop.- Specified by:
getCommandTopicNamein interfaceIJobQueue<U extends StatusBean>- Returns:
- topic name
-
getCommandAckTopicName
Description copied from interface:IJobQueueThe topic used by the consumer to send acknowledgements for commands received on the command topic.- Specified by:
getCommandAckTopicNamein interfaceIJobQueue<U extends StatusBean>- Returns:
- command acknowledgement topic name
-
getBeanClass
Description copied from interface:IBeanClassClass of bean usually extending StatusBean- Specified by:
getBeanClassin interfaceIBeanClass<U extends StatusBean>- Returns:
- class or null
-
setBeanClass
Description copied from interface:IBeanClassClass of bean usually extending StatusBean It is not compulsory to set the bean class unless trying to deserialize messages sent by older versions of the connector service.- Specified by:
setBeanClassin interfaceIBeanClass<U extends StatusBean>
-
defer
Description copied from interface:IJobQueueSets the given bean (which must beStatus.SUBMITTEDto be deferred, not to be run until it is undeferred.- Specified by:
deferin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean whose process to resume.- Throws:
EventException
-
undefer
Description copied from interface:IJobQueueAllow a submitted, deferred bean to be run when it is reached again.- Specified by:
undeferin interfaceIJobQueue<U extends StatusBean>- Parameters:
bean- bean whose process to resume.- Throws:
EventException
-
getConnectorService
Description copied from interface:IURIConnectionThe underlyng service which the uri is connected using- Specified by:
getConnectorServicein interfaceIURIConnection
-
getSession
protected javax.jms.Session getSession() throws javax.jms.JMSException- Throws:
javax.jms.JMSException
-
getQueueSession
protected javax.jms.QueueSession getQueueSession() throws javax.jms.JMSException- Throws:
javax.jms.JMSException
-
createQueue
Creates and returns a queue of the given name- Parameters:
queueName-- Returns:
- Throws:
javax.jms.JMSException
-
getUri
Description copied from interface:IURIConnectionThe URI of this connection.- Specified by:
getUriin interfaceIURIConnection- Returns:
-
getSubmitQueueName
-
getStatusTopicName
-
setStatusTopicName
-
isForSameObject
A utility method to test if a bean from the queue represents the same bean as that given . This is done by comparing their uniqueIds, if present, and falls back on usingObject.equals(Object)- Parameters:
qbean-bean-- Returns:
trueif the two beans represent the same object,falseotherwise
-
isConnected
public boolean isConnected()Description copied from interface:IConnectionReturns whether this connection is connected. This method returnstrueuntil disconnect is called, even if the connection is created lazily.- Specified by:
isConnectedin interfaceIConnection- Returns:
trueif connected,falseotherwise
-
setConnected
protected void setConnected(boolean connected)
-