All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.http.server.reactive.AbstractListenerWriteFlushProcessor Maven / Gradle / Ivy

There is a newer version: 6.1.6
Show newest version
/*
 * 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.http.server.reactive;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Processor;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
 * An alternative to {@link AbstractListenerWriteProcessor} but instead writing
 * a {@code Publisher>} with flush boundaries enforces after
 * the completion of each nested Publisher.
 *
 * @author Arjen Poutsma
 * @author Violeta Georgieva
 * @author Rossen Stoyanchev
 * @since 5.0
 */
public abstract class AbstractListenerWriteFlushProcessor implements Processor, Void> {

	protected final Log logger = LogFactory.getLog(getClass());

	private final AtomicReference state = new AtomicReference<>(State.UNSUBSCRIBED);

	@Nullable
	private Subscription subscription;

	private volatile boolean subscriberCompleted;

	private final WriteResultPublisher resultPublisher = new WriteResultPublisher();


	// Subscriber methods and async I/O notification methods...

	@Override
	public final void onSubscribe(Subscription subscription) {
		this.state.get().onSubscribe(this, subscription);
	}

	@Override
	public final void onNext(Publisher publisher) {
		logger.trace("Received onNext publisher");
		this.state.get().onNext(this, publisher);
	}

	/**
	 * Error signal from the upstream, write Publisher. This is also used by
	 * sub-classes to delegate error notifications from the container.
	 */
	@Override
	public final void onError(Throwable ex) {
		if (logger.isTraceEnabled()) {
			logger.trace("Received onError: " + ex);
		}
		this.state.get().onError(this, ex);
	}

	/**
	 * Completion signal from the upstream, write Publisher. This is also used
	 * by sub-classes to delegate completion notifications from the container.
	 */
	@Override
	public final void onComplete() {
		logger.trace("Received onComplete");
		this.state.get().onComplete(this);
	}

	/**
	 * Invoked when flusing is possible, either in the same thread after a check
	 * via {@link #isWritePossible()}, or as a callback from the underlying
	 * container.
	 */
	protected final void onFlushPossible() {
		this.state.get().onFlushPossible(this);
	}

	/**
	 * Invoked during an error or completion callback from the underlying
	 * container to cancel the upstream subscription.
	 */
	protected void cancel() {
		this.logger.trace("Received request to cancel");
		if (this.subscription != null) {
			this.subscription.cancel();
		}
	}


	// Publisher implementation for result notifications...

	@Override
	public final void subscribe(Subscriber subscriber) {
		this.resultPublisher.subscribe(subscriber);
	}


	// Write API methods to be implemented or template methods to override...

	/**
	 * Create a new processor for the current flush boundary.
	 */
	protected abstract Processor createWriteProcessor();

	/**
	 * Whether writing/flushing is possible.
	 */
	protected abstract boolean isWritePossible();

	/**
	 * Flush the output if ready, or otherwise {@link #isFlushPending()} should
	 * return true after.
	 */
	protected abstract void flush() throws IOException;

	/**
	 * Whether flushing is pending.
	 */
	protected abstract boolean isFlushPending();

	/**
	 * Invoked when an error happens while flushing. Sub-classes may choose
	 * to ignore this if they know the underlying API will provide an error
	 * notification in a container thread.
	 * 

Defaults to no-op. */ protected void flushingFailed(Throwable t) { } // Private methods for use in State... private boolean changeState(State oldState, State newState) { boolean result = this.state.compareAndSet(oldState, newState); if (result && logger.isTraceEnabled()) { logger.trace(oldState + " -> " + newState); } return result; } private void flushIfPossible() { boolean result = isWritePossible(); if (logger.isTraceEnabled()) { logger.trace("isWritePossible[" + result + "]"); } if (result) { onFlushPossible(); } } /** * Represents a state for the {@link Processor} to be in. * *

	 *       UNSUBSCRIBED
	 *            |
	 *            v
	 *        REQUESTED <---> RECEIVED ------+
	 *            |              |           |
	 *            |              v           |
	 *            |           FLUSHING       |
	 *            |              |           |
	 *            |              v           |
	 *            +--------> COMPLETED <-----+
	 * 
*/ private enum State { UNSUBSCRIBED { @Override public void onSubscribe(AbstractListenerWriteFlushProcessor processor, Subscription subscription) { Assert.notNull(subscription, "Subscription must not be null"); if (processor.changeState(this, REQUESTED)) { processor.subscription = subscription; subscription.request(1); } else { super.onSubscribe(processor, subscription); } } }, REQUESTED { @Override public void onNext(AbstractListenerWriteFlushProcessor processor, Publisher currentPublisher) { if (processor.changeState(this, RECEIVED)) { Processor currentProcessor = processor.createWriteProcessor(); currentPublisher.subscribe(currentProcessor); currentProcessor.subscribe(new WriteResultSubscriber(processor)); } } @Override public void onComplete(AbstractListenerWriteFlushProcessor processor) { if (processor.changeState(this, COMPLETED)) { processor.resultPublisher.publishComplete(); } else { processor.state.get().onComplete(processor); } } }, RECEIVED { @Override public void writeComplete(AbstractListenerWriteFlushProcessor processor) { try { processor.flush(); } catch (Throwable ex) { processor.flushingFailed(ex); return; } if (processor.changeState(this, REQUESTED)) { if (processor.subscriberCompleted) { if (processor.isFlushPending()) { // Ensure the final flush processor.changeState(REQUESTED, FLUSHING); processor.flushIfPossible(); } else if (processor.changeState(REQUESTED, COMPLETED)) { processor.resultPublisher.publishComplete(); } else { processor.state.get().onComplete(processor); } } else { Assert.state(processor.subscription != null, "No subscription"); processor.subscription.request(1); } } } @Override public void onComplete(AbstractListenerWriteFlushProcessor processor) { processor.subscriberCompleted = true; } }, FLUSHING { public void onFlushPossible(AbstractListenerWriteFlushProcessor processor) { try { processor.flush(); } catch (Throwable ex) { processor.flushingFailed(ex); return; } if (processor.changeState(this, COMPLETED)) { processor.resultPublisher.publishComplete(); } else { processor.state.get().onComplete(processor); } } public void onNext(AbstractListenerWriteFlushProcessor proc, Publisher pub) { // ignore } @Override public void onComplete(AbstractListenerWriteFlushProcessor processor) { // ignore } }, COMPLETED { @Override public void onNext(AbstractListenerWriteFlushProcessor proc, Publisher pub) { // ignore } @Override public void onError(AbstractListenerWriteFlushProcessor processor, Throwable t) { // ignore } @Override public void onComplete(AbstractListenerWriteFlushProcessor processor) { // ignore } }; public void onSubscribe(AbstractListenerWriteFlushProcessor proc, Subscription subscription) { subscription.cancel(); } public void onNext(AbstractListenerWriteFlushProcessor proc, Publisher pub) { throw new IllegalStateException(toString()); } public void onError(AbstractListenerWriteFlushProcessor processor, Throwable ex) { if (processor.changeState(this, COMPLETED)) { processor.resultPublisher.publishError(ex); } else { processor.state.get().onError(processor, ex); } } public void onComplete(AbstractListenerWriteFlushProcessor processor) { throw new IllegalStateException(toString()); } public void writeComplete(AbstractListenerWriteFlushProcessor processor) { throw new IllegalStateException(toString()); } public void onFlushPossible(AbstractListenerWriteFlushProcessor processor) { // ignore } /** * Subscriber to receive and delegate completion notifications for from * the current Publisher, i.e. for the current flush boundary. */ private static class WriteResultSubscriber implements Subscriber { private final AbstractListenerWriteFlushProcessor processor; public WriteResultSubscriber(AbstractListenerWriteFlushProcessor processor) { this.processor = processor; } @Override public void onSubscribe(Subscription subscription) { subscription.request(Long.MAX_VALUE); } @Override public void onNext(Void aVoid) { } @Override public void onError(Throwable ex) { this.processor.cancel(); this.processor.onError(ex); } @Override public void onComplete() { if (this.processor.logger.isTraceEnabled()) { this.processor.logger.trace(this.processor.state + " writeComplete"); } this.processor.state.get().writeComplete(this.processor); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy