Skip to content

Commit 4ec6141

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
Reorder methods, reformat code. Original Pull Request: #2959
1 parent 20688c5 commit 4ec6141

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

src/main/java/org/springframework/data/redis/listener/KeyExpirationEventMessageListener.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* @author Christoph Strobl
3030
* @since 1.7
3131
*/
32-
public class KeyExpirationEventMessageListener extends KeyspaceEventMessageListener implements
33-
ApplicationEventPublisherAware {
32+
public class KeyExpirationEventMessageListener extends KeyspaceEventMessageListener
33+
implements ApplicationEventPublisherAware {
3434

3535
private static final Topic KEYEVENT_EXPIRED_TOPIC = new PatternTopic("__keyevent@*__:expired");
3636

@@ -45,6 +45,11 @@ public KeyExpirationEventMessageListener(RedisMessageListenerContainer listenerC
4545
super(listenerContainer);
4646
}
4747

48+
@Override
49+
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
50+
this.publisher = applicationEventPublisher;
51+
}
52+
4853
@Override
4954
protected void doRegister(RedisMessageListenerContainer listenerContainer) {
5055
listenerContainer.addMessageListener(this, KEYEVENT_EXPIRED_TOPIC);
@@ -67,8 +72,4 @@ protected void publishEvent(RedisKeyExpiredEvent event) {
6772
}
6873
}
6974

70-
@Override
71-
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
72-
this.publisher = applicationEventPublisher;
73-
}
7475
}

src/main/java/org/springframework/data/redis/listener/KeyspaceEventMessageListener.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.data.redis.connection.Message;
2323
import org.springframework.data.redis.connection.MessageListener;
2424
import org.springframework.data.redis.connection.RedisConnection;
25+
import org.springframework.data.redis.connection.RedisConnectionFactory;
26+
import org.springframework.data.redis.connection.RedisServerCommands;
2527
import org.springframework.lang.Nullable;
2628
import org.springframework.util.Assert;
2729
import org.springframework.util.ObjectUtils;
@@ -40,7 +42,7 @@ public abstract class KeyspaceEventMessageListener implements MessageListener, I
4042

4143
private final RedisMessageListenerContainer listenerContainer;
4244

43-
private String keyspaceNotificationsConfigParameter = "EA";
45+
private @Nullable String keyspaceNotificationsConfigParameter = "EA";
4446

4547
/**
4648
* Creates new {@link KeyspaceEventMessageListener}.
@@ -53,6 +55,26 @@ public KeyspaceEventMessageListener(RedisMessageListenerContainer listenerContai
5355
this.listenerContainer = listenerContainer;
5456
}
5557

58+
/**
59+
* Set the configuration string to use for {@literal notify-keyspace-events}.
60+
*
61+
* @param keyspaceNotificationsConfigParameter can be {@literal null}.
62+
* @since 1.8
63+
*/
64+
public void setKeyspaceNotificationsConfigParameter(@Nullable String keyspaceNotificationsConfigParameter) {
65+
this.keyspaceNotificationsConfigParameter = keyspaceNotificationsConfigParameter;
66+
}
67+
68+
@Override
69+
public void afterPropertiesSet() {
70+
init();
71+
}
72+
73+
@Override
74+
public void destroy() throws Exception {
75+
listenerContainer.removeMessageListener(this);
76+
}
77+
5678
@Override
5779
public void onMessage(Message message, @Nullable byte[] pattern) {
5880

@@ -76,20 +98,18 @@ public void onMessage(Message message, @Nullable byte[] pattern) {
7698
*/
7799
public void init() {
78100

79-
if (StringUtils.hasText(keyspaceNotificationsConfigParameter)) {
101+
RedisConnectionFactory connectionFactory = listenerContainer.getConnectionFactory();
80102

81-
RedisConnection connection = listenerContainer.getConnectionFactory().getConnection();
103+
if (StringUtils.hasText(keyspaceNotificationsConfigParameter) && connectionFactory != null) {
82104

83-
try {
105+
try (RedisConnection connection = connectionFactory.getConnection()) {
84106

85-
Properties config = connection.getConfig("notify-keyspace-events");
107+
RedisServerCommands commands = connection.serverCommands();
108+
Properties config = commands.getConfig("notify-keyspace-events");
86109

87110
if (!StringUtils.hasText(config.getProperty("notify-keyspace-events"))) {
88-
connection.setConfig("notify-keyspace-events", keyspaceNotificationsConfigParameter);
111+
commands.setConfig("notify-keyspace-events", keyspaceNotificationsConfigParameter);
89112
}
90-
91-
} finally {
92-
connection.close();
93113
}
94114
}
95115

@@ -105,23 +125,4 @@ protected void doRegister(RedisMessageListenerContainer container) {
105125
listenerContainer.addMessageListener(this, TOPIC_ALL_KEYEVENTS);
106126
}
107127

108-
@Override
109-
public void destroy() throws Exception {
110-
listenerContainer.removeMessageListener(this);
111-
}
112-
113-
/**
114-
* Set the configuration string to use for {@literal notify-keyspace-events}.
115-
*
116-
* @param keyspaceNotificationsConfigParameter can be {@literal null}.
117-
* @since 1.8
118-
*/
119-
public void setKeyspaceNotificationsConfigParameter(String keyspaceNotificationsConfigParameter) {
120-
this.keyspaceNotificationsConfigParameter = keyspaceNotificationsConfigParameter;
121-
}
122-
123-
@Override
124-
public void afterPropertiesSet() throws Exception {
125-
init();
126-
}
127128
}

0 commit comments

Comments
 (0)