Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
88c693c6
Commit
88c693c6
authored
Jun 10, 2015
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Provide a way to disable the Restarter
Allow the Restarter to be disabled via a System property. Fixes gh-3173
parent
b5780219
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
10 deletions
+61
-10
RestartApplicationListener.java
...ork/boot/devtools/restart/RestartApplicationListener.java
+15
-1
Restarter.java
.../org/springframework/boot/devtools/restart/Restarter.java
+24
-0
RestartApplicationListenerTests.java
...oot/devtools/restart/RestartApplicationListenerTests.java
+22
-9
No files found.
spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java
View file @
88c693c6
...
...
@@ -35,10 +35,12 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
private
int
order
=
HIGHEST_PRECEDENCE
;
private
static
final
String
ENABLED_PROPERTY
=
"spring.devtools.restart.enabled"
;
@Override
public
void
onApplicationEvent
(
ApplicationEvent
event
)
{
if
(
event
instanceof
ApplicationStartedEvent
)
{
Restarter
.
initialize
(((
ApplicationStartedEvent
)
event
).
getArgs
()
);
onApplicationStartedEvent
((
ApplicationStartedEvent
)
event
);
}
if
(
event
instanceof
ApplicationReadyEvent
||
event
instanceof
ApplicationFailedEvent
)
{
...
...
@@ -46,6 +48,18 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
}
}
private
void
onApplicationStartedEvent
(
ApplicationStartedEvent
event
)
{
// It's too early to use the Spring environment but we should still allow
// users to disable restart using a System property.
String
enabled
=
System
.
getProperty
(
ENABLED_PROPERTY
);
if
(
enabled
==
null
||
Boolean
.
parseBoolean
(
enabled
))
{
Restarter
.
initialize
(
event
.
getArgs
());
}
else
{
Restarter
.
disable
();
}
}
@Override
public
int
getOrder
()
{
return
this
.
order
;
...
...
spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java
View file @
88c693c6
...
...
@@ -78,12 +78,16 @@ import org.springframework.util.ReflectionUtils;
*/
public
class
Restarter
{
private
static
final
String
[]
NO_ARGS
=
{};
private
static
Restarter
instance
;
private
Log
logger
=
new
DeferredLog
();
private
final
boolean
forceReferenceCleanup
;
private
boolean
enabled
=
true
;
private
URL
[]
initialUrls
;
private
final
String
mainClassName
;
...
...
@@ -185,6 +189,14 @@ public class Restarter {
}
}
/**
* Set if restart support is enabled.
* @param enabled if restart support is enabled
*/
private
void
setEnabled
(
boolean
enabled
)
{
this
.
enabled
=
false
;
}
/**
* Add additional URLs to be includes in the next restart.
* @param urls the urls to add
...
...
@@ -215,6 +227,10 @@ public class Restarter {
* Restart the running application.
*/
public
void
restart
()
{
if
(!
this
.
enabled
)
{
this
.
logger
.
debug
(
"Application restart is disabled"
);
return
;
}
this
.
logger
.
debug
(
"Restarting application"
);
getLeakSafeThread
().
call
(
new
Callable
<
Void
>()
{
...
...
@@ -402,6 +418,14 @@ public class Restarter {
return
this
.
initialUrls
;
}
/**
* Initialize and disable restart support.
*/
public
static
void
disable
()
{
initialize
(
NO_ARGS
,
false
,
RestartInitializer
.
NONE
);
getInstance
().
setEnabled
(
false
);
}
/**
* Initialize restart support. See
* {@link #initialize(String[], boolean, RestartInitializer)} for details.
...
...
spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java
View file @
88c693c6
...
...
@@ -23,8 +23,6 @@ import org.springframework.boot.SpringApplication;
import
org.springframework.boot.context.event.ApplicationFailedEvent
;
import
org.springframework.boot.context.event.ApplicationReadyEvent
;
import
org.springframework.boot.context.event.ApplicationStartedEvent
;
import
org.springframework.boot.devtools.restart.RestartApplicationListener
;
import
org.springframework.boot.devtools.restart.Restarter
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.core.Ordered
;
import
org.springframework.test.util.ReflectionTestUtils
;
...
...
@@ -42,10 +40,15 @@ import static org.mockito.Mockito.mock;
*/
public
class
RestartApplicationListenerTests
{
private
static
final
String
ENABLED_PROPERTY
=
"spring.devtools.restart.enabled"
;
private
static
final
String
[]
ARGS
=
new
String
[]
{
"a"
,
"b"
,
"c"
};
@Before
@After
public
void
cleanup
()
{
Restarter
.
clearInstance
();
System
.
clearProperty
(
ENABLED_PROPERTY
);
}
@Test
...
...
@@ -57,11 +60,25 @@ public class RestartApplicationListenerTests {
@Test
public
void
initializeWithReady
()
throws
Exception
{
testInitialize
(
false
);
assertThat
(
ReflectionTestUtils
.
getField
(
Restarter
.
getInstance
(),
"args"
),
equalTo
((
Object
)
ARGS
));
assertThat
(
Restarter
.
getInstance
().
isFinished
(),
equalTo
(
true
));
}
@Test
public
void
initializeWithFail
()
throws
Exception
{
testInitialize
(
true
);
assertThat
(
ReflectionTestUtils
.
getField
(
Restarter
.
getInstance
(),
"args"
),
equalTo
((
Object
)
ARGS
));
assertThat
(
Restarter
.
getInstance
().
isFinished
(),
equalTo
(
true
));
}
@Test
public
void
disableWithSystemProperty
()
throws
Exception
{
System
.
setProperty
(
ENABLED_PROPERTY
,
"false"
);
testInitialize
(
false
);
assertThat
(
ReflectionTestUtils
.
getField
(
Restarter
.
getInstance
(),
"enabled"
),
equalTo
((
Object
)
false
));
}
private
void
testInitialize
(
boolean
failed
)
{
...
...
@@ -69,21 +86,17 @@ public class RestartApplicationListenerTests {
RestartApplicationListener
listener
=
new
RestartApplicationListener
();
SpringApplication
application
=
new
SpringApplication
();
ConfigurableApplicationContext
context
=
mock
(
ConfigurableApplicationContext
.
class
);
String
[]
args
=
new
String
[]
{
"a"
,
"b"
,
"c"
};
listener
.
onApplicationEvent
(
new
ApplicationStartedEvent
(
application
,
args
));
listener
.
onApplicationEvent
(
new
ApplicationStartedEvent
(
application
,
ARGS
));
assertThat
(
Restarter
.
getInstance
(),
not
(
nullValue
()));
assertThat
(
Restarter
.
getInstance
().
isFinished
(),
equalTo
(
false
));
assertThat
(
ReflectionTestUtils
.
getField
(
Restarter
.
getInstance
(),
"args"
),
equalTo
((
Object
)
args
));
if
(
failed
)
{
listener
.
onApplicationEvent
(
new
ApplicationFailedEvent
(
application
,
args
,
listener
.
onApplicationEvent
(
new
ApplicationFailedEvent
(
application
,
ARGS
,
context
,
new
RuntimeException
()));
}
else
{
listener
.
onApplicationEvent
(
new
ApplicationReadyEvent
(
application
,
args
,
listener
.
onApplicationEvent
(
new
ApplicationReadyEvent
(
application
,
ARGS
,
context
));
}
assertThat
(
Restarter
.
getInstance
().
isFinished
(),
equalTo
(
true
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment