Class Restarter
java.lang.Object
org.springframework.boot.devtools.restart.Restarter
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 the RestartApplicationListener
can be
relied upon to perform initialization, however, you may need to call
initialize(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 "uber 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
- Author:
- Phillip Webb, Andy Wilkinson
- See Also:
-
Constructor Summary
ModifierConstructorDescriptionprotected
Restarter
(Thread thread, String[] args, boolean forceReferenceCleanup, RestartInitializer initializer) Internal constructor to create a newRestarter
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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
Clear the instance.static void
disable()
Initialize and disable restart support.URL[]
Return the initial set of URLs as configured by theRestartInitializer
.static Restarter
Return the activeRestarter
instance.getOrAddAttribute
(String name, ObjectFactory<?> objectFactory) 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.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 Details
-
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:
-
-
Method Details
-
initialize
protected void initialize(boolean restartOnInitialize) -
addUrls
Add additional URLs to be includes in the next restart.- Parameters:
urls
- the urls to add
-
addClassLoaderFiles
Add additionalClassLoaderFiles
to be included in the next restart.- Parameters:
classLoaderFiles
- the files to add
-
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
Restart the running application.- Parameters:
failureHandler
- a failure handler to deal with application that doesn't start
-
start
Start the application.- Parameters:
failureHandler
- a failure handler for application that won't start- Throws:
Exception
- in case of errors
-
relaunch
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
-
stop
Stop the application.- Throws:
Exception
- in case of errors
-
getOrAddAttribute
-
removeAttribute
-
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
Initialize restart support. Seeinitialize(String[], boolean, RestartInitializer)
for details.- Parameters:
args
- main application arguments- See Also:
-
initialize
Initialize restart support. Seeinitialize(String[], boolean, RestartInitializer)
for details.- Parameters:
args
- main application argumentsinitializer
- the restart initializer- See Also:
-
initialize
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
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
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
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.
-