作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在以下两个示例中,处理通量流的行为似乎有所不同。
示例 1:
public static void main(String[] args) throws InterruptedException {
log.debug(" Before reading flux stream");
Flux<Customer> results = readFluxStream();
log.debug(Thread.currentThread().getName() + " : After reading flux stream");
results.subscribe(new LearnFlux().new CustomerConsumer());
Thread.sleep(5000);
log.debug(" Exit Main Thread ");
}
public static Flux<Customer> readFluxStream() {
List<Customer> customers = buildCustomers();
Customer[] customerArray = new Customer[customers.size()];
customerArray = customers.toArray(customerArray);
Flux<Customer> temp = Flux.fromArray(customerArray).delayElements(Duration.ofSeconds(1)).log();
return temp;
}
private class CustomerConsumer implements Consumer<Customer> {
@Override
public void accept(Customer customer) {
log.debug(Thread.currentThread().getName() + " This is a consumer " + customer.getFirstName());
}
}
从下面的日志中我们了解到 Flux 消费者正在不同的线程中运行。(在 *** 中突出显示)。我在主线程中引入了 sleep ,以便可以在控制台中捕获消费者日志。
19:07:24.695 [***main***] DEBUG com.learnjava.LearnFlux - Before reading flux stream
19:07:24.759 [***main***] DEBUG reactor.util.Loggers - Using Slf4j logging framework
19:07:24.779 [***main***] DEBUG com.learnjava.LearnFlux - main : After reading flux stream
19:07:24.788 [***main***] INFO reactor.Flux.ConcatMap.1 - onSubscribe(FluxConcatMap.ConcatMapImmediate)
19:07:24.790 [***main***] INFO reactor.Flux.ConcatMap.1 - request(unbounded)
19:07:25.821 [***parallel-1***] INFO reactor.Flux.ConcatMap.1 - onNext(Customer[id=null, firstName='Tom', lastName='Cruise'])
19:07:25.835 [***parallel-1***] DEBUG com.learnjava.LearnFlux - parallel-1 This is a consumer Tom
19:07:26.841 [***parallel-2***] INFO reactor.Flux.ConcatMap.1 - onNext(Customer[id=null, firstName='Jim', lastName='Carry'])
19:07:26.842 [***parallel-2***] DEBUG com.learnjava.LearnFlux - parallel-2 This is a consumer Jim
19:07:26.844 [***parallel-2***] INFO reactor.Flux.ConcatMap.1 - onComplete()
19:07:29.817 [***main***] DEBUG com.learnjava.LearnFlux - Exit Main Thread
示例 2
public interface CustomerRepository extends ReactiveCrudRepository<Customer, Long> {
@Query("SELECT * FROM customer WHERE last_name = :lastname")
Flux<Customer> findByLastName(String lastName);
}
public class CustomerConsumer implements Consumer<Customer> {
private static final Logger log = LoggerFactory.getLogger(CustomerConsumer.class);
@Override
public void accept(Customer customer) {
log.info(" This is a concusmer " + customer);
}
}
log.info(" Invoking R2DBC flux response ");
Flux<Customer> customers = repository.findAll();
customers.subscribe(new CustomerConsumer());
log.info("complete consumer in main thread");
从下面的日志中,我们观察到消费者正在同一个主线程中运行。 (在 *** 中突出显示)
[***main***] Invoking R2DBC flux response
[***main***] This is a concusmer Customer[id=1, firstName='Jack', lastName='Bauer']
[***main***]This is a concusmer Customer[id=2, firstName='Chloe', lastName='O'Brian']
[***main***] This is a concusmer Customer[id=3, firstName='Michelle', lastName='Dessler']
[***main***] complete consumer in main thread
澄清 :
最佳答案
Why is the Flux consumer in the first example running in a different thread where as the Flux returned by the R2DBC based repository (second example) is processed in the same main thread?
delayElements()
运算符(operator)。由于它是基于时间的运算符,因此默认情况下,它切换到并行调度程序,然后在并行执行程序上运行(以及您在日志中看到的并行线程。)基于时间的调度程序必须切换为“立即”调度程序,这将使您的操作保持在同一线程上,不能进行基于时间的调度(这是
delayElements
运算符所要求的。)
.delayElements(Duration.ofSeconds(1), Schedulers.boundedElastic())
例如,您将看到您的日志将显示正在使用的有界弹性线程池。
关于project-reactor - 为助焊剂消费者注册不同的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68320273/
我是一名优秀的程序员,十分优秀!