15
15
*/
16
16
package org .springframework .data .redis .core ;
17
17
18
- import org .springframework .data .redis .hash .HashMapper ;
19
- import org .springframework .data .redis .hash .ObjectHashMapper ;
20
- import org .springframework .lang .Nullable ;
21
18
import reactor .core .publisher .Flux ;
22
19
import reactor .core .publisher .Mono ;
23
20
import reactor .core .scheduler .Schedulers ;
31
28
import java .util .stream .Collectors ;
32
29
33
30
import org .reactivestreams .Publisher ;
31
+
34
32
import org .springframework .data .redis .connection .DataType ;
35
33
import org .springframework .data .redis .connection .ReactiveRedisConnection ;
36
34
import org .springframework .data .redis .connection .ReactiveRedisConnection .CommandResponse ;
39
37
import org .springframework .data .redis .core .script .DefaultReactiveScriptExecutor ;
40
38
import org .springframework .data .redis .core .script .ReactiveScriptExecutor ;
41
39
import org .springframework .data .redis .core .script .RedisScript ;
40
+ import org .springframework .data .redis .hash .HashMapper ;
41
+ import org .springframework .data .redis .hash .ObjectHashMapper ;
42
42
import org .springframework .data .redis .listener .ReactiveRedisMessageListenerContainer ;
43
43
import org .springframework .data .redis .listener .Topic ;
44
44
import org .springframework .data .redis .serializer .RedisElementReader ;
45
45
import org .springframework .data .redis .serializer .RedisElementWriter ;
46
46
import org .springframework .data .redis .serializer .RedisSerializationContext ;
47
+ import org .springframework .lang .Nullable ;
47
48
import org .springframework .util .Assert ;
48
49
import org .springframework .util .ClassUtils ;
49
50
@@ -131,22 +132,7 @@ public <T> Flux<T> execute(ReactiveRedisCallback<T> action) {
131
132
public <T > Flux <T > execute (ReactiveRedisCallback <T > action , boolean exposeConnection ) {
132
133
133
134
Assert .notNull (action , "Callback object must not be null" );
134
-
135
- ReactiveRedisConnectionFactory factory = getConnectionFactory ();
136
- ReactiveRedisConnection conn = factory .getReactiveConnection ();
137
-
138
- try {
139
-
140
- ReactiveRedisConnection connToUse = preProcessConnection (conn , false );
141
-
142
- ReactiveRedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy (connToUse ));
143
- Publisher <T > result = action .doInRedis (connToExpose );
144
-
145
- return Flux .from (postProcessResult (result , connToUse , false )).doFinally (signalType -> conn .close ());
146
- } catch (RuntimeException e ) {
147
- conn .close ();
148
- throw e ;
149
- }
135
+ return Flux .from (doInConnection (action , exposeConnection ));
150
136
}
151
137
152
138
/**
@@ -160,7 +146,7 @@ public <T> Flux<T> createFlux(ReactiveRedisCallback<T> callback) {
160
146
161
147
Assert .notNull (callback , "ReactiveRedisCallback must not be null!" );
162
148
163
- return Flux .defer (() -> doInConnection (callback , exposeConnection ));
149
+ return Flux .from ( doInConnection (callback , exposeConnection ));
164
150
}
165
151
166
152
/**
@@ -170,11 +156,11 @@ public <T> Flux<T> createFlux(ReactiveRedisCallback<T> callback) {
170
156
* @param callback must not be {@literal null}
171
157
* @return a {@link Mono} wrapping the {@link ReactiveRedisCallback}.
172
158
*/
173
- public <T > Mono <T > createMono (final ReactiveRedisCallback <T > callback ) {
159
+ public <T > Mono <T > createMono (ReactiveRedisCallback <T > callback ) {
174
160
175
161
Assert .notNull (callback , "ReactiveRedisCallback must not be null!" );
176
162
177
- return Mono .defer (() -> Mono . from (doInConnection (callback , exposeConnection ) ));
163
+ return Mono .from (doInConnection (callback , exposeConnection ));
178
164
}
179
165
180
166
/**
@@ -190,15 +176,19 @@ private <T> Publisher<T> doInConnection(ReactiveRedisCallback<T> action, boolean
190
176
191
177
Assert .notNull (action , "Callback object must not be null" );
192
178
193
- ReactiveRedisConnectionFactory factory = getConnectionFactory ();
194
- ReactiveRedisConnection conn = factory .getReactiveConnection ();
179
+ return Flux .usingWhen (Mono .fromSupplier (() -> {
180
+
181
+ ReactiveRedisConnectionFactory factory = getConnectionFactory ();
182
+ ReactiveRedisConnection conn = factory .getReactiveConnection ();
183
+ ReactiveRedisConnection connToUse = preProcessConnection (conn , false );
195
184
196
- ReactiveRedisConnection connToUse = preProcessConnection (conn , false );
185
+ return (exposeConnection ? connToUse : createRedisConnectionProxy (connToUse ));
186
+ }), conn -> {
187
+ Publisher <T > result = action .doInRedis (conn );
197
188
198
- ReactiveRedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy (connToUse ));
199
- Publisher <T > result = action .doInRedis (connToExpose );
189
+ return postProcessResult (result , conn , false );
200
190
201
- return Flux . from ( postProcessResult ( result , connToUse , false )). doFinally ( signal -> conn . close () );
191
+ }, ReactiveRedisConnection :: closeLater , ReactiveRedisConnection :: closeLater );
202
192
}
203
193
204
194
/*
0 commit comments