在Java 9中,我们如何实现SubmissionPublisher类?

2023年 9月 2日 29.0k 0

在Java 9中,我们如何实现SubmissionPublisher类?

从 Java 9 开始,我们可以通过引入四个核心接口来创建Reactive Streams:Publisher、Subscriber、Subscription、Processor 和一个具体类:实现 Publisher 的 SubmissionPublisher界面。每个界面都扮演着不同的角色,对应于响应式流的原则。我们可以使用 SubmissionPublisher 类的 submit() 方法将提供的项目发布给每个订阅者。

语法

public class SubmissionPublisher extends Object implements Flow.Publisher, AutoCloseable

登录后复制

在下面的示例中,我们可以实现 SubmissionPublisher 类

示例

import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
import java.util.concurrent.SubmissionPublisher;

class MySubscriber implements Subscriber {
private Subscription subscription;
private String name;
public MySubscriber(String name) {
this.name = name;
}
@Override
public void onComplete() {
System.out.println(name + ": onComplete");
}
@Override
public void onError(Throwable t) {
System.out.println(name + ": onError");
t.printStackTrace();
}
@Override
public void onNext(T msg) {
System.out.println(name + ": " + msg.toString() + " received in onNext");
subscription.request(1);
}
@Override
public void onSubscribe(Subscription subscription) {
System.out.println(name + ": onSubscribe");
this.subscription = subscription;
subscription.request(1);
}
}

// Main class
public class FlowTest {
public static void main(String args[]) {
SubmissionPublisher publisher = new SubmissionPublisher();
MySubscriber subscriber = new MySubscriber("Mine");
MySubscriber subscriberYours = new MySubscriber("Yours");
MySubscriber subscriberHis = new MySubscriber("His");
MySubscriber subscriberHers = new MySubscriber("Her");

publisher.subscribe(subscriber);
publisher.subscribe(subscriberYours);
publisher.subscribe(subscriberHis);
publisher.subscribe(subscriberHers);

publisher.submit("One");
publisher.submit("Two");
publisher.submit("Three");
publisher.submit("Four");
publisher.submit("Five");

try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
publisher.close();
}
}

登录后复制

输出

Yours: onSubscribe
His: onSubscribe
Mine: onSubscribe
His: One received in onNext
Yours: One received in onNext
Mine: One received in onNext
Yours: Two received in onNext
His: Two received in onNext
Yours: Three received in onNext
Mine: Two received in onNext
Yours: Four received in onNext
His: Three received in onNext
Yours: Five received in onNext
Mine: Three received in onNext
Her: onSubscribe
His: Four received in onNext
Her: One received in onNext
Mine: Four received in onNext
Her: Two received in onNext
His: Five received in onNext
Her: Three received in onNext
Mine: Five received in onNext
Her: Four received in onNext
Her: Five received in onNext
Yours: onComplete
His: onComplete
Mine: onComplete
Her: onComplete

登录后复制

以上就是在Java 9中,我们如何实现SubmissionPublisher类?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论