T
- the class of the inner object that is stored in this containerpublic class FileReloadContainer<T> extends Object
Provides a container for returning an object and reloading the object if an underlying file has changed. Ideally suited for automatically reloading configuration files that might be edited outside of the application.
There are two constructors:FileReloadContainer(T, Resource, FileReloadCallback<T>)
is used for objects having an underlying resource and are reloadableFileReloadContainer(T)
is used
for objects that either do not have an underlying file or are otherwise
not reloadable
If the first constructor is used, the Resource will be stored for later
reloading. If Resource.getFile()
does not
throw an exception, the returned File object will be stored and
File.lastModified()
will be called every time
the getObject()
method is called to see if the file
has changed. If the file has changed, the last modified time is updated
and the reload callback, FileReloadCallback.reload
, is called. If it returns a non-null object,
the new object is stored and it gets returned to the caller. If a null
object is returned, the stored object isn't modified and the old object
is returned to the caller.
If an unchecked exception is thrown by the reload callback, it will be
caught, logged, and a DataAccessResourceFailureException
with
a cause of the unchecked exception. This will propogate up to the
caller of the getObject method. If you do not want unchecked exceptions
on reloads to propogate up to the caller of getObject, they need to be
caught within the reload method. Returning a null in the case of errors
is a good alternative in this case.
Constructor and Description |
---|
FileReloadContainer(File file,
FileReloadCallback<T> callback) |
FileReloadContainer(T object)
Creates a new container with an object which has no underlying file.
|
FileReloadContainer(T object,
org.springframework.core.io.Resource resource,
FileReloadCallback<T> callback)
Creates a new container with an object and a file underlying that
object.
|
Modifier and Type | Method and Description |
---|---|
File |
getFile()
Get the file underlying the object in this container, if any.
|
long |
getLastUpdate()
Get the timestamp in milliseconds of the last time the file was reloaded.
|
T |
getObject()
Get the object in this container.
|
long |
getReloadCheckInterval()
Get the reload check interval.
|
void |
reload()
Force a reload of the configuration.
|
void |
setReloadCheckInterval(long reloadCheckInterval)
Set the reload check interval.
|
public FileReloadContainer(T object, org.springframework.core.io.Resource resource, FileReloadCallback<T> callback)
reload
on the callback will be called when the file is modified. The
check will be performed when getObject()
is called and
at least reloadCheckInterval milliseconds have passed.object
- object to be stored in this containercallback
- reload
will be called when the underlying file object is modifiedresource
- a Resource
object.IllegalArgumentException
- if object, file, or callback are nullpublic FileReloadContainer(File file, FileReloadCallback<T> callback)
public FileReloadContainer(T object)
object
- object to be stored in this containerIllegalArgumentException
- if object is nullpublic T getObject() throws org.springframework.dao.DataAccessResourceFailureException
org.springframework.dao.DataAccessResourceFailureException
- if an unchecked exception
is received while trying to reload the object from the underlying filepublic void reload()
public File getFile()
public long getReloadCheckInterval()
reload()
is explicitly called.public void setReloadCheckInterval(long reloadCheckInterval)
reloadCheckInterval
- reload check interval in milliseconds. A negative value
indicates that automatic reload checks are not performed and the
file will only be reloaded if reload()
is explicitly called.public long getLastUpdate()
Copyright © 2021. All rights reserved.