1

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

小任务类:

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   
4

1 回答 1

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"/>
于 2013-09-30T20:14:13.593 回答