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
50eedefe
Commit
50eedefe
authored
Jun 05, 2015
by
Eddú Meléndez
Committed by
Stephane Nicoll
Jun 06, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add connection timeout property for redis
See gh-3142
parent
98b6fafe
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
26 deletions
+61
-26
RedisAutoConfiguration.java
...work/boot/autoconfigure/redis/RedisAutoConfiguration.java
+4
-0
RedisProperties.java
...ngframework/boot/autoconfigure/redis/RedisProperties.java
+14
-0
RedisAutoConfigurationTests.java
...boot/autoconfigure/redis/RedisAutoConfigurationTests.java
+43
-26
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java
View file @
50eedefe
...
@@ -52,6 +52,7 @@ import redis.clients.jedis.JedisPoolConfig;
...
@@ -52,6 +52,7 @@ import redis.clients.jedis.JedisPoolConfig;
* @author Christian Dupuis
* @author Christian Dupuis
* @author Christoph Strobl
* @author Christoph Strobl
* @author Phillip Webb
* @author Phillip Webb
* @author Eddú Meléndez
*/
*/
@Configuration
@Configuration
@ConditionalOnClass
({
JedisConnection
.
class
,
RedisOperations
.
class
,
Jedis
.
class
})
@ConditionalOnClass
({
JedisConnection
.
class
,
RedisOperations
.
class
,
Jedis
.
class
})
...
@@ -83,6 +84,9 @@ public class RedisAutoConfiguration {
...
@@ -83,6 +84,9 @@ public class RedisAutoConfiguration {
factory
.
setPassword
(
this
.
properties
.
getPassword
());
factory
.
setPassword
(
this
.
properties
.
getPassword
());
}
}
factory
.
setDatabase
(
this
.
properties
.
getDatabase
());
factory
.
setDatabase
(
this
.
properties
.
getDatabase
());
if
(
this
.
properties
.
getTimeout
()
>
0
)
{
factory
.
setTimeout
(
this
.
properties
.
getTimeout
());
}
return
factory
;
return
factory
;
}
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisProperties.java
View file @
50eedefe
...
@@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
...
@@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*
*
* @author Dave Syer
* @author Dave Syer
* @author Christoph Strobl
* @author Christoph Strobl
* @author Eddú Meléndez
*/
*/
@ConfigurationProperties
(
prefix
=
"spring.redis"
)
@ConfigurationProperties
(
prefix
=
"spring.redis"
)
public
class
RedisProperties
{
public
class
RedisProperties
{
...
@@ -51,6 +52,11 @@ public class RedisProperties {
...
@@ -51,6 +52,11 @@ public class RedisProperties {
private
Sentinel
sentinel
;
private
Sentinel
sentinel
;
/**
* Timeout to set in milliseconds.
*/
private
int
timeout
;
public
String
getHost
()
{
public
String
getHost
()
{
return
this
.
host
;
return
this
.
host
;
}
}
...
@@ -95,6 +101,14 @@ public class RedisProperties {
...
@@ -95,6 +101,14 @@ public class RedisProperties {
return
this
.
sentinel
;
return
this
.
sentinel
;
}
}
public
void
setTimeout
(
int
timeout
)
{
this
.
timeout
=
timeout
;
}
public
int
getTimeout
()
{
return
this
.
timeout
;
}
public
void
setSentinel
(
Sentinel
sentinel
)
{
public
void
setSentinel
(
Sentinel
sentinel
)
{
this
.
sentinel
=
sentinel
;
this
.
sentinel
=
sentinel
;
}
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java
View file @
50eedefe
...
@@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.redis;
...
@@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.redis;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.List
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
...
@@ -40,29 +42,34 @@ import static org.junit.Assert.assertTrue;
...
@@ -40,29 +42,34 @@ import static org.junit.Assert.assertTrue;
* @author Dave Syer
* @author Dave Syer
* @author Christian Dupuis
* @author Christian Dupuis
* @author Christoph Strobl
* @author Christoph Strobl
* @author Eddú Meléndez
*/
*/
public
class
RedisAutoConfigurationTests
{
public
class
RedisAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
private
AnnotationConfigApplicationContext
context
;
@Before
public
void
setup
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
}
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
@Test
public
void
testDefaultRedisConfiguration
()
throws
Exception
{
public
void
testDefaultRedisConfiguration
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
load
();
this
.
context
.
register
(
RedisAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertNotNull
(
this
.
context
.
getBean
(
"redisTemplate"
,
RedisOperations
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
"redisTemplate"
,
RedisOperations
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
StringRedisTemplate
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
StringRedisTemplate
.
class
));
}
}
@Test
@Test
public
void
testOverrideRedisConfiguration
()
throws
Exception
{
public
void
testOverrideRedisConfiguration
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
load
(
"spring.redis.host:foo"
,
"spring.redis.database:1"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.redis.host:foo"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.redis.database:1"
);
this
.
context
.
register
(
RedisAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertEquals
(
"foo"
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
assertEquals
(
"foo"
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
.
getHostName
());
.
getHostName
());
assertEquals
(
1
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
).
getDatabase
());
assertEquals
(
1
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
).
getDatabase
());
...
@@ -70,33 +77,29 @@ public class RedisAutoConfigurationTests {
...
@@ -70,33 +77,29 @@ public class RedisAutoConfigurationTests {
@Test
@Test
public
void
testRedisConfigurationWithPool
()
throws
Exception
{
public
void
testRedisConfigurationWithPool
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
load
(
"spring.redis.host:foo"
,
"spring.redis.pool.max-idle:1"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.redis.host:foo"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.redis.pool.max-idle:1"
);
this
.
context
.
register
(
RedisAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertEquals
(
"foo"
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
assertEquals
(
"foo"
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
.
getHostName
());
.
getHostName
());
assertEquals
(
1
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
assertEquals
(
1
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
.
getPoolConfig
().
getMaxIdle
());
.
getPoolConfig
().
getMaxIdle
());
}
}
@Test
public
void
testRedisConfigurationWithTimeout
()
throws
Exception
{
load
(
"spring.redis.host:foo"
,
"spring.redis.timeout:100"
);
assertEquals
(
"foo"
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
.
getHostName
());
assertEquals
(
100
,
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
.
getTimeout
());
}
@Test
@Test
public
void
testRedisConfigurationWithSentinel
()
throws
Exception
{
public
void
testRedisConfigurationWithSentinel
()
throws
Exception
{
List
<
String
>
sentinels
=
Arrays
.
asList
(
"127.0.0.1:26379"
,
"127.0.0.1:26380"
);
List
<
String
>
sentinels
=
Arrays
.
asList
(
"127.0.0.1:26379"
,
"127.0.0.1:26380"
);
if
(
isAtLeastOneSentinelAvailable
(
sentinels
))
{
if
(
isAtLeastOneSentinelAvailable
(
sentinels
))
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
load
(
"spring.redis.sentinel.master:mymaster"
,
"spring.redis.sentinel.nodes:"
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
+
StringUtils
.
collectionToCommaDelimitedString
(
sentinels
));
"spring.redis.sentinel.master:mymaster"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.redis.sentinel.nodes:"
+
StringUtils
.
collectionToCommaDelimitedString
(
sentinels
));
this
.
context
.
register
(
RedisAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertTrue
(
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
assertTrue
(
this
.
context
.
getBean
(
JedisConnectionFactory
.
class
)
.
isRedisSentinelAware
());
.
isRedisSentinelAware
());
...
@@ -138,4 +141,18 @@ public class RedisAutoConfigurationTests {
...
@@ -138,4 +141,18 @@ public class RedisAutoConfigurationTests {
}
}
}
}
private
void
load
(
String
...
environment
)
{
this
.
context
=
doLoad
(
environment
);
}
private
AnnotationConfigApplicationContext
doLoad
(
String
...
environment
)
{
AnnotationConfigApplicationContext
applicationContext
=
new
AnnotationConfigApplicationContext
();
EnvironmentTestUtils
.
addEnvironment
(
applicationContext
,
environment
);
applicationContext
.
register
(
RedisAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
applicationContext
.
refresh
();
return
applicationContext
;
}
}
}
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