From 43eea41dd6b7260e6ee4c226ece23498e65eafbc Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 6 Apr 2017 12:52:31 -0400 Subject: [PATCH] Polish --- .../web/reactive/result/view/Render.java | 64 +++++++++++++++++++ .../annotation/ReactiveTypeHandler.java | 47 +++++++------- 2 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 spring-webflux/src/main/java/org/springframework/web/reactive/result/view/Render.java diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/Render.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/Render.java new file mode 100644 index 00000000000..2407cb73383 --- /dev/null +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/Render.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.web.reactive.result.view; + +import java.util.Map; +import java.util.Optional; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.ui.Model; + +/** + * + * @author Rossen Stoyanchev + * @since 5.0 + */ +public interface Render { + + Optional view(); + + Map model(); + + Optional status(); + + HttpHeaders headers(); + + + static Builder view(String name) { + return null; + } + + + interface Builder> { + + B modelAttribute(String name, Object value); + + B modelMap(Map model); + + B model(Model model); + + B status(HttpStatus status); + + B header(String headerName, String... headerValues); + + B headers(HttpHeaders headers); + + Render build(); + + } + +} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java index 11048566221..9b2f81b847f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java @@ -184,15 +184,16 @@ class ReactiveTypeHandler { private Subscription subscription; - private final AtomicReference queue = new AtomicReference(); + private final AtomicReference elementRef = new AtomicReference<>(); + + private Throwable error; + + private volatile boolean terminated; private final AtomicLong executing = new AtomicLong(); private volatile boolean done; - - private volatile boolean terminated; - - private Throwable error; + protected AbstractEmitterSubscriber(ResponseBodyEmitter emitter, TaskExecutor executor) { this.emitter = emitter; @@ -229,20 +230,20 @@ class ReactiveTypeHandler { @Override public final void onNext(Object element) { - this.queue.lazySet(element); + this.elementRef.lazySet(element); trySchedule(); } @Override public final void onError(Throwable ex) { - error = ex; - terminated = true; + this.error = ex; + this.terminated = true; trySchedule(); } @Override public final void onComplete() { - terminated = true; + this.terminated = true; trySchedule(); } @@ -262,24 +263,23 @@ class ReactiveTypeHandler { } finally { this.executing.decrementAndGet(); - queue.lazySet(null); + this.elementRef.lazySet(null); } } } @Override public void run() { - if (done) { - queue.lazySet(null); + if (this.done) { + this.elementRef.lazySet(null); return; } - boolean d = terminated; - Object o = queue.get(); - if (o != null) { - queue.lazySet(null); + Object element = this.elementRef.get(); + if (element != null) { + this.elementRef.lazySet(null); try { - send(o); + send(element); this.subscription.request(1); } catch (final Throwable ex) { @@ -291,16 +291,17 @@ class ReactiveTypeHandler { } } - if (d) { + if (this.terminated) { this.done = true; - Throwable ex = error; - error = null; + Throwable ex = this.error; + this.error = null; if (ex != null) { if (logger.isDebugEnabled()) { logger.debug("Publisher error for " + this.emitter, ex); } - emitter.completeWithError(ex); - } else { + this.emitter.completeWithError(ex); + } + else { if (logger.isDebugEnabled()) { logger.debug("Publishing completed for " + this.emitter); } @@ -309,7 +310,7 @@ class ReactiveTypeHandler { return; } - if (executing.decrementAndGet() != 0) { + if (this.executing.decrementAndGet() != 0) { schedule(); } }