@@ -43,9 +43,12 @@ import javax.servlet.http.HttpServletResponse
43
43
@Slf4j
44
44
class RxResultSubscriber extends Subscriber implements AsyncListener {
45
45
/**
46
- * The Async context
46
+ * The prefix for server sent events
47
47
*/
48
48
public static final String DATA_PREFIX = " data: "
49
+ /**
50
+ * The Async context
51
+ */
49
52
final GrailsAsyncContext asyncContext
50
53
/**
51
54
* The exception handler
@@ -198,57 +201,62 @@ class RxResultSubscriber extends Subscriber implements AsyncListener {
198
201
199
202
@Override
200
203
void onCompleted () {
201
- // When the observable finishes emitting items
202
- // terminate the asynchronous context in the appropriate manner based on the
203
- // completion strategy
204
- switch (completionStrategy) {
205
- case RxCompletionStrategy . COMPLETE :
206
- asyncContext. complete()
207
- break
208
- case RxCompletionStrategy . DISPATCH :
209
- asyncContext. dispatch()
210
- break
211
- case RxCompletionStrategy . NONE :
212
- // for none, the RxResult will have terminated the asynchronous context so do nothing
213
- break
214
- default :
215
- if (isRender) {
216
- asyncContext. response. flushBuffer()
217
- }
218
- asyncContext. complete()
204
+ synchronized (asyncContext) {
205
+ // When the observable finishes emitting items
206
+ // terminate the asynchronous context in the appropriate manner based on the
207
+ // completion strategy
208
+ switch (completionStrategy) {
209
+ case RxCompletionStrategy . COMPLETE :
210
+ asyncContext. complete()
211
+ break
212
+ case RxCompletionStrategy . DISPATCH :
213
+ asyncContext. dispatch()
214
+ break
215
+ case RxCompletionStrategy . NONE :
216
+ // for none, the RxResult will have terminated the asynchronous context so do nothing
217
+ break
218
+ default :
219
+ if (isRender) {
220
+ asyncContext. response. flushBuffer()
221
+ }
222
+ asyncContext. complete()
223
+ }
224
+
219
225
}
220
226
}
221
227
222
228
@Override
223
229
void onError (Throwable e ) {
224
- if (! asyncComplete && ! asyncContext. response. isCommitted()) {
225
- // if an error occurred and the response has not yet been commited try and handle it
226
- def httpServletResponse = (HttpServletResponse ) asyncContext. response
227
- // first check if the exception resolver and resolve a model and view
228
- if (exceptionResolver != null ) {
229
- def modelAndView = exceptionResolver. resolveException((HttpServletRequest ) asyncContext. request, httpServletResponse, this , (Exception ) e)
230
- if (modelAndView != null ) {
231
- asyncContext. getRequest(). setAttribute(GrailsApplicationAttributes . MODEL_AND_VIEW , modelAndView);
232
- asyncContext. dispatch()
230
+ synchronized (asyncContext) {
231
+ if (! asyncComplete && ! asyncContext. response. isCommitted()) {
232
+ // if an error occurred and the response has not yet been commited try and handle it
233
+ def httpServletResponse = (HttpServletResponse ) asyncContext. response
234
+ // first check if the exception resolver and resolve a model and view
235
+ if (exceptionResolver != null ) {
236
+ def modelAndView = exceptionResolver. resolveException((HttpServletRequest ) asyncContext. request, httpServletResponse, this , (Exception ) e)
237
+ if (modelAndView != null ) {
238
+ asyncContext. getRequest(). setAttribute(GrailsApplicationAttributes . MODEL_AND_VIEW , modelAndView);
239
+ asyncContext. dispatch()
240
+ }
241
+ else {
242
+ // if the error can't be resolved send the default error
243
+ sendDefaultError(e, httpServletResponse)
244
+ }
233
245
}
234
246
else {
235
- // if the error can't be resolved send the default error
236
247
sendDefaultError(e, httpServletResponse)
237
248
}
238
249
}
239
- else {
240
- sendDefaultError(e, httpServletResponse)
241
- }
242
- }
243
- else if (! asyncComplete) {
244
- if (e != null ) {
245
- log. error(" Async Dispatch Error: ${ e.message} " , e)
246
- }
247
- else {
248
- log. debug(" Async timeout occurred" )
250
+ else if (! asyncComplete) {
251
+ if (e != null ) {
252
+ log. error(" Async Dispatch Error: ${ e.message} " , e)
253
+ }
254
+ else {
255
+ log. debug(" Async timeout occurred" )
256
+ }
257
+ asyncContext. request. removeAttribute(WebAsyncUtils . WEB_ASYNC_MANAGER_ATTRIBUTE )
258
+ asyncContext. complete()
249
259
}
250
- asyncContext. request. removeAttribute(WebAsyncUtils . WEB_ASYNC_MANAGER_ATTRIBUTE )
251
- asyncContext. complete()
252
260
}
253
261
}
254
262
0 commit comments