Class AbstractAsyncFactoryBean<T>
- java.lang.Object
-
- ubic.gemma.core.context.AbstractAsyncFactoryBean<T>
-
- Type Parameters:
T
- type of bean that this factory provides
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean
,org.springframework.beans.factory.FactoryBean<Future<T>>
,AsyncFactoryBean<T>
- Direct Known Subclasses:
HomologeneServiceFactory
public abstract class AbstractAsyncFactoryBean<T> extends Object implements AsyncFactoryBean<T>, org.springframework.beans.factory.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
ExecutorService
which 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 protected
AbstractAsyncFactoryBean()
protected
AbstractAsyncFactoryBean(ExecutorService executor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract T
createObject()
Create a new bean as pergetObject()
.void
destroy()
Future<T>
getObject()
Obtain a bean asynchronously.Class<?>
getObjectType()
boolean
isInitialized()
Indicate if this bean has been initialized or if its initialization is pending.
-
-
-
Constructor Detail
-
AbstractAsyncFactoryBean
protected AbstractAsyncFactoryBean()
-
AbstractAsyncFactoryBean
protected AbstractAsyncFactoryBean(ExecutorService executor)
-
-
Method Detail
-
getObject
public final Future<T> getObject()
Description copied from interface:AsyncFactoryBean
Obtain a bean asynchronously.- Specified by:
getObject
in interfaceAsyncFactoryBean<T>
- Specified by:
getObject
in interfaceorg.springframework.beans.factory.FactoryBean<T>
- Returns:
- a future that completes when the bean is ready
-
getObjectType
public final Class<?> getObjectType()
- Specified by:
getObjectType
in interfaceorg.springframework.beans.factory.FactoryBean<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:AsyncFactoryBean
Indicate 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:
isInitialized
in interfaceAsyncFactoryBean<T>
-
destroy
public final void destroy()
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
-
-