我在运行jar文件时在application.properties和spring.batch.job.enabled=false
中尝试了-Dspring.batch.job.enabled=false
。
但是@EnableBatchProcessing
会在应用程序启动时自动开始运行批处理作业。我如何调试这种情况?
TestConfiguration.class
@Configuration
@EnableBatchProcessing
public class TestConfiguration {...}
MainApplication
@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 boot而不是父
<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>
答案 0 :(得分:2)
我能够知道最新情况,我正在使用自定义阅读器/处理器/写入器。当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");
}
答案 1 :(得分:1)
我也面临同样的问题,属性'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工作&amp;弹出批处理作业未在服务器启动时初始化。
请注意,系统属性必须紧跟在standalone.xml中的extensions标记之后。