Home > Java > javaTutorial > In Java 9, how do we implement SubmissionPublisher class?

In Java 9, how do we implement SubmissionPublisher class?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-02 19:05:02
forward
1058 people have browsed it

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

Starting from Java 9, we can create Reactive Streams by introducing four core interfaces: Publisher , Subscriber, Subscription, Processor and a concrete class: the SubmissionPublisher interface that implements Publisher. Each interface plays a different role, corresponding to the principles of ResponsivenessFlow. We can publish the provided items to each subscriber using the submit() method of the SubmissionPublisher class.

Syntax

1

<strong>public class SubmissionPublisher<T> extends Object implements Flow.Publisher<T>, AutoCloseable</strong>

Copy after login

In the following example, we can implement SubmissionPublisher Class

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

import java.util.concurrent.Flow.Subscriber;

import java.util.concurrent.Flow.Subscription;

import java.util.concurrent.SubmissionPublisher;

 

class MySubscriber<T> implements <strong>Subscriber<T></strong> {

   private <strong>Subscription </strong>subscription;

   private String name;

   public MySubscriber(String name) {

      this.name = name;

   }

   <strong>@Override</strong>

   public void <strong>onComplete()</strong> {

      System.out.println(name + ": onComplete");

   }

   <strong>@Override</strong>

   public void <strong>onError</strong>(Throwable t) {

      System.out.println(name + ": onError");

      t.printStackTrace();

   }

  <strong> @Override</strong>

   public void <strong>onNext</strong>(T msg) {

      System.out.println(name + ": " + msg.toString() + " received in onNext");

      subscription.<strong>request</strong>(1);

   }

   <strong>@Override</strong>

   public void <strong>onSubscribe</strong>(Subscription subscription) {

      System.out.println(name + ": onSubscribe");

      this.subscription = subscription;

      subscription.<strong>request</strong>(1);

   }

}

 

<strong>// Main class</strong>

public class FlowTest {

   public static void main(String args[]) {

      <strong>SubmissionPublisher<String></strong> publisher = new <strong>SubmissionPublisher</strong><>();

      MySubscriber<String> subscriber = new MySubscriber<>("Mine");

      MySubscriber<String> subscriberYours = new MySubscriber<>("Yours");

      MySubscriber<String> subscriberHis = new MySubscriber<>("His");

      MySubscriber<String> subscriberHers = new MySubscriber<>("Her");

 

      publisher.<strong>subscribe</strong>(subscriber);

      publisher.<strong>subscribe</strong>(subscriberYours);

      publisher.<strong>subscribe</strong>(subscriberHis);

      publisher.<strong>subscribe</strong>(subscriberHers);

 

      publisher.<strong>submit</strong>("One");

      publisher.<strong>submit</strong>("Two");

      publisher.<strong>submit</strong>("Three");

      publisher.<strong>submit</strong>("Four");

      publisher.<strong>submit</strong>("Five");

 

      try {

         Thread.sleep(1000);

      } catch(InterruptedException e) {

         e.printStackTrace();

      }

      publisher.close();

   }

}

Copy after login

output

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<strong>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</strong>

Copy after login

The above is the detailed content of In Java 9, how do we implement SubmissionPublisher class?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template