spring-210730-02—Spring5新功能-Webflux-响应式编程-Java实现
什么是响应式编程
什么是响应式编程:
简称RP(Reactive Programming)
响应式编程是一种面向数据流和变化传播的编程范式。这意味着可以在编程语言中很方便地表达静态或动态的数据流,
而相关的计算模型会自动将变化的值通过数据流进行传播。
例如:
对于 a=b+c 这个表达式的处理,在命令式编程中,会先计算 b+c 的结果,
再把此结果赋值给 变量a,因此 b,c 两值的变化不会对 变量a 产生影响。
但在响应式编程中,变量a 的值会随时跟随 b,c 的变化而变化。
电子表格程序就是响应式编程的一个例子。
单元格可以包含字面值或类似"=B1+C1"的公式,
而包含公式的单元格的值会依据其他单元格的值的变化而变化。
观察者模式(演示)
目前我也不懂,只是看着做了,有一个了解。2021.07.30
Java8以及之前版本提供了观察者模式的两个类:
Observer,Observable
ObserverTest.java
package com.bgy.spring.reactorjava8;
import java.util.Observable;
public class ObserverTest extends Observable {
public static void main(String[] args) {
ObserverTest observer = new ObserverTest();
// 添加观察者
observer.addObserver((o,element)->{
System.out.println("发生了变化");
});
observer.addObserver((o,element)->{
System.out.println("手动通知被观察者,准备改变");
});
// 数据变化
observer.setChanged();
// 通知
observer.notifyObservers();
}
}
/*
运行结果:
手动通知被观察者,准备改变
发生了变化
*/
尚硅谷视频给的例子
我还是看不懂2021.07.30
这个把Java9做了封装,
整个Spring5框架基于Java8,运行时兼容JDK9,这是Spring特点,可能这就是其中一个吧
Flow.java
public final class Flow {
private Flow() {} // uninstantiable
/**
* A producer of items (and related control messages) received by
* Subscribers. Each current {@link Subscriber} receives the same
* items (via method {@code onNext}) in the same order, unless
* drops or errors are encountered. If a Publisher encounters an
* error that does not allow items to be issued to a Subscriber,
* that Subscriber receives {@code onError}, and then receives no
* further messages. Otherwise, when it is known that no further
* messages will be issued to it, a subscriber receives {@code
* onComplete}. Publishers ensure that Subscriber method
* invocations for each subscription are strictly ordered in <a
* href="package-summary.html#MemoryVisibility"><i>happens-before</i></a>
* order.
*
* <p>Publishers may vary in policy about whether drops (failures
* to issue an item because of resource limitations) are treated
* as unrecoverable errors. Publishers may also vary about
* whether Subscribers receive items that were produced or
* available before they subscribed.
*
* @param <T> the published item type
*/
@FunctionalInterface
public static interface Publisher<T> {
/**
* Adds the given Subscriber if possible. If already
* subscribed, or the attempt to subscribe fails due to policy
* violations or errors, the Subscriber's {@code onError}
* method is invoked with an {@link IllegalStateException}.
* Otherwise, the Subscriber's {@code onSubscribe} method is
* invoked with a new {@link Subscription}. Subscribers may
* enable receiving items by invoking the {@code request}
* method of this Subscription, and may unsubscribe by
* invoking its {@code cancel} method.
*
* @param subscriber the subscriber
* @throws NullPointerException if subscriber is null
*/
public void subscribe(Subscriber<? super T> subscriber);
}
/**
* A receiver of messages. The methods in this interface are
* invoked in strict sequential order for each {@link
* Subscription}.
*
* @param <T> the subscribed item type
*/
public static interface Subscriber<T> {
/**
* Method invoked prior to invoking any other Subscriber
* methods for the given Subscription. If this method throws
* an exception, resulting behavior is not guaranteed, but may
* cause the Subscription not to be established or to be cancelled.
*
* <p>Typically, implementations of this method invoke {@code
* subscription.request} to enable receiving items.
*
* @param subscription a new subscription
*/
public void onSubscribe(Subscription subscription);
/**
* Method invoked with a Subscription's next item. If this
* method throws an exception, resulting behavior is not
* guaranteed, but may cause the Subscription to be cancelled.
*
* @param item the item
*/
public void onNext(T item);
/**
* Method invoked upon an unrecoverable error encountered by a
* Publisher or Subscription, after which no other Subscriber
* methods are invoked by the Subscription. If this method
* itself throws an exception, resulting behavior is
* undefined.
*
* @param throwable the exception
*/
public void onError(Throwable throwable);
/**
* Method invoked when it is known that no additional
* Subscriber method invocations will occur for a Subscription
* that is not already terminated by error, after which no
* other Subscriber methods are invoked by the Subscription.
* If this method throws an exception, resulting behavior is
* undefined.
*/
public void onComplete();
}
/**
* Message control linking a {@link Publisher} and {@link
* Subscriber}. Subscribers receive items only when requested,
* and may cancel at any time. The methods in this interface are
* intended to be invoked only by their Subscribers; usages in
* other contexts have undefined effects.
*/
public static interface Subscription {
/**
* Adds the given number {@code n} of items to the current
* unfulfilled demand for this subscription. If {@code n} is
* less than or equal to zero, the Subscriber will receive an
* {@code onError} signal with an {@link
* IllegalArgumentException} argument. Otherwise, the
* Subscriber will receive up to {@code n} additional {@code
* onNext} invocations (or fewer if terminated).
*
* @param n the increment of demand; a value of {@code
* Long.MAX_VALUE} may be considered as effectively unbounded
*/
public void request(long n);
/**
* Causes the Subscriber to (eventually) stop receiving
* messages. Implementation is best-effort -- additional
* messages may be received after invoking this method.
* A cancelled subscription need not ever receive an
* {@code onComplete} or {@code onError} signal.
*/
public void cancel();
}
/**
* A component that acts as both a Subscriber and Publisher.
*
* @param <T> the subscribed item type
* @param <R> the published item type
*/
public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
}
static final int DEFAULT_BUFFER_SIZE = 256;
/**
* Returns a default value for Publisher or Subscriber buffering,
* that may be used in the absence of other constraints.
*
* @implNote
* The current value returned is 256.
*
* @return the buffer size value
*/
public static int defaultBufferSize() {
return DEFAULT_BUFFER_SIZE;
}
}
Main.java
package com.atguigu.demo;
import java.util.concurrent.Flow;
public class Main {
public static void main(String[] args) {
Flow.Publisher<String> publisher = subscriber -> {
subscriber.onNext("1"); // 1
subscriber.onNext("2");
subscriber.onError(new RuntimeException("出错")); // 2
// subscriber.onComplete();
};
publisher.subscribe(new Flow.Subscriber<>() {
@Override
public void onSubscribe(Flow.Subscription subscription) {
subscription.cancel();
}
@Override
public void onNext(String item) {
System.out.println(item);
}
@Override
public void onError(Throwable throwable) {
System.out.println("出错了");
}
@Override
public void onComplete() {
System.out.println("publish complete");
}
});
}
}