CodeBlanks Logo Icon CodeBlanks

Reactive layer

Write reactive streams with the Flow API.

The reactive publish layer

The reactive layer builds a push-based model. The Subscriber interface consumes items through its onNext(String item) method. A SimplePublisher holds a subscriber list; publish calls onNext on each subscriber.

This is the core of Reactive Streams: the publisher pushes items to its subscribers. It is built with plain Java, no external library.

Functional interface and lambda

Subscriber is an interface with a single abstract method; that makes it a functional interface. So it can be written with a lambda instead of an anonymous class: s -> System.out.println("got: " + s). The compiler infers from the target type that s is a String.

This shortens the code and improves readability. The subscribe method expects a Subscriber; the lambda is converted automatically.

Practice Write the Subscriber and SimplePublisher classes

Add an interface Subscriber to the structure above with a void onNext(String item); method. Define class SimplePublisher that holds subscribers in a list, has a void subscribe(Subscriber s) method that adds a subscriber, and a void publish(String item) method that calls s.onNext(item) on each subscriber. In main create SimplePublisher p = new SimplePublisher();, subscribe with p.subscribe(s -> System.out.println("got: " + s)); and call p.publish("hello");.

Starter code

Main.java
@interface Service {}

@Service
class HelloService {
    String greet() { return "hello"; }
}

public class Main {
    public static void main(String[] args) {
        HelloService s = new HelloService();
        System.out.println("service ready: " + s.greet());
    }
}

Sample solution

Main.java
@interface Service {}

@Service
class HelloService {
    String greet() { return "hello"; }
}

interface Subscriber {
    void onNext(String item);
}

class SimplePublisher {
    java.util.List<Subscriber> subs = new java.util.ArrayList<>();
    void subscribe(Subscriber s) { subs.add(s); }
    void publish(String item) {
        for (Subscriber s : subs) s.onNext(item);
    }
}

public class Main {
    public static void main(String[] args) {
        SimplePublisher p = new SimplePublisher();
        p.subscribe(s -> System.out.println("got: " + s));
        p.publish("hello");
    }
}

Expected output

got: hello

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the subscriber method

Question
void ____(String item);
Answer
void onNext(String item);

Explanation: void onNext(String item); is the subscriber method.

2 What does a Subscriber do in a reactive stream?

  • It opens files
  • It compiles the class
  • It collects garbage
  • It consumes items Correct answer

Explanation: The Subscriber consumes published items via onNext.

3 Match the reactive elements

Term Meaning
Publisher Publishes
Subscriber Subscribes
onNext Item event
subscribe Add subscription

All lessons in Expert