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
bf8051cd
Commit
bf8051cd
authored
Apr 13, 2021
by
Scott Frederick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Log ApplicationAvailability state changes
Fixes gh-23098
parent
5f957668
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
ApplicationAvailabilityBean.java
...mework/boot/availability/ApplicationAvailabilityBean.java
+27
-0
ApplicationAvailabilityBeanTests.java
...k/boot/availability/ApplicationAvailabilityBeanTests.java
+32
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityBean.java
View file @
bf8051cd
...
...
@@ -19,6 +19,10 @@ package org.springframework.boot.availability;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.util.Assert
;
...
...
@@ -34,6 +38,8 @@ import org.springframework.util.Assert;
public
class
ApplicationAvailabilityBean
implements
ApplicationAvailability
,
ApplicationListener
<
AvailabilityChangeEvent
<?>>
{
private
static
final
Log
logger
=
LogFactory
.
getLog
(
ApplicationAvailability
.
class
);
private
final
Map
<
Class
<?
extends
AvailabilityState
>,
AvailabilityChangeEvent
<?>>
events
=
new
HashMap
<>();
@Override
...
...
@@ -58,10 +64,31 @@ public class ApplicationAvailabilityBean
@Override
public
void
onApplicationEvent
(
AvailabilityChangeEvent
<?>
event
)
{
logStateChange
(
event
);
Class
<?
extends
AvailabilityState
>
stateType
=
getStateType
(
event
.
getState
());
this
.
events
.
put
(
stateType
,
event
);
}
private
void
logStateChange
(
AvailabilityChangeEvent
<?>
event
)
{
Class
<?
extends
AvailabilityState
>
stateType
=
getStateType
(
event
.
getState
());
StringBuilder
message
=
new
StringBuilder
(
"Application availability state "
+
stateType
.
getSimpleName
()
+
" changed"
);
AvailabilityChangeEvent
<?
extends
AvailabilityState
>
lastChangeEvent
=
getLastChangeEvent
(
stateType
);
if
(
lastChangeEvent
!=
null
)
{
message
.
append
(
" from "
+
lastChangeEvent
.
getState
());
}
message
.
append
(
" to "
+
event
.
getState
());
if
(
event
.
getSource
()
!=
null
)
{
if
(
event
.
getSource
()
instanceof
Throwable
)
{
message
.
append
(
": "
+
event
.
getSource
());
}
else
if
(!(
event
.
getSource
()
instanceof
ApplicationEventPublisher
))
{
message
.
append
(
": "
+
event
.
getSource
().
getClass
().
getName
());
}
}
logger
.
info
(
message
);
}
@SuppressWarnings
(
"unchecked"
)
private
Class
<?
extends
AvailabilityState
>
getStateType
(
AvailabilityState
state
)
{
if
(
state
instanceof
Enum
)
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityBeanTests.java
View file @
bf8051cd
...
...
@@ -16,9 +16,14 @@
package
org
.
springframework
.
boot
.
availability
;
import
java.io.IOException
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.springframework.boot.testsupport.system.CapturedOutput
;
import
org.springframework.boot.testsupport.system.OutputCaptureExtension
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -29,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Brian Clozel
* @author Phillip Webb
*/
@ExtendWith
(
OutputCaptureExtension
.
class
)
class
ApplicationAvailabilityBeanTests
{
private
AnnotationConfigApplicationContext
context
;
...
...
@@ -87,6 +93,28 @@ class ApplicationAvailabilityBeanTests {
assertThat
(
this
.
availability
.
getLastChangeEvent
(
TestState
.
class
)).
isNotNull
();
}
@Test
void
stateChangesAreLogged
(
CapturedOutput
output
)
{
AvailabilityChangeEvent
.
publish
(
this
.
context
,
LivenessState
.
CORRECT
);
assertThat
(
output
).
contains
(
"Application availability state LivenessState changed to CORRECT\n"
);
AvailabilityChangeEvent
.
publish
(
this
.
context
,
LivenessState
.
BROKEN
);
assertThat
(
output
).
contains
(
"Application availability state LivenessState changed from CORRECT to BROKEN\n"
);
}
@Test
void
stateChangesAreLoggedWithExceptionSource
(
CapturedOutput
output
)
{
AvailabilityChangeEvent
.
publish
(
this
.
context
,
new
IOException
(
"connection error"
),
LivenessState
.
BROKEN
);
assertThat
(
output
).
contains
(
"Application availability state LivenessState changed to BROKEN: "
+
"java.io.IOException: connection error\n"
);
}
@Test
void
stateChangesAreLoggedWithOtherSource
(
CapturedOutput
output
)
{
AvailabilityChangeEvent
.
publish
(
this
.
context
,
new
CustomEventSource
(),
LivenessState
.
BROKEN
);
assertThat
(
output
).
contains
(
"Application availability state LivenessState changed to BROKEN: "
+
CustomEventSource
.
class
.
getName
()
+
"\n"
);
}
enum
TestState
implements
AvailabilityState
{
ONE
{
...
...
@@ -107,4 +135,8 @@ class ApplicationAvailabilityBeanTests {
}
static
class
CustomEventSource
{
}
}
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