我想测试弹簧反应器,为此我实现了一个小例子,这里是相关的代码:
应用:
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private ServiceData data;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
this.data.start();
}
}
FluxCreatorFunction
public class FluxCreatorFunction<T extends Function<V, E>, V, E> {
public ConnectableFlux<E> createFlux(T t, V v) {
return (ConnectableFlux<E>) Flux.<E>create(flux -> {
while (true) {
try {
Thread.sleep((ThreadLocalRandom.current().nextInt(1, (3 + 1)) * 1000));
flux.next(t.apply(v));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).publish();
}
}
ServiceData
@Service
public class ServiceData {
@Autowired
@Qualifier("inserter")
private ConnectableFlux<Todo> fluxInserter;
@Autowired
@Qualifier("deleter")
private ConnectableFlux<Todo> fluxDeleter;
@Autowired
private TodoRepository repo;
public void start() {
this.startInserter();
this.startDeleter();
}
public void startInserter() {
this.fluxInserter.subscribe(new ConsumerInserter());
this.fluxInserter.subscribe((todo) -> {
this.repo.save(todo);
this.repo.flush();
});
this.fluxInserter.connect();
}
public void startDeleter() {
this.fluxDeleter.subscribe(new ConsumerDeleter());
this.fluxDeleter.subscribe((todo) -> {
this.repo.delete(todo);
this.repo.flush();
});
this.fluxDeleter.connect();
}
@Bean
@Qualifier("inserter")
public ConnectableFlux<Todo> createInserter() {
return new FluxCreatorFunction<FunctionInserter, Void, Todo>().createFlux(new FunctionInserter(), null);
}
@Bean
@Qualifier("deleter")
public ConnectableFlux<Todo> createDeleter() {
return new FluxCreatorFunction<FunctionDeleter, TodoRepository, Todo>().createFlux(new FunctionDeleter(), this.repo);
}
}
class FunctionInserter implements Function<Void, Todo> {
private RestTemplate restTemplate = new RestTemplate();
@Override
public Todo apply(Void v) {
String quote = this.restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", QuoteResource.class).getValue().getQuote();
return new Todo(quote, false);
}
}
class FunctionDeleter implements Function<TodoRepository, Todo> {
@Override
public Todo apply(TodoRepository repo) {
return repo.findAll().get(0);
}
}
class ConsumerInserter implements Consumer<Todo> {
@Override
public void accept(Todo todo) {
System.out.println("New todo: " + todo.getText());
}
}
class ConsumerDeleter implements Consumer<Todo> {
@Override
public void accept(Todo todo) {
System.out.println("Deleted todo: " + todo.getText());
}
}
正如您所看到的,我正在创建两个不同的Flux
发布商。两者都创建为服务的@Bean
和@Autowired
。
问题是:第一个Flux
正在进行工作。如果我先启动插入器:
this.startInserter();
this.startDeleter();
输出结果为:
New todo: So easy it is to switch container in #springboot.
New todo: Spring has come quite a ways in addressing developer enjoyment and ease of use since the last time I built an application using it.
New todo: Working with Spring Boot is like pair-programming with the Spring developers.
New todo: Spring has come quite a ways in addressing developer enjoyment and ease of use since the last time I built an application using it.
New todo: The real benefit of Boot, however, is that it's just Spring. That means any direction the code takes, regardless of complexity, I know it's a safe bet.
New todo: I have two hours today to build an app from scratch. @springboot to the rescue!
New todo: Really loving Spring Boot, makes stand alone Spring apps easy.
New todo: Really loving Spring Boot, makes stand alone Spring apps easy.
如果我转过身来:
this.startDeleter();
this.startInserter();
输出结果为:
Deleted todo: Spring has come quite a ways in addressing developer enjoyment and ease of use since the last time I built an application using it.
Deleted todo: Spring has come quite a ways in addressing developer enjoyment and ease of use since the last time I built an application using it.
Deleted todo: The real benefit of Boot, however, is that it's just Spring. That means any direction the code takes, regardless of complexity, I know it's a safe bet.
Deleted todo: Spring has come quite a ways in addressing developer enjoyment and ease of use since the last time I built an application using it.
Deleted todo: So easy it is to switch container in #springboot.
Deleted todo: I have two hours today to build an app from scratch. @springboot to the rescue!
Deleted todo: Really loving Spring Boot, makes stand alone Spring apps easy.
Deleted todo: So easy it is to switch container in #springboot.
因此,我首先开始Flux
无关紧要。第二个Flux
永远不会做它的工作,我不知道为什么。两个Flux
都在同一个线程上运行吗?他们需要一个标识符吗?还有什么可能是问题?
答案 0 :(得分:1)
在Flux.create
中生成数据的方式必须是异步的。在这里你是阻止,这是不受支持的。是的,Flux
最终都在同一个线程上执行,第一个无限循环并阻塞所述线程。
您可以使用“subscribeOn”和Schedulers.parallel()
或Schedulers.elastic()
等调度程序将工作推迟到单独的线程上。
您也可以尝试避免使用create/generate
并使用时间运算符完全不阻塞。最新的3.1.0.M2里程碑有delayUntil
,
例如。您还可以使用Flux.range(1, n).concatMap(index -> Mono.delay(generateRandomDelayValue(index))
编辑:我可以想到一种方法来重现您尝试实施的随机延迟行为:
public class FluxCreatorFunction<T extends Function<V, E>, V, E> {
public ConnectableFlux<E> createFlux(T t, V v) {
//use generate to generate a random value per cycle
return Flux.generate(sink -> ThreadLocalRandom
.current()
.nextInt(1, (3 + 1)))
//that random value will be used as a delay, so we need to
//transform each value into a new async sequence, and also
//ensure that the order is preserved, hence concatMap
.concatMap(randomDelay ->
//we introduce a delay then...
Mono.delay(Duration.ofSeconds(randomDelay))
//... map to the result of the function
.map(ignore -> t.apply(v))
.publish();
}
}