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

com.yelstream.topp.furnace.reactive.integration.QueuePublisherExample Maven / Gradle / Ivy

Go to download

Topp Furnace Reactive Integration provides interactions between selected reactive frameworks.

The newest version!
package com.yelstream.topp.furnace.reactive.integration;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;

public class QueuePublisherExample {
    public static void main(String[] args) {
        Queue queue = new ConcurrentLinkedQueue<>();
        QueuePublisher queuePublisher = new QueuePublisher<>(queue);

        queuePublisher.subscribe(new Subscriber<>() {
            @Override
            public void onSubscribe(Subscription subscription) {
                subscription.request(Long.MAX_VALUE);
            }

            @Override
            public void onNext(Integer item) {
                System.out.println("Received: " + item);
            }

            @Override
            public void onError(Throwable throwable) {
                throwable.printStackTrace();
            }

            @Override
            public void onComplete() {
                System.out.println("Completed");
            }
        });

        // Start the publisher
        queuePublisher.start();

        // Add items to the queue
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                queue.add(i);
                try {
                    Thread.sleep(500); // Simulate delay between items
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy