繁体   English   中英

Spring 启动 spring.batch.job.enabled=false 无法识别

[英]Spring boot spring.batch.job.enabled=false not able to recognize

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

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

测试配置.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我使用依赖作为 spring 引导而不是作为父

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

我知道最近发生了什么,我正在使用自定义阅读器/处理器/写入器。 当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");

}

我也面临同样的问题,属性'spring.batch.job.enabled = false'在启动时无法识别我们在属性文件中给出它。 可能是因为在批处理启动之前,属性可能没有加载到上下文中。

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

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

有了它,SUCCESSFULLY工作并且弹出批处理作业没有在服务器启动时初始化。

请注意,系统属性必须紧跟在standalone.xml中的extensions标记之后。

您可以将调试断点放在 Reader 中的 read() 方法中,然后退出该方法以跟踪调用 read() 的方法,继续前进,您将找到启动批处理作业的方法。

@Component
public class ActSpeedSubTaskReader implements ItemReader<ActSpeedTask> {

  

  @Override
  public ActSpeedTask read()
      throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
   // put the breakpoint here
   // read here
    return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM