org.springframework.batch.item.ItemStreamException:无法初始化阅读器

时间:2016-01-14 12:30:14

标签: spring spring-batch

我正在尝试解析具有多个记录的平面文件。 解析我正在使用FlatFileItemReader类。 解析时我得到了这个错误。

Jan 14, 2016 4:37:45 PM org.springframework.batch.core.step.AbstractStep execute
SEVERE: Encountered an error executing the step
org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:142)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:307)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:192)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:380)
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:124)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:117)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy16.run(Unknown Source)
    at com.deere.FtpMain.main(FtpMain.java:24)
Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): URL [http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:251)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:139)
    ... 22 more

在我的读者课上是

@Bean
public ItemReader<FtpData> reader() throws MalformedURLException{
    FlatFileItemReader<FtpData> reader=new FlatFileItemReader<FtpData>();
    reader.setResource(new UrlResource("http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&amp;f=snd1ol1p2"));
    reader.setLineMapper(new DefaultLineMapper<FtpData>(){{
        setLineTokenizer(new DelimitedLineTokenizer());
        setFieldSetMapper(new ftpDataSetMapper());
    }});
    return reader;
}

请建议我任何一个

1 个答案:

答案 0 :(得分:0)

网址:http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2重定向到http://download.finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2,HTTP响应代码为301.

要使用这种方法(不是我知道任何其他方法,但这是简单的HTTP协议),只需使用直接URL。它会起作用:

    @Bean
public FlatFileItemReader<TickerData> reader() throws Exception {
    FlatFileItemReader<TickerData> reader = new FlatFileItemReader<TickerData>();
    reader.setResource(new UrlResource("http://download.finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2"));
    reader.setLineMapper(new DefaultLineMapper() {{
        setLineTokenizer(new DelimitedLineTokenizer());
        setFieldSetMapper(new FieldSetMapper<TickerData>() {
            @Override
            public TickerData mapFieldSet(FieldSet fieldSet) throws BindException {
                TickerData data = new TickerData();
                data.setSymbol(fieldSet.readString(0));
                data.setName(fieldSet.readString(1));
                data.setLastTradeDate(fieldSet.readDate(2, "mm/DD/yyyy"));
                data.setOpen(fieldSet.readBigDecimal(3));
                data.setLastTrade(fieldSet.readBigDecimal(4));
                data.setChangePct(fieldSet.readString(5));
                return data;
            }
        });
    }});
    return reader;
}

这些来自我的日志:

2017-06-02 00:39:37.816  INFO 1898 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [convertPrice]
2017-06-02 00:39:39.159  INFO 1898 --- [           main] com.keyhole.example.LogItemWriter        : TickerData [symbol=XOM, name=Exxon Mobil Corporation Common, lastTradeDate=Sun Jan 01 00:06:00 IST 2017, open=80.3700, lastTrade=80.7148, changePct=+0.2668%, openGBP=51.31, lastTradeGBP=51.53]
2017-06-02 00:39:39.160  INFO 1898 --- [           main] com.keyhole.example.LogItemWriter        : TickerData [symbol=IBM, name=International Business Machines, lastTradeDate=Sun Jan 01 00:06:00 IST 2017, open=152.80, lastTrade=152.43, changePct=-0.13%, openGBP=97.54, lastTradeGBP=97.31]
2017-06-02 00:39:39.160  INFO 1898 --- [           main] com.keyhole.example.LogItemWriter        : TickerData [symbol=JNJ, name=Johnson & Johnson Common Stock, lastTradeDate=Sun Jan 01 00:06:00 IST 2017, open=128.32, lastTrade=128.36, changePct=+0.09%, openGBP=81.92, lastTradeGBP=81.94]
2017-06-02 00:39:39.160  INFO 1898 --- [           main] com.keyhole.example.LogItemWriter        : TickerData [symbol=MSFT, name=Microsoft Corporation, lastTradeDate=Sun Jan 01 00:06:00 IST 2017, open=70.24, lastTrade=69.77, changePct=-0.10%, openGBP=44.84, lastTradeGBP=44.54]
2017-06-02 00:39:39.175  INFO 1898 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=TickerPriceConversion]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]

另请参阅this SO link