小编Вад*_*нюк的帖子

Project Reactor:doOnNext(或其他 doOnXXX)异步

有没有像 doOnNext 这样的方法,但是是异步的?例如,我需要为确定的元素做一些长时间的日志记录(通过电子邮件发送通知)。

Scheduler myParallel = Schedulers.newParallel("my-parallel", 4);

Flux<Integer> ints = Flux.just(1, 2, 3, 4, 5)
        .publishOn(myParallel)
        .doOnNext(v -> {
            // For example, we need to do something time-consuming only for 3

            if (v.equals(3)) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("LOG FOR " + v);
        });

ints.subscribe(System.out::println);
Run Code Online (Sandbox Code Playgroud)

但是为什么我要等待 3 的记录?我想异步执行此逻辑。

现在我只有这个解决方案

Thread.sleep(10000);

Scheduler myParallel = Schedulers.newParallel("my-parallel", 4);
Scheduler myParallel2 = Schedulers.newParallel("my-parallel2", 4);

Flux<Integer> ints = Flux.just(1, 2, 3, 4, 5)
        .publishOn(myParallel)
        .doOnNext(v -> { …
Run Code Online (Sandbox Code Playgroud)

java asynchronous project-reactor reactive-streams

6
推荐指数
1
解决办法
3445
查看次数

是否可以在没有递归的情况下获得StackOverflowError?

我有一个任务是在java中获取"StackOverflowError"而不使用-Xss和递归.我真的没有想法...只有一些废话,比如在运行时生成巨大的java类,编译它并调用...

java stack-overflow

5
推荐指数
1
解决办法
1346
查看次数

Project Reactor 有条件执行

我有一个要保存的对象(到 MongoDB),但在此之前我需要检查某些条件是否为真。

对象包含其他对象的 ID。看起来像

"object": {
   "id": "123",
   "subobject1": { "id": "1" },
   "subobject2": { "id": "2" }
}
Run Code Online (Sandbox Code Playgroud)

子对象只包含 id,其他信息位于其他集合中,所以我必须检查信息是否存在。

在块样式中,我可以做类似的事情

    if (!languageRepository.exists(Example.of(wordSet.getNativeLanguage())).block()) {
        throw new RuntimeException("Native language doesn't exist");
    }

    if (!languageRepository.exists(Example.of(wordSet.getTargetLanguage())).block()) {
        throw new RuntimeException("Target language doesn't exist");
    }
Run Code Online (Sandbox Code Playgroud)

只有这样我才能保存我的对象

return wordSetRepository.save(wordSet);
Run Code Online (Sandbox Code Playgroud)

如何在不阻塞的情况下以“反应式”风格做到这一点?

java reactor mongodb project-reactor reactive

5
推荐指数
1
解决办法
1万
查看次数

Spring Security WebFlux - 带有身份验证的主体

我想实现简单的 Spring Security WebFlux 应用程序。
我想使用像这样的 JSON 消息

{
   'username': 'admin', 
   'password': 'adminPassword'
} 
Run Code Online (Sandbox Code Playgroud)

在正文中(对 /signin 的 POST 请求)以登录我的应用程序。

我做了什么?

我创建了这个配置

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity(proxyTargetClass = true)
public class WebFluxSecurityConfig {

    @Autowired
    private ReactiveUserDetailsService userDetailsService;

    @Autowired
    private ObjectMapper mapper;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(11);
    }

    @Bean
    public ServerSecurityContextRepository securityContextRepository() {
        WebSessionServerSecurityContextRepository securityContextRepository =
                new WebSessionServerSecurityContextRepository();

        securityContextRepository.setSpringSecurityContextAttrName("securityContext");

        return securityContextRepository;
    }

    @Bean
    public ReactiveAuthenticationManager authenticationManager() {
        UserDetailsRepositoryReactiveAuthenticationManager authenticationManager =
                new UserDetailsRepositoryReactiveAuthenticationManager(userDetailsService);

        authenticationManager.setPasswordEncoder(passwordEncoder());

        return authenticationManager;
    }

    @Bean
    public AuthenticationWebFilter authenticationWebFilter() { …
Run Code Online (Sandbox Code Playgroud)

java spring-security reactor-netty spring-webflux

4
推荐指数
1
解决办法
1万
查看次数

非阻塞样式的好处是什么?

我正在尝试了解非阻塞编程的核心原理(以及项目反应堆之类的框架)。主要思想是使“线程池”具有确定数量的线程(执行程序)和在此执行的任务。我们不应有任何阻塞的线程。在“用户代码”中,我们只运行一些要执行的内容并留下回调(与结果有关)。出“用户”线程未被阻止,对。但是,如果我的任务取决于某些jdbc查询,该怎么办?我的任务将请求此查询,然后将被阻止等待结果,对吗?因此,该线程被阻止。

但是我们避免创建线程(这很昂贵)。这是这种风格的核心优势吗?

如果我的线程池由2个执行程序组成,并且两个执行程序都被阻止等待,则其他任务将不会执行,对吗?如何避免呢?创建两个以上的线程?

java multithreading asynchronous nonblocking project-reactor

2
推荐指数
1
解决办法
658
查看次数