Reactor编程模型中,阻塞上传文件FilePart的3中方式

1. 使用非阻塞的方式处理数据

将 Flux 转换为 Mono,并确保整个链式调用是非阻塞的。你可以使用 Mono.fromCallable 或其他非阻塞的方式来处理资源转换。

java 复制代码
public Mono<Void> addDocumentsByFile(FilePart file) {
    String suffix = FileNameUtil.getSuffix(file.filename());
    
    return file.content()
        .reduce(DataBuffer::write)
        .map(DataBuffer::toByteBuffer)
        .map(ByteBuffer::array)
        .map(ByteArrayResource::new)
        .flatMap(resource -> {
            TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(resource);
            List<Document> splitDocuments;
            if ("csv".equalsIgnoreCase(suffix)) {
                splitDocuments = new CsvTextSplitter().apply(tikaDocumentReader.read());
            } else {
                splitDocuments = new TokenTextSplitter().apply(tikaDocumentReader.read());
            }
            // 将文档存入向量数据库
            return Mono.fromRunnable(() -> {
                for (Document doc : splitDocuments) {
                    vectorStore.add(doc); // 假设vectorStore是一个异步接口
                }
            });
        })
        .then();
}

2. 使用 publishOn 切换到阻塞线程池

如果你确实需要使用阻塞操作,可以使用 publishOn 将操作切换到一个允许阻塞的线程池(例如 (Schedulers.boundedElastic()))。这会确保阻塞操作不会影响到非阻塞线程。

java 复制代码
public Mono<Void> addDocumentsByFile(FilePart file) {
    String suffix = FileNameUtil.getSuffix(file.filename());

    return file.content()
        .reduce(DataBuffer::write)
        .map(DataBuffer::toByteBuffer)
        .map(ByteBuffer::array)
        .map(ByteArrayResource::new)
        .publishOn(Schedulers.boundedElastic())
        .flatMap(resource -> {
            TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(resource);
            List<Document> splitDocuments;
            if ("csv".equalsIgnoreCase(suffix)) {
                splitDocuments = new CsvTextSplitter().apply(tikaDocumentReader.read());
            } else {
                splitDocuments = new TokenTextSplitter().apply(tikaDocumentReader.read());
            }
            // 将文档存入向量数据库
            for (Document doc : splitDocuments) {
                vectorStore.add(doc); // 假设vectorStore是一个同步接口
            }
            return Mono.empty();
        });
}

3. 使用 Mono.fromCallable 和 subscribeOn

如果你需要在一个特定的线程上执行阻塞操作,可以使用 Mono.fromCallable 并结合 subscribeOn 来确保操作在正确的线程上执行。

java 复制代码
public Mono<Void> addDocumentsByFile(FilePart file) {
    String suffix = FileNameUtil.getSuffix(file.filename());

    return file.content()
        .reduce(DataBuffer::write)
        .map(DataBuffer::toByteBuffer)
        .map(ByteBuffer::array)
        .map(ByteArrayResource::new)
        .flatMap(resource -> Mono.fromCallable(() -> {
            TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(resource);
            List<Document> splitDocuments;
            if ("csv".equalsIgnoreCase(suffix)) {
                splitDocuments = new CsvTextSplitter().apply(tikaDocumentReader.read());
            } else {
                splitDocuments = new TokenTextSplitter().apply(tikaDocumentReader.read());
            }
            // 将文档存入向量数据库
            for (Document doc : splitDocuments) {
                vectorStore.add(doc); // 假设vectorStore是一个同步接口
            }
            return null;
        }).subscribeOn(Schedulers.boundedElastic()))
        .then();
}

总结

为了避免阻塞操作导致的问题,建议尽量使用非阻塞的方式处理数据流。如果必须使用阻塞操作,可以通过 publishOn 或 subscribeOn 将操作切换到允许阻塞的线程池。这样可以确保你的应用程序在非阻塞环境中正常工作。

相关推荐
寒山李白21 分钟前
关于Java项目构建/配置工具方式(Gradle-Groovy、Gradle-Kotlin、Maven)的区别于选择
java·kotlin·gradle·maven
无妄无望1 小时前
docker学习(4)容器的生命周期与资源控制
java·学习·docker
MC丶科1 小时前
【SpringBoot 快速上手实战系列】5 分钟用 Spring Boot 搭建一个用户管理系统(含前后端分离)!新手也能一次跑通!
java·vue.js·spring boot·后端
千码君20161 小时前
React Native:从react的解构看编程众多语言中的解构
java·javascript·python·react native·react.js·解包·解构
夜白宋2 小时前
【word多文档docx合并】
java·word
@yanyu6663 小时前
idea中配置tomcat
java·mysql·tomcat
2501_916766543 小时前
【项目部署】JavaWeb、MavenJavaWeb项目部署至 Tomcat 的实现方式
java·tomcat
RoboWizard3 小时前
扩容刚需 金士顿新款Canvas Plus存储卡
java·spring·缓存·电脑·金士顿
lang201509283 小时前
Spring Boot 入门:5分钟搭建Hello World
java·spring boot·后端