使用FileWritingMessageHandler的资源加载器

时间:2014-08-12 20:37:23

标签: java spring spring-integration

directory-expression端点使用<int-file:outbound-gateway>时,会在org.springframework.integration.file.FileWritingMessageHandler上调用以下方法:

private File evaluateDestinationDirectoryExpression(Message<?> message) {

    final File destinationDirectory;

    final Object destinationDirectoryToUse = this.destinationDirectoryExpression.getValue(
            this.evaluationContext, message);

    if (destinationDirectoryToUse == null) {
        throw new IllegalStateException(String.format("The provided " +
                "destinationDirectoryExpression (%s) must not resolve to null.",
                this.destinationDirectoryExpression.getExpressionString()));
    }
    else if (destinationDirectoryToUse instanceof String) {

        final String destinationDirectoryPath = (String) destinationDirectoryToUse;

        Assert.hasText(destinationDirectoryPath, String.format(
                "Unable to resolve destination directory name for the provided Expression '%s'.",
                this.destinationDirectoryExpression.getExpressionString()));
        destinationDirectory = new File(destinationDirectoryPath);
    }
    else if (destinationDirectoryToUse instanceof File) {
        destinationDirectory = (File) destinationDirectoryToUse;
    } else {
        throw new IllegalStateException(String.format("The provided " +
                "destinationDirectoryExpression (%s) must be of type " +
                "java.io.File or be a String.", this.destinationDirectoryExpression.getExpressionString()));
    }

    validateDestinationDirectory(destinationDirectory, this.autoCreateDirectory);
    return destinationDirectory;
}

基于这段代码,我看到如果要使用的目录求值为String,它会使用该String来创建一个新的java.io.File对象。

是否有理由不使用ResourceLoader而不是直接创建新文件?

我问,因为我的表达式正在评估格式的字符串&#39; file:// path / to / file /&#39;这当然是java.io.File(String)构造函数的无效路径。我假设Spring会像处理directory上的<int-file:outbound-gateway>属性一样处理String,并将其传递给ResourceLoader。

摘自我的配置文件:

<int-file:outbound-gateway 
    request-channel="inputChannel"
    reply-channel="updateTable"
    directory-expression=" 
        '${baseDirectory}'
        +
        T(java.text.MessageFormat).format('${dynamicPathPattern}', headers['Id'])
    "
    filename-generator-expression="headers.filename"
    delete-source-files="true"/>

baseDirectory是一个属性,它会更改表单的每个环境&#39; file:// hostname / some / path /&#39;

2 个答案:

答案 0 :(得分:1)

虽然没有直接回答这个问题,但我想发布我使用的解决方法。

在我的XML配置中,我将directory-expression更改为通过DefaultResourceLoader而不是字符串来评估文件。

所以这就是我的新配置:

<int-file:outbound-gateway 
    request-channel="inputChannel"
    reply-channel="updateTable"
    directory-expression=" new org.springframework.core.io.DefaultResourceLoader().getResource(
        '${baseDirectory}'
        +
        T(java.text.MessageFormat).format('${dynamicPathPattern}', headers['Id'])).getFile()
    "
    filename-generator-expression="headers.filename"
    delete-source-files="true"/>

答案 1 :(得分:1)

没有特别的理由就是这种情况,可能只是在实施时没有考虑过。

这个请求对我来说听起来很合理,并且通过提供更简单的语法,可以使其他人受益(即使你找到了解决办法)。请打开'Improvement' JIRA issue;感谢。