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
8e2a6eec
Commit
8e2a6eec
authored
Dec 23, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add database query defaults in SimpleHealthManager
parent
063403a0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
12 deletions
+35
-12
SimpleHealthIndicator.java
...gframework/boot/actuate/health/SimpleHealthIndicator.java
+35
-11
SimpleHealthIndicatorTests.java
...ework/boot/actuate/health/SimpleHealthIndicatorTests.java
+0
-1
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java
View file @
8e2a6eec
...
@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.health;
...
@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.health;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -37,32 +38,44 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
...
@@ -37,32 +38,44 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
private
JdbcTemplate
jdbcTemplate
;
private
JdbcTemplate
jdbcTemplate
;
private
String
query
=
"SELECT 'Hello'"
;
private
static
Map
<
String
,
String
>
queries
=
new
HashMap
<
String
,
String
>();
static
{
queries
.
put
(
"HSQL Database Engine"
,
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS"
);
queries
.
put
(
"Oracle"
,
"SELECT 'Hello' from DUAL"
);
queries
.
put
(
"Apache Derby"
,
"SELECT 1 FROM SYSIBM.SYSDUMMY1"
);
}
private
static
String
DEFAULT_QUERY
=
"SELECT 'Hello'"
;
private
String
query
=
null
;
@Override
@Override
public
Map
<
String
,
Object
>
health
()
{
public
Map
<
String
,
Object
>
health
()
{
LinkedHashMap
<
String
,
Object
>
map
=
new
LinkedHashMap
<
String
,
Object
>();
LinkedHashMap
<
String
,
Object
>
map
=
new
LinkedHashMap
<
String
,
Object
>();
map
.
put
(
"status"
,
"ok"
);
map
.
put
(
"status"
,
"ok"
);
String
product
=
"unknown"
;
if
(
this
.
dataSource
!=
null
)
{
if
(
this
.
dataSource
!=
null
)
{
try
{
try
{
String
product
=
this
.
jdbcTemplate
product
=
this
.
jdbcTemplate
.
execute
(
new
ConnectionCallback
<
String
>()
{
.
execute
(
new
ConnectionCallback
<
String
>()
{
@Override
@Override
public
String
doInConnection
(
Connection
connection
)
public
String
doInConnection
(
Connection
connection
)
throws
SQLException
,
DataAccessException
{
throws
SQLException
,
DataAccessException
{
return
connection
.
getMetaData
().
getDatabaseProductName
();
return
connection
.
getMetaData
().
getDatabaseProductName
();
}
}
});
});
map
.
put
(
"database"
,
product
);
map
.
put
(
"database"
,
product
);
}
}
catch
(
DataAccessException
ex
)
{
catch
(
DataAccessException
ex
)
{
map
.
put
(
"status"
,
"error"
);
map
.
put
(
"status"
,
"error"
);
map
.
put
(
"error"
,
ex
.
getClass
().
getName
()
+
": "
+
ex
.
getMessage
());
map
.
put
(
"error"
,
ex
.
getClass
().
getName
()
+
": "
+
ex
.
getMessage
());
}
}
if
(
StringUtils
.
hasText
(
this
.
query
))
{
String
query
=
detectQuery
(
product
);
if
(
StringUtils
.
hasText
(
query
))
{
try
{
try
{
map
.
put
(
"hello"
,
map
.
put
(
"hello"
,
this
.
jdbcTemplate
.
queryForObject
(
this
.
query
,
String
.
class
));
this
.
jdbcTemplate
.
queryForObject
(
query
,
String
.
class
));
}
}
catch
(
Exception
ex
)
{
catch
(
Exception
ex
)
{
map
.
put
(
"status"
,
"error"
);
map
.
put
(
"status"
,
"error"
);
...
@@ -73,6 +86,17 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
...
@@ -73,6 +86,17 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
return
map
;
return
map
;
}
}
protected
String
detectQuery
(
String
product
)
{
String
query
=
this
.
query
;
if
(!
StringUtils
.
hasText
(
query
))
{
query
=
queries
.
get
(
product
);
}
if
(!
StringUtils
.
hasText
(
query
))
{
query
=
DEFAULT_QUERY
;
}
return
query
;
}
public
void
setDataSource
(
DataSource
dataSource
)
{
public
void
setDataSource
(
DataSource
dataSource
)
{
this
.
dataSource
=
dataSource
;
this
.
dataSource
=
dataSource
;
this
.
jdbcTemplate
=
new
JdbcTemplate
(
dataSource
);
this
.
jdbcTemplate
=
new
JdbcTemplate
(
dataSource
);
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/SimpleHealthIndicatorTests.java
View file @
8e2a6eec
...
@@ -51,7 +51,6 @@ public class SimpleHealthIndicatorTests {
...
@@ -51,7 +51,6 @@ public class SimpleHealthIndicatorTests {
@Test
@Test
public
void
database
()
{
public
void
database
()
{
this
.
indicator
.
setDataSource
(
this
.
dataSource
);
this
.
indicator
.
setDataSource
(
this
.
dataSource
);
this
.
indicator
.
setQuery
(
"SELECT count(*) FROM INFORMATION_SCHEMA.SYSTEM_TABLES"
);
Map
<
String
,
Object
>
health
=
this
.
indicator
.
health
();
Map
<
String
,
Object
>
health
=
this
.
indicator
.
health
();
assertNotNull
(
health
.
get
(
"database"
));
assertNotNull
(
health
.
get
(
"database"
));
assertNotNull
(
health
.
get
(
"hello"
));
assertNotNull
(
health
.
get
(
"hello"
));
...
...
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