com.yelstream.topp.furnace.reactive.integration.SubscriberToQueueExample Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of topp-furnace-reactive-integration Show documentation
Show all versions of topp-furnace-reactive-integration Show documentation
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();
}
}