Class Restarter
- java.lang.Object
- org.springframework.boot.devtools.restart.Restarter
public class Restarter extends Object
Allows a running application to be restarted with an updated classpath. The restarter works by creating a new application ClassLoader that is split into two parts. The top part contains static URLs that don't change (for example 3rd party libraries and Spring Boot itself) and the bottom part contains URLs where classes and resources might be updated.The Restarter should be
initialized
early to ensure that classes are loaded multiple times. Mostly theRestartApplicationListener
can be relied upon to perform initialization, however, you may need to callinitialize(String[])
directly if your SpringApplication arguments are not identical to your main method arguments.By default, applications running in an IDE (i.e. those not packaged as "fat jars") will automatically detect URLs that can change. It's also possible to manually configure URLs or class file updates for remote restart scenarios.
- Since:
- 1.3.0
- See Also:
RestartApplicationListener
,initialize(String[])
,getInstance()
,restart()
Constructor Summary
Constructors Modifier Constructor Description protected
Restarter(Thread thread, String[] args, boolean forceReferenceCleanup, RestartInitializer initializer)
Internal constructor to create a newRestarter
instance.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addClassLoaderFiles(ClassLoaderFiles classLoaderFiles)
Add additionalClassLoaderFiles
to be included in the next restart.void
addUrls(Collection<URL> urls)
Add additional URLs to be includes in the next restart.static void
clearInstance()
Clear the instance.static void
disable()
Initialize and disable restart support.URL[]
getInitialUrls()
Return the initial set of URLs as configured by theRestartInitializer
.static Restarter
getInstance()
Return the activeRestarter
instance.Object
getOrAddAttribute(String name, org.springframework.beans.factory.ObjectFactory<?> objectFactory)
ThreadFactory
getThreadFactory()
Return aThreadFactory
that can be used to create leak safe threads.protected void
initialize(boolean restartOnInitialize)
static void
initialize(String[] args)
Initialize restart support.static void
initialize(String[] args, boolean forceReferenceCleanup)
Initialize restart support.static void
initialize(String[] args, boolean forceReferenceCleanup, RestartInitializer initializer)
Initialize restart support.static void
initialize(String[] args, boolean forceReferenceCleanup, RestartInitializer initializer, boolean restartOnInitialize)
Initialize restart support for the current application.static void
initialize(String[] args, RestartInitializer initializer)
Initialize restart support.protected Throwable
relaunch(ClassLoader classLoader)
Relaunch the application using the specified classloader.Object
removeAttribute(String name)
void
restart()
Restart the running application.void
restart(FailureHandler failureHandler)
Restart the running application.protected void
start(FailureHandler failureHandler)
Start the application.protected void
stop()
Stop the application.
Constructor Detail
Restarter
protected Restarter(Thread thread, String[] args, boolean forceReferenceCleanup, RestartInitializer initializer)
Internal constructor to create a newRestarter
instance.- Parameters:
thread
- the source threadargs
- the application argumentsforceReferenceCleanup
- if soft/weak reference cleanup should be forcedinitializer
- the restart initializer- See Also:
initialize(String[])
Method Detail
initialize
protected void initialize(boolean restartOnInitialize)
addUrls
public void addUrls(Collection<URL> urls)
Add additional URLs to be includes in the next restart.- Parameters:
urls
- the urls to add
addClassLoaderFiles
public void addClassLoaderFiles(ClassLoaderFiles classLoaderFiles)
Add additionalClassLoaderFiles
to be included in the next restart.- Parameters:
classLoaderFiles
- the files to add
getThreadFactory
public ThreadFactory getThreadFactory()
Return aThreadFactory
that can be used to create leak safe threads.- Returns:
- a leak safe thread factory
restart
public void restart()
Restart the running application.
restart
public void restart(FailureHandler failureHandler)
Restart the running application.- Parameters:
failureHandler
- a failure handler to deal with application that doesn't start
start
protected void start(FailureHandler failureHandler) throws Exception
Start the application.- Parameters:
failureHandler
- a failure handler for application that won't start- Throws:
Exception
- in case of errors
relaunch
protected Throwable relaunch(ClassLoader classLoader) throws Exception
Relaunch the application using the specified classloader.- Parameters:
classLoader
- the classloader to use- Returns:
- any exception that caused the launch to fail or
null
- Throws:
Exception
- in case of errors
getOrAddAttribute
public Object getOrAddAttribute(String name, org.springframework.beans.factory.ObjectFactory<?> objectFactory)
removeAttribute
public Object removeAttribute(String name)
getInitialUrls
public URL[] getInitialUrls()
Return the initial set of URLs as configured by theRestartInitializer
.- Returns:
- the initial URLs or
null
disable
public static void disable()
Initialize and disable restart support.
initialize
public static void initialize(String[] args)
Initialize restart support. Seeinitialize(String[], boolean, RestartInitializer)
for details.- Parameters:
args
- main application arguments- See Also:
initialize(String[], boolean, RestartInitializer)
initialize
public static void initialize(String[] args, RestartInitializer initializer)
Initialize restart support. Seeinitialize(String[], boolean, RestartInitializer)
for details.- Parameters:
args
- main application argumentsinitializer
- the restart initializer- See Also:
initialize(String[], boolean, RestartInitializer)
initialize
public static void initialize(String[] args, boolean forceReferenceCleanup)
Initialize restart support. Seeinitialize(String[], boolean, RestartInitializer)
for details.- Parameters:
args
- main application argumentsforceReferenceCleanup
- if forcing of soft/weak reference should happen on- See Also:
initialize(String[], boolean, RestartInitializer)
initialize
public static void initialize(String[] args, boolean forceReferenceCleanup, RestartInitializer initializer)
Initialize restart support. Seeinitialize(String[], boolean, RestartInitializer, boolean)
for details.- Parameters:
args
- main application argumentsforceReferenceCleanup
- if forcing of soft/weak reference should happen oninitializer
- the restart initializer- See Also:
initialize(String[], boolean, RestartInitializer)
initialize
public static void initialize(String[] args, boolean forceReferenceCleanup, RestartInitializer initializer, boolean restartOnInitialize)
Initialize restart support for the current application. Called automatically byRestartApplicationListener
but can also be called directly if main application arguments are not the same as those passed to theSpringApplication
.- Parameters:
args
- main application argumentsforceReferenceCleanup
- if forcing of soft/weak reference should happen on each restart. This will slow down restarts and is intended primarily for testinginitializer
- the restart initializerrestartOnInitialize
- if the restarter should be restarted immediately when theRestartInitializer
returns nonnull
results
getInstance
public static Restarter getInstance()
Return the activeRestarter
instance. Cannot be called beforeinitialization
.- Returns:
- the restarter
clearInstance
public static void clearInstance()
Clear the instance. Primarily provided for tests and not usually used in application code.