【问题标题】:Can i use spring ItemProcessor from spring batch standalone?我可以独立使用spring批处理中的spring ItemProcessor吗?
【发布时间】:2020-04-28 06:41:22
【问题描述】:

我的要求是使用 spring 控制器通过文件上传功能读取 CSV、XML、JSON、excel 文件格式。读取文件转换并保存到数据库中。

我想使用像 Spring Batch ItemProcessor 这样的 genericItem 处理器来读取上述文件格式。我的转换逻辑对所有人都是通用的,然后将其保存到数据库中。

有什么方法可以让我以独立的方式使用 spring batch ItemProcessor 而无需创建批处理作业,或者是否有任何开源工具可以读取上述格式的文件?

【问题讨论】:

  • 试试 apache Camel。 Camel 是一个开源集成框架,可让您快速轻松地集成各种使用或生成数据的系统。
  • 独立使用 Spring Batch ItemProcessor 是什么意思? ItemProcessor 是一个接口。如果您的意思是在不创建作业的情况下使用 Spring Batch 组件(读取器、写入器等),那么是可能的。这是一个例子:stackoverflow.com/questions/42834093/…

标签: spring spring-boot spring-batch


【解决方案1】:

您可以使用 apache camel 来获得所需的流量。互联网上有很多代码示例可以使用 spring-boot 配置 apache camel。 在此示例中,我假设数据发布到 /csv/excel/xml 端点,并且数据被路由到 direct:records。从direct:records,我们有一个自定义处理器来处理数据。我们可以有多个这样的步骤。

@Component
public class StudentRoute extends RouteBuilder {

    @Override
    public void configure() {
        restConfiguration()
                .component("servlet")
                .bindingMode(RestBindingMode.json);

        rest("/student").produces("application/json")
                .post("/csv").to("direct:records")
                .post("/excel").to("direct:records")
                .post("/xml").to("direct:records");

        from("direct:records")
                .process(new Processor() {

                    @Override
                    public void process(Exchange exchange) throws Exception {
                        Object value = exchange.getIn().getBody(Object.class);
                        //write your logic here
                    }
                });
    }
}

还可以点击此链接了解有关 Apache Camel 的更多信息。 Spring Boot, Apache Camel, and Swagger UI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2016-11-21
    • 2011-12-30
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多