我在Linux上运行Spring Batch作业(在Windows上运行正常)。这是一个简单的文件存在检查,但是当它确实存在时,我无法让系统找到路径/文件。 Spring的Resource类是否会以某种方式自动知道它何时在Linux或Windows上运行?还有别的我能做的吗
我的工作定义
<batch:job id="vendorGoalsJob" restartable="true">
<batch:step id="checkStartFileExists" next="readHeaderFiles">
<batch:tasklet ref="startFileExistsTasklet" />
</batch:step>
more steps....
</batch:job>
<bean id="startFileExistsTasklet"
class="com.blah.blah.StartFileExistsTasklet"
scope="step">
<property name="resource" value="/di/global/Users/my filename with blanks.txt" />
</bean>
tasklet类:
package com.blah.blah;
import static java.lang.String.format;
import org.apache.log4j.Logger;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.core.io.Resource;
public class StartFileExistsTasklet implements Tasklet {
private static final Logger logger = Logger.getLogger(StartFileExistsTasklet.class);
private Resource resource;
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
if (!resource.isReadable()) {
logger.info(format("Resource %s not found.", resource.getFilename()));
chunkContext.getStepContext().getStepExecution().setTerminateOnly();
}
return RepeatStatus.FINISHED;
}
public void setResource(Resource resource) {
this.resource = resource;
}
}
...我总是收到'未找到的消息':
2013-09-26 15:47:09,342 handleStep Executing step: [checkStartFileExists] (SimpleStepHandler.java:133)
2013-09-26 15:47:09,352 execute Resource vendor-goal-trigger.txt not found.
2013-09-26 15:47:09,361 isInterrupted Step interrupted through StepExecution
答案 0 :(得分:2)
如果在资源URL中未指定协议,则Spring使用的默认RessourceLoader从类路径加载。
/**
* Default implementation of the {@link ResourceLoader} interface.
* Used by {@link ResourceEditor}, and serves as base class for
* {@link org.springframework.context.support.AbstractApplicationContext}.
* Can also be used standalone.
*
* <p>Will return a {@link UrlResource} if the location value is a URL,
* and a {@link ClassPathResource} if it is a non-URL path or a
* "classpath:" pseudo-URL.
*
* @author Juergen Hoeller
* @since 10.03.2004
* @see FileSystemResourceLoader
* @see org.springframework.context.support.ClassPathXmlApplicationContext
*/
public class DefaultResourceLoader implements ResourceLoader { ... }
您可以尝试使用file:
前缀:
<property name="resource" value="file:/di/global/Users/my filename with blanks.txt"/>