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

com.yelstream.topp.furnace.reactive.integration.SubscriberToQueueExample 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;

public class SubscriberToQueueExample {
    public static void main(String[] args) {
        Queue queue = new ConcurrentLinkedQueue<>();
        SubscriberToQueue subscriberToQueue = new SubscriberToQueue<>(queue);

        // Create a simple publisher
        Flow.Publisher publisher = new SimplePublisher();
        publisher.subscribe(subscriberToQueue);

        // Process the queue in another thread
        new Thread(() -> {
            while (!subscriberToQueue.isCompleted() || !queue.isEmpty()) {
                Integer item = queue.poll();
                if (item != null) {
                    System.out.println("Item from queue: " + item);
                } else {
                    // Sleep for a while to avoid busy-waiting
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
            }
            System.out.println("Queue processing completed.");
        }).start();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy