【问题标题】:Spring boot spring.batch.job.enabled=false not able to recognizeSpring boot spring.batch.job.enabled=false 无法识别
【发布时间】:2015-09-25 09:07:44
【问题描述】:

我在 application.properties 中尝试了spring.batch.job.enabled=false,在运行 jar 文件时尝试了-Dspring.batch.job.enabled=false。

但是@EnableBatchProcessing 在应用程序启动时自动开始运行批处理作业。我该如何调试这种情况?

TestConfiguration.class

@Configuration
@EnableBatchProcessing
public class TestConfiguration {...}

主应用程序

@ComponentScan("com.demo")
@EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) throws BeansException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException, JobRestartException {

ConfigurableApplicationContext ctx = SpringApplication.run(TestConfiguration.class, args);
...}

pom.xml 我将依赖项用作弹簧启动而不是父项

<dependencyManagement>
    <dependencies>
        <!-- Import dependecy for spring boot from here-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

    我能够知道发生了什么,我正在使用自定义读取器/处理器/写入器。当 springboot 应用程序启动时,它实际上尝试对这个自定义 bean bean 进行依赖注入,我在其中编写了一些应用程序逻辑。

    例子

    ** TestConfiguration.class**

        @Configuration
        @EnableBatchProcessing
        public class TestConfiguration {
    
            @Bean
            @Conditional(Employee.class)
            public ItemWriter<Employee> writer_employee(DataSource dataSource) throws IOException {
                FlatFileItemWriter<Employee> writer = new FlatFileItemWriter<Employee>();
                writer.setResource(new FileSystemResource(FinanceReportUtil.createFile("Employee.csv")));
                writer.setHeaderCallback(new FlatFileHeaderCallback() {
                    @Override
                        public void writeHeader(Writer writer) throws IOException {
                        writer.write("id, name");
                     }
                 });
                DelimitedLineAggregator<Employee> delLineAgg = new DelimitedLineAggregator<Employee>();
                delLineAgg.setDelimiter(",");
                BeanWrapperFieldExtractor<Employee> fieldExtractor = new BeanWrapperFieldExtractor<Employee>();
                fieldExtractor.setNames(new String[]{"id", "name"});
                delLineAgg.setFieldExtractor(fieldExtractor);
                writer.setLineAggregator(delLineAgg);
                return writer;
            }
    
            @Bean
            @Conditional(Manager.class)
            public ItemWriter<Person> writer_manager(DataSource dataSource) throws IOException {
    
                // Does the same logic as employee
            }
    
            // Also has job and step etc.
        }
    

    即使使用 spring.batch.job.enabled=false,它也会创建文件,为了克服这个问题,我创建了自定义逻辑来注入或不注入 bean,如下所示

    application.properties

    # all, manager, employee
    person=manager
    

    ManagerCondition.class

    public class ManagerCondition implements Condition {
    
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String person= context.getEnvironment().getProperty("person");
        return person.equals("manager");
    
    }
    

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题,当我们在属性文件中给出这个属性时,属性“spring.batch.job.enabled=false”在启动时没有被识别。这可能是因为在批处理启动之前属性可能尚未加载到上下文中。

      所以我在standalone.xml 中将属性“spring.batch.job.enabled=false”设置为系统属性,如下所示。

      <system-properties>  
              <property name="spring.batch.job.enabled" value="false"/>  
      </system-properties>  
      

      这样它就成功了,并且春季批处理作业没有在服务器启动时初始化。

      请注意,系统属性必须放在standalone.xml 中的扩展标记之后。

      【讨论】:

      • +1 为我提供有关未加载属性的提示。我已经定义了一个自定义的“batch.properties”文件,我在其中禁用了作业的自动执行,但我没有在@PropertySource 中包含该属性文件! :|
      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 2019-02-26
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 2017-05-17
      相关资源
      最近更新 更多