22
22
import org .springframework .data .redis .connection .Message ;
23
23
import org .springframework .data .redis .connection .MessageListener ;
24
24
import org .springframework .data .redis .connection .RedisConnection ;
25
+ import org .springframework .data .redis .connection .RedisConnectionFactory ;
26
+ import org .springframework .data .redis .connection .RedisServerCommands ;
25
27
import org .springframework .lang .Nullable ;
26
28
import org .springframework .util .Assert ;
27
29
import org .springframework .util .ObjectUtils ;
@@ -40,7 +42,7 @@ public abstract class KeyspaceEventMessageListener implements MessageListener, I
40
42
41
43
private final RedisMessageListenerContainer listenerContainer ;
42
44
43
- private String keyspaceNotificationsConfigParameter = "EA" ;
45
+ private @ Nullable String keyspaceNotificationsConfigParameter = "EA" ;
44
46
45
47
/**
46
48
* Creates new {@link KeyspaceEventMessageListener}.
@@ -53,6 +55,26 @@ public KeyspaceEventMessageListener(RedisMessageListenerContainer listenerContai
53
55
this .listenerContainer = listenerContainer ;
54
56
}
55
57
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
+
56
78
@ Override
57
79
public void onMessage (Message message , @ Nullable byte [] pattern ) {
58
80
@@ -76,20 +98,18 @@ public void onMessage(Message message, @Nullable byte[] pattern) {
76
98
*/
77
99
public void init () {
78
100
79
- if ( StringUtils . hasText ( keyspaceNotificationsConfigParameter )) {
101
+ RedisConnectionFactory connectionFactory = listenerContainer . getConnectionFactory ();
80
102
81
- RedisConnection connection = listenerContainer . getConnectionFactory (). getConnection ();
103
+ if ( StringUtils . hasText ( keyspaceNotificationsConfigParameter ) && connectionFactory != null ) {
82
104
83
- try {
105
+ try ( RedisConnection connection = connectionFactory . getConnection ()) {
84
106
85
- Properties config = connection .getConfig ("notify-keyspace-events" );
107
+ RedisServerCommands commands = connection .serverCommands ();
108
+ Properties config = commands .getConfig ("notify-keyspace-events" );
86
109
87
110
if (!StringUtils .hasText (config .getProperty ("notify-keyspace-events" ))) {
88
- connection .setConfig ("notify-keyspace-events" , keyspaceNotificationsConfigParameter );
111
+ commands .setConfig ("notify-keyspace-events" , keyspaceNotificationsConfigParameter );
89
112
}
90
-
91
- } finally {
92
- connection .close ();
93
113
}
94
114
}
95
115
@@ -105,23 +125,4 @@ protected void doRegister(RedisMessageListenerContainer container) {
105
125
listenerContainer .addMessageListener (this , TOPIC_ALL_KEYEVENTS );
106
126
}
107
127
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
- }
127
128
}
0 commit comments