Package org.refcodes.jobbus
Class AbstractJobBus<CTX,H>
- java.lang.Object
-
- org.refcodes.jobbus.AbstractJobBus<CTX,H>
-
- Type Parameters:
CTX- The context type to use, can by anyComponent, service or POJO.H- The handle type used to reference a job.
- All Implemented Interfaces:
org.refcodes.component.FlushHandle<CTX>,org.refcodes.component.HandleLookup<CTX,H>,org.refcodes.component.ProgressHandle<CTX>,org.refcodes.component.ResetHandle<CTX>,JobBus<CTX,H>
- Direct Known Subclasses:
AbstractJobBusDirectory
public abstract class AbstractJobBus<CTX,H> extends java.lang.Object implements JobBus<CTX,H>
TheAbstractJobBusimplements theJobBusinterface.
-
-
Constructor Summary
Constructors Constructor Description AbstractJobBus(CTX aContext, org.refcodes.component.HandleGenerator<H> aHandleGenerator)Instantiates theAbstractJobBuswith the provided context and the providedHandleGenerator.AbstractJobBus(CTX aContext, org.refcodes.component.HandleGenerator<H> aHandleGenerator, java.util.concurrent.ExecutorService aExecutorService)Instantiates theAbstractJobBuswith the provided context and the providedHandleGenerator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Hexecute(org.refcodes.command.Undoable<CTX,?,?> aJob)Executes the given job.<RET,E extends java.lang.Exception>
voidexecute(org.refcodes.command.Undoable<CTX,RET,E> aJob, java.util.function.BiConsumer<RET,E> aResultConsumer)Executes the given job and invokes the providesBiConsumerlambda (closure) upon finished execution.<RET,E extends java.lang.Exception>
voidexecute(org.refcodes.command.Undoable<CTX,RET,E> aJob, java.util.function.Consumer<RET> aResultConsumer)Executes the given job and invokes the providedConsumerlambda (closure) upon finished execution.voidflush(H aHandle)<E extends java.lang.Exception>
EgetException(H aHandle)Gets the exception.protected org.refcodes.command.Undoable<CTX,?,?>getJob(H aHandle)Gets the job.floatgetProgress(H aHandle)<RET> RETgetResult(H aHandle)Gets the result.<JOB extends org.refcodes.command.Undoable<CTX,RET,?>,RET>
RETgetResult(JOB aJob)Executes the job and waits for the job's result or an exception.<JOB extends org.refcodes.command.Undoable<CTX,RET,?>,RET>
RETgetResult(JOB aJob, long aTimeoutInMs)Executes the job and waits for the job's result or an exception or till the timeout has been reached.protected java.util.Collection<org.refcodes.command.Undoable<CTX,?,?>>handleReferences()Retrieves the list of objects referenced by the handles.protected java.util.Set<H>handles()Returns the set of handles managed by this implementation.booleanhasException(H aHandle)Determines whether the job identified by the given handle ended with an exception (instead of a regular result).booleanhasFlush(H aHandle)booleanhasHandle(H aHandle)booleanhasProgress(H aHandle)booleanhasReset(H aHandle)booleanhasResult(H aHandle)Determines whether the job identified by the given handle has a regular result (instead of an exception).booleanisExecuted(H aHandle)Determines whether the job has been executed.org.refcodes.command.Undoable<CTX,?,?>lookupHandle(H aHandle)org.refcodes.command.Undoable<CTX,?,?>removeHandle(H aHandle)voidreset(H aHandle)voidwaitForExecution(H aHandle)Waits till the job identified by the given handle finished execution by regularly terminating or by throwing an exception.voidwaitForExecution(H aHandle, long aTimeoutInMs)Waits till the job identified by the given handle finished execution by regularly terminating or by throwing an exception or till the timeout has been reached.
-
-
-
Constructor Detail
-
AbstractJobBus
public AbstractJobBus(CTX aContext, org.refcodes.component.HandleGenerator<H> aHandleGenerator)
Instantiates theAbstractJobBuswith the provided context and the providedHandleGenerator. It is up to you which context (service,Component, POJO) you want to provide to a job (Undoable) when being executed. Also you can provide anyHandleGeneratoryou thing useful when creating handles. It is up to yourHandleGeneratorto generate unique handle objects. TheJobBusDirectoryactually uses aStringobjects generatingHandleGenerator. Make sure your handles implement theObject.hashCode()andObject.equals(Object)methods as of their method contracts as them handles will be used in collections such asHashMapdata structures.- Parameters:
aContext- The context which is passed to the job (Undoable) instances when being executed.aHandleGenerator- TheHandleGeneratorto be used when generating unique handle objects.
-
AbstractJobBus
public AbstractJobBus(CTX aContext, org.refcodes.component.HandleGenerator<H> aHandleGenerator, java.util.concurrent.ExecutorService aExecutorService)
Instantiates theAbstractJobBuswith the provided context and the providedHandleGenerator. It is up to you which context (service,Component, POJO) you want to provide to a job (Undoable) when being executed. Also you can provide anyHandleGeneratoryou thing useful when creating handles. It is up to yourHandleGeneratorto generate unique handle objects. TheJobBusDirectoryactually uses aStringobjects generatingHandleGenerator. Make sure your handles implement theObject.hashCode()andObject.equals(Object)methods as of their method contracts as them handles will be used in collections such asHashMapdata structures.- Parameters:
aContext- The context which is passed to the job (Undoable) instances when being executed.aHandleGenerator- TheHandleGeneratorto be used when generating unique handle objects.aExecutorService- TheExecutorServiceto be used, when null then anExecutorServicesomething likeControlFlowUtility.createCachedExecutorService(boolean)is then retrieved.
-
-
Method Detail
-
execute
public H execute(org.refcodes.command.Undoable<CTX,?,?> aJob)
Executes the given job. By double dispatch the job's execute method is invoked. The Job is provided with a service-bus to be able to perform its task. This is an implementation of the command pattern.
-
execute
public <RET,E extends java.lang.Exception> void execute(org.refcodes.command.Undoable<CTX,RET,E> aJob, java.util.function.BiConsumer<RET,E> aResultConsumer)
Executes the given job and invokes the providesBiConsumerlambda (closure) upon finished execution. The first parameter is passed in case we have an ordinary result, the second one is passed in case we have an exceptional situation. Either the one parameter or the other one is passed, never both of them at the same invocation! In case both arguments are null, then your job has returned null. Consider using theOptionalto be returned by your job. Though consider that theOptionalis NOT serializable which could cause problems with remote job execution! In case you do not have to take action upon an exceptional situation, then use theJobBus.execute(Undoable, Consumer)method.- Specified by:
executein interfaceJobBus<CTX,H>- Type Parameters:
RET- the generic typeE- the element type- Parameters:
aJob- The job to be executed.aResultConsumer- TheBiConsumerlambda to be invoked upon finished execution.- See Also:
- "http://www.refcodes.org/blog/obliged_to_do_the_optional_or_is_it_optional"
-
execute
public <RET,E extends java.lang.Exception> void execute(org.refcodes.command.Undoable<CTX,RET,E> aJob, java.util.function.Consumer<RET> aResultConsumer)
Executes the given job and invokes the providedConsumerlambda (closure) upon finished execution. The parameter is passed in case we have an ordinary result, an exceptual situation is "ignored" as theConsumerwill not be invoked in such a case. In case you have to take action upon an exceptional situation, then use theJobBus.execute(Undoable, BiConsumer)method.
-
getException
public <E extends java.lang.Exception> E getException(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.command.NotYetExecutedRuntimeException, org.refcodes.command.NoExceptionAvailableRuntimeException
Gets the exception.- Specified by:
getExceptionin interfaceJobBus<CTX,H>- Type Parameters:
E- the element type- Parameters:
aHandle- the handle- Returns:
- the exception
- Throws:
org.refcodes.component.UnknownHandleRuntimeException- the unknown handle runtime exceptionorg.refcodes.command.NotYetExecutedRuntimeException- the not yet executed runtime exceptionorg.refcodes.command.NoExceptionAvailableRuntimeException- the no exception available runtime exception
-
getResult
public <RET> RET getResult(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.command.NotYetExecutedRuntimeException, org.refcodes.command.NoResultAvailableRuntimeException
Gets the result.- Specified by:
getResultin interfaceJobBus<CTX,H>- Type Parameters:
RET- the generic type- Parameters:
aHandle- the handle- Returns:
- the result
- Throws:
org.refcodes.component.UnknownHandleRuntimeException- the unknown handle runtime exceptionorg.refcodes.command.NotYetExecutedRuntimeException- the not yet executed runtime exceptionorg.refcodes.command.NoResultAvailableRuntimeException- the no result available runtime exception
-
hasException
public boolean hasException(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.command.NotYetExecutedRuntimeException
Determines whether the job identified by the given handle ended with an exception (instead of a regular result). Most jobs do not have any exception, i.e. check if the job has been executed and also check if there is a result or an exception before requesting the according information.- Specified by:
hasExceptionin interfaceJobBus<CTX,H>- Parameters:
aHandle- The handle associated to the job in question.- Returns:
- The exception of the job's execution.
- Throws:
org.refcodes.component.UnknownHandleRuntimeException- in case the handle is not unknown.org.refcodes.command.NotYetExecutedRuntimeException- in case the job has not been executed yet.
-
hasResult
public boolean hasResult(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.command.NotYetExecutedRuntimeException
Determines whether the job identified by the given handle has a regular result (instead of an exception). There may be jobs which do not have any result, i.e. check if the job has been executed and also check if there is a result or an exception before requesting the according information.- Specified by:
hasResultin interfaceJobBus<CTX,H>- Parameters:
aHandle- The handle associated to the job in question.- Returns:
- The result of the job.
- Throws:
org.refcodes.component.UnknownHandleRuntimeException- in case the handle is not unknown.org.refcodes.command.NotYetExecutedRuntimeException- in case the job has not been executed yet
-
isExecuted
public boolean isExecuted(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
Determines whether the job has been executed.- Specified by:
isExecutedin interfaceJobBus<CTX,H>- Parameters:
aHandle- The handle associated to the job in question.- Returns:
- True if the job represented by the handle has been executed.
- Throws:
org.refcodes.component.UnknownHandleRuntimeException- in case the handle is not unknown.
-
waitForExecution
public void waitForExecution(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
Waits till the job identified by the given handle finished execution by regularly terminating or by throwing an exception. The handle my be used to determine if there was an exception or not and to retrieve the result or in case of an exception the exception object.- Specified by:
waitForExecutionin interfaceJobBus<CTX,H>- Parameters:
aHandle- The handle associated with the provided job.- Throws:
org.refcodes.component.UnknownHandleRuntimeException- in case the handle is not unknown.
-
waitForExecution
public void waitForExecution(H aHandle, long aTimeoutInMs) throws org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.component.HandleTimeoutRuntimeException
Waits till the job identified by the given handle finished execution by regularly terminating or by throwing an exception or till the timeout has been reached. In case the timeout has been reached before execution was finished, then aHandleTimeoutRuntimeExceptionis thrown to abort the waiting loop. This is especially useful when a remote connection is used to managed a distributedJobBusinfrastructure and due to connection issues, wait time would be endless. The handle my be used to determine if there was an exception or not and to retrieve the result or in case of an exception the exception object.- Specified by:
waitForExecutionin interfaceJobBus<CTX,H>- Parameters:
aHandle- The handle associated with the provided job.aTimeoutInMs- The timeout to wait at most even when execution did not finish. In case the timeout has been reached before execution was finished, then aHandleTimeoutRuntimeExceptionis thrown to abort the waiting loop.- Throws:
org.refcodes.component.UnknownHandleRuntimeException- Thrown in case the handle is not unknown.org.refcodes.component.HandleTimeoutRuntimeException- Thrown in case the timeout was reached before execution finished.
-
getResult
public <JOB extends org.refcodes.command.Undoable<CTX,RET,?>,RET> RET getResult(JOB aJob) throws org.refcodes.command.NoResultAvailableRuntimeException
Executes the job and waits for the job's result or an exception.- Specified by:
getResultin interfaceJobBus<CTX,H>- Type Parameters:
JOB- the generic typeRET- The return type of theUndoable's proceedings.- Parameters:
aJob- The job to execute- Returns:
- The result of the job execution. In case the job does not produce a result by default ("void"), then the according exception is thrown. Make sure the job produces a result!
- Throws:
org.refcodes.command.NoResultAvailableRuntimeException- in case a job has been executed which never delivers a result or which terminated with an exception. UseJobBus.hasException(Object)andJobBus.hasResult(Object)to clarify which state yourUndoable(job) is in.
-
getResult
public <JOB extends org.refcodes.command.Undoable<CTX,RET,?>,RET> RET getResult(JOB aJob, long aTimeoutInMs) throws org.refcodes.command.NoResultAvailableRuntimeException, org.refcodes.component.HandleTimeoutRuntimeException
Executes the job and waits for the job's result or an exception or till the timeout has been reached. In case the timeout has been reached before execution was finished, then aHandleTimeoutRuntimeExceptionis thrown to abort the waiting loop. This is especially useful when a remote connection is used to managed a distributedJobBusinfrastructure and due to connection issues, wait time would be endless.- Specified by:
getResultin interfaceJobBus<CTX,H>- Type Parameters:
JOB- the generic typeRET- The return type of theUndoable's proceedings.- Parameters:
aJob- The job to executeaTimeoutInMs- The timeout to wait at most even when execution did not finish. In case the timeout has been reached before execution was finished, then aHandleTimeoutRuntimeExceptionis thrown to abort the waiting loop.- Returns:
- The result of the job execution. In case the job does not produce a result by default ("void"), then the according exception is thrown. Make sure the job produces a result!
- Throws:
org.refcodes.command.NoResultAvailableRuntimeException- in case a job has been executed which never delivers a result or which terminated with an exception. UseJobBus.hasException(Object)andJobBus.hasResult(Object)to clarify which state yourUndoable(job) is in.org.refcodes.component.HandleTimeoutRuntimeException- the handle timeout runtime exception
-
hasHandle
public boolean hasHandle(H aHandle)
-
lookupHandle
public org.refcodes.command.Undoable<CTX,?,?> lookupHandle(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
-
removeHandle
public org.refcodes.command.Undoable<CTX,?,?> removeHandle(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
-
hasProgress
public boolean hasProgress(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
- Specified by:
hasProgressin interfaceorg.refcodes.component.ProgressHandle<CTX>- Throws:
org.refcodes.component.UnknownHandleRuntimeException
-
getProgress
public float getProgress(H aHandle) throws org.refcodes.component.UnsupportedHandleOperationRuntimeException, org.refcodes.component.UnknownHandleRuntimeException
- Specified by:
getProgressin interfaceorg.refcodes.component.ProgressHandle<CTX>- Throws:
org.refcodes.component.UnsupportedHandleOperationRuntimeExceptionorg.refcodes.component.UnknownHandleRuntimeException
-
hasReset
public boolean hasReset(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
- Specified by:
hasResetin interfaceorg.refcodes.component.ResetHandle<CTX>- Throws:
org.refcodes.component.UnknownHandleRuntimeException
-
reset
public void reset(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.component.UnsupportedHandleOperationRuntimeException
- Specified by:
resetin interfaceorg.refcodes.component.ResetHandle<CTX>- Throws:
org.refcodes.component.UnknownHandleRuntimeExceptionorg.refcodes.component.UnsupportedHandleOperationRuntimeException
-
hasFlush
public boolean hasFlush(H aHandle) throws org.refcodes.component.UnknownHandleRuntimeException
- Specified by:
hasFlushin interfaceorg.refcodes.component.FlushHandle<CTX>- Throws:
org.refcodes.component.UnknownHandleRuntimeException
-
flush
public void flush(H aHandle) throws org.refcodes.component.OpenException, org.refcodes.component.UnknownHandleRuntimeException, org.refcodes.component.UnsupportedHandleOperationRuntimeException
- Specified by:
flushin interfaceorg.refcodes.component.FlushHandle<CTX>- Throws:
org.refcodes.component.OpenExceptionorg.refcodes.component.UnknownHandleRuntimeExceptionorg.refcodes.component.UnsupportedHandleOperationRuntimeException
-
handleReferences
protected java.util.Collection<org.refcodes.command.Undoable<CTX,?,?>> handleReferences()
Retrieves the list of objects referenced by the handles.- Returns:
- A collection with the objects referenced by handles.
-
handles
protected java.util.Set<H> handles()
Returns the set of handles managed by this implementation.- Returns:
- The set containing all currently managed handles.
-
-