2013-09-30 39 views
1

我在Linux上运行Spring批处理作业(在Windows上正常工作)。这是一个简单的文件存在检查,但我无法让系统找到路径/文件,当它确实存在。 Spring的Resource类以何种方式自动知道它在Linux或Windows上运行的时间?有没有别的东西我可以做在Linux系统上使用Spring批处理找不到文件资源

我的工作定义

<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> 

的任务蕾类:

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 

回答

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"/> 
+1

宾果!就是这样。谢谢。谢谢。谢谢。而且,你的解释有助于我进一步理解Spring也在做什么。 – Davidson

+1

np,你可以找到这里描述的行为:org.springframework.core.io.DefaultResourceLoader。这个类有时用在你自己的代码中 –

相关问题