Class AbstractAsyncFactoryBean<T>
- java.lang.Object
-
- ubic.gemma.persistence.util.AbstractAsyncFactoryBean<T>
-
- Type Parameters:
T- type of bean that this factory provides
- All Implemented Interfaces:
DisposableBean,FactoryBean<Future<T>>,AsyncFactoryBean<T>
- Direct Known Subclasses:
HomologeneServiceFactory
public abstract class AbstractAsyncFactoryBean<T> extends Object implements AsyncFactoryBean<T>, DisposableBean
Base implementation ofAsyncFactoryBean.For singleton beans, this implementation holds the singleton using a
Future. Thus, thecreateObject()does not need to actually use a singleton pattern as it is guaranteed to be called only once whenFactoryBean.isSingleton()is true.The implementation initializes beans using a
ExecutorServicewhich can be configured viaAbstractAsyncFactoryBean(ExecutorService). The default executor is single-threaded, which is suitable for singleton beans.This implementation handles destruction of the factory when the context is closed by dispatching a
Future.cancel(boolean)on any pending bean creation.The
getObject()is thread-safe and will appropriately handle multiple threads attempting to create a singleton bean or coordinate multiple pending beans.- Author:
- poirigui
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractAsyncFactoryBean()protectedAbstractAsyncFactoryBean(ExecutorService executor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract TcreateObject()Create a new bean as pergetObject().voiddestroy()Future<T>getObject()Obtain a bean asynchronously.Class<?>getObjectType()booleanisInitialized()Indicate if this bean has been initialized or if its initialization is pending.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.springframework.beans.factory.FactoryBean
isSingleton
-
-
-
-
Constructor Detail
-
AbstractAsyncFactoryBean
protected AbstractAsyncFactoryBean()
-
AbstractAsyncFactoryBean
protected AbstractAsyncFactoryBean(ExecutorService executor)
-
-
Method Detail
-
getObject
public final Future<T> getObject()
Description copied from interface:AsyncFactoryBeanObtain a bean asynchronously.- Specified by:
getObjectin interfaceAsyncFactoryBean<T>- Specified by:
getObjectin interfaceFactoryBean<T>- Returns:
- a future that completes when the bean is ready
-
getObjectType
public final Class<?> getObjectType()
- Specified by:
getObjectTypein interfaceFactoryBean<T>
-
createObject
protected abstract T createObject() throws Exception
Create a new bean as pergetObject().The implementation of a singleton bean does not need to use the singleton pattern as this implementation already guarantees it.
- Throws:
Exception
-
isInitialized
public final boolean isInitialized()
Description copied from interface:AsyncFactoryBeanIndicate if this bean has been initialized or if its initialization is pending.This is only meaningful if
FactoryBean.isSingleton()is true as otherwise a new, yet to be initialized bean will be returned everytime and this method will always return false.- Specified by:
isInitializedin interfaceAsyncFactoryBean<T>
-
destroy
public final void destroy()
- Specified by:
destroyin interfaceDisposableBean
-
-