Spring批处理源目录[target/config]不存在

Spring batch Source directory [target/config] does not exist

我有一个 spring 批处理应用程序,其中 属性 文件 batch-default.properties 设置为

batch.job.configuration.file.dir=target/config

现在这个应用程序在我的本地机器上运行良好,即使我没有任何这样的目录,但是当我尝试在我的集成服务器上部署它时我收到错误:

Cannot resolve reference to bean 'org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0.source' while setting bean property 'source'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [target/config] does not exist.
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:334)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)

有没有人遇到过类似的问题?

此处提供任何帮助。

-Vaibhav

我们在测试服务器上遇到了类似的问题。看起来像文件权限问题,即 Spring Batch Admin 尝试为 "target/config" 创建目录结构,但不允许用户创建目录。

这是导致问题的链:

META-INF/spring/batch/bootstrap/integration/configuration-context.xml 包含引用 属性:

的文件轮询器的定义
<file:inbound-channel-adapter directory="${batch.job.configuration.file.dir}" channel="job-configuration-files"
        filename-pattern=".*\.xml">
        <poller max-messages-per-poll="1" cron="5/1 * * * * *" />
    </file:inbound-channel-adapter>

检查入站通道适配器的文档显示以下内容 (spring-integration-file-2.1.xsd):

 <xsd:attribute name="directory" type="xsd:string" use="required">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[Specifies the input directory (The directory to poll from) e.g.:
                    directory="file:/absolute/input" or directory="file:relative/input"]]></xsd:documentation>
                </xsd:annotation>            
            </xsd:attribute>

和 (!)

 <xsd:attribute name="auto-create-directory" type="xsd:string" default="true">
                <xsd:annotation>
                    <xsd:documentation>
                        Specify whether to automatically create the source directory if it does not yet exist when this
                        adapter is being initialized. The default value is 'true'. If set to 'false' and the directory
                        does not exist upon initialization, an Exception will be thrown.
                    </xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>

因此,auto-create-directory 为真,Spring 正尝试在您服务器的 shell 路径上的某处创建(相对)目录结构。

报错信息,查看java class org.springframework.integration.file.FileReadingMessageSource给出解释:

if (!this.directory.exists() && this.autoCreateDirectory) {
            this.directory.mkdirs();
        }
        Assert.isTrue(this.directory.exists(),
                "Source directory [" + directory + "] does not exist.");

java.io.File.mkdirs() 的 java 文档说:

public boolean mkdirs()

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Returns:
    true if and only if the directory was created, along with all necessary parent directories; false otherwise

所以发生的事情是,mkdirs() returns "false" 因为他无法创建目录。下面的 exists() 也将 return "false" returning 原始 post.

中所述的错误消息

您可以使用绝对路径将参数设置为现有的可写目录,例如“/tmp”。不幸的是,如果您将作业定义存储在 class 路径上,我不知道这个 spring 批处理功能应该如何工作;不使用文件轮询器而是使用 "classpath-aware" 文件轮询器更有意义...