在Spring Boot 2 with Reactor中,我试图合并两个热源。但是,似乎唯一一个报告了 中的两个参数中的第一个。我如何识别第二个.FluxmergeFluxmergemergeFlux
在下面的示例中,in 甚至不会打印 when 是第一个参数。如果我做第一个,那么不打印。System.errB-2outgoing1aoutgoing2A-2
以下是完整的示例;
package com.example.demo;
import java.time.Duration;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
public class Weather {
String city;
Integer temperature;
public Weather(String city, Integer temperature) {
this.city = city;
this.temperature = temperature;
}
@Override
public String toString() {
return "Weather [city=" + city + ", temperature=" + temperature + "]";
}
public static void main(String[] args) {
BlockingQueue<Weather> queue = new LinkedBlockingQueue<>();
BlockingQueue<Weather> queue2 = new LinkedBlockingQueue<>();
// Assume Spring @Repository "A-1"
new Thread(() -> {
for (int d = 1; d < 1000; d += 1) {
for (String s: new String[] {"LDN", "NYC", "PAR", "ZUR"}) {
queue.add(new Weather(s, d));
try { Thread.sleep(250); } catch (InterruptedException e) {}
}
}
}).start();
// Assume Spring @Repository "B-1"
new Thread(() -> {
for (int d = 1; d < 1000; d += 1) {
for (String s: new String[] {"MOS", "TLV"}) {
queue2.add(new Weather(s, d));
try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}
}).start();
// Assume Spring @Service "A-2" = real-time LDN, NYC, PAR, ZUR
Flux<Weather> outgoing1 = Flux.<Weather>create(
sink -> {
for (int i = 0; i < 1000; i++) {
try {
sink.next(queue.take());
System.err.println("1 " + queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
sink.complete();
}
)
阿晨1998
相关分类