Skip to content

Commit ebbcd95

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-507 - Do not destroy RedisConnectionFactory in RedisKeyValueAdapter.destroy.
Do not destroy RedisConnectionFactory which is likely managed outside of RedisKeyValueAdapter. This change makes sure to retain a working connection factory while shutting down message listeners as those try to unsubscribe on shutdown. Original Pull Request: #195
1 parent c1476b7 commit ebbcd95

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* Please use {@link RedisTemplate} for this purpose.
8585
*
8686
* @author Christoph Strobl
87+
* @author Mark Paluch
8788
* @since 1.7
8889
*/
8990
public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
@@ -447,13 +448,6 @@ public byte[] toBytes(Object source) {
447448
*/
448449
public void destroy() throws Exception {
449450

450-
if (redisOps instanceof RedisTemplate) {
451-
RedisConnectionFactory connectionFactory = ((RedisTemplate<?, ?>) redisOps).getConnectionFactory();
452-
if (connectionFactory instanceof DisposableBean) {
453-
((DisposableBean) connectionFactory).destroy();
454-
}
455-
}
456-
457451
this.expirationListener.destroy();
458452
this.messageListenerContainer.destroy();
459453
}

src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.After;
3030
import org.junit.Before;
3131
import org.junit.Test;
32+
import org.springframework.beans.factory.DisposableBean;
3233
import org.springframework.dao.DataAccessException;
3334
import org.springframework.data.annotation.Id;
3435
import org.springframework.data.annotation.Reference;
@@ -86,6 +87,14 @@ public void tearDown() {
8687
} catch (Exception e) {
8788
// ignore
8889
}
90+
91+
try {
92+
if (connectionFactory instanceof DisposableBean) {
93+
((DisposableBean) connectionFactory).destroy();
94+
}
95+
} catch (Exception e) {
96+
// ignore
97+
}
8998
}
9099

91100
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.redis.core;
18+
19+
import static org.mockito.Mockito.*;
20+
21+
import java.util.Arrays;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Mock;
27+
import org.mockito.runners.MockitoJUnitRunner;
28+
import org.springframework.data.redis.connection.RedisConnection;
29+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
30+
31+
/**
32+
* Unit tests for {@link RedisKeyValueAdapter}.
33+
*
34+
* @author Mark Paluch
35+
*/
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class RedisKeyValueAdapterUnitTests {
38+
39+
RedisTemplate<?, ?> redisTemplate;
40+
RedisKeyValueAdapter redisKeyValueAdapter;
41+
42+
@Mock JedisConnectionFactory jedisConnectionFactoryMock;
43+
@Mock RedisConnection redisConnectionMock;
44+
45+
@Before
46+
public void setUp() throws Exception {
47+
48+
redisTemplate = new RedisTemplate<Object, Object>();
49+
redisTemplate.setConnectionFactory(jedisConnectionFactoryMock);
50+
redisTemplate.afterPropertiesSet();
51+
52+
when(jedisConnectionFactoryMock.getConnection()).thenReturn(redisConnectionMock);
53+
when(redisConnectionMock.getConfig("notify-keyspace-events"))
54+
.thenReturn(Arrays.asList("notify-keyspace-events", "KEA"));
55+
56+
redisKeyValueAdapter = new RedisKeyValueAdapter(redisTemplate);
57+
}
58+
59+
@Test
60+
public void destroyShouldNotDestroyConnectionFactory() throws Exception {
61+
62+
redisKeyValueAdapter.destroy();
63+
64+
verify(jedisConnectionFactoryMock, never()).destroy();
65+
}
66+
}

0 commit comments

Comments
 (0)