【问题标题】:Spring Batch Item Reader passing file object instead of resource name?Spring Batch Item Reader传递文件对象而不是资源名称?
【发布时间】:2014-11-12 08:57:38
【问题描述】:

通常,ItemReader 具有资源名称作为属性,我们可以将文件对象传递给 ItemReader 的任何实现。

我正在使用 3 版本的 Spring Batch API。

更新 :::

<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">

    <!-- Read a csv file -->
    <property name="resource" value="classpath:cvs/I_10000_3ColRem_input_File.csv" />

    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

            <!-- split it -->
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="customerId,year,month,numPurchases,sow,purchaseAmt,cm,mc,multiChannel,loyalty,productReturn,relationDur,cb" />
                </bean>
            </property>

            <property name="fieldSetMapper">

                <!-- return back to reader, rather than a mapped object. -->

                <!-- map to an object -->
                <bean
                    class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="testCLV" />
                    <property name="customEditors">
                      <map>
                        <entry key="java.lang.Double">
                             <ref local="doubleEditor" />
                        </entry>
                  </map>
                </property>
                </bean>
            </property>
        </bean>
    </property>

</bean>

我的 App.java 看起来像

ApplicationContext context = 
            new ClassPathXmlApplicationContext(springConfig);

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("reportJob");



    try {
        long a, b;
        a = System.currentTimeMillis();
        JobExecution execution = jobLauncher.run(job, new JobParameters());

        b = System.currentTimeMillis();
        System.out.println("Exit Status : " + execution.getStatus());
        System.out.println("jobLauncher.run "+(b-a)+"mil to execute. ("+((b-a)/1000)+" seconds)");
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Done");

我的要求是关注::

通过 web-application 用户将上传一个文件,我从中提取 inputStream

假设我有一个 streamInput obj 作为“streamInput”,我如何将它注入 ItemReader 的资源并运行我的工作。

【问题讨论】:

  • 我认为没有。为什么需要这个功能?
  • 我的要求是处理一个由网络应用程序上传的文件,所以我会有一个文件对象。如果我将文件保存在服务器上,那么它可能会遇到内存问题。你有解决这个问题的办法吗?
  • 我认为 spring-integration 可以解决问题(但我帮不了你,对不起)。使用“spring-integration”标记问题以获得更广泛的可见性

标签: spring-batch spring-integration


【解决方案1】:

您可以从InputStream (InputStreamResource) 创建一个Resource,您可以从File 对象中获取该Resource。您可以在此处阅读有关InputStreamResource 的更多信息:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/InputStreamResource.html

【讨论】:

  • 感谢您的快速回复,请参阅上面的更新文本
  • 只是想了想,我会使用FactoryBean 来提供Resource。这将允许您在FactoryBean 中设置InputStream,这样当您调用作业时,FactoryBean 将提供Resource
  • 感谢 Minella 的快速回复,您是否有任何示例代码可以证明您在上面所说的内容?
猜你喜欢
  • 2014-09-09
  • 2021-09-25
  • 2018-12-22
  • 2015-10-03
  • 2020-08-24
  • 2017-07-12
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多