Ama*_*sta 5 spring-batch spring-boot
我在文档中读到:
“默认值为 ISOLATION_SERIALIZABLE,这可以防止同一作业意外并发执行”
但是,当我同时启动不同的作业(默认隔离级别为可串行化)时,出现错误:ORA-08177: can't serialize access for this transaction.这正常吗?
其次,要将默认隔离级别更改为 READ_COMMITTED,我知道我们无法在 application.properties 中更改此级别,并且我必须重新定义 BatchConfigurer。精确的 ?
使用 BasicBatchConfigurer,我必须定义一个显式构造函数(默认构造函数未定义隐式超级构造函数 BasicBatchConfigurer())。但是,我有错误:
Parameter 0 of constructor in MyBatchConfigurer required a bean of type 'org.springframework.boot.autoconfigure.batch.BatchProperties' that could not be found.
Run Code Online (Sandbox Code Playgroud)
如何创建:BatchProperties 属性、DataSource dataSource 和 TransactionManagerCustomizers transactionManagerCustomizers ?
这是我的代码:
PeopleApplication.java
@SpringBootApplication
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class })
public class PeopleApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(PeopleApplication.class)
.web(WebApplicationType.NONE)
.run(args);
int exitValue = SpringApplication.exit(ctx);
System.exit(exitValue);
}
}
Run Code Online (Sandbox Code Playgroud)
MyBatchConfigurer.java
@Component
@PropertySource("classpath:fileA.properties")
public class MyBatchConfigurer extends BasicBatchConfigurer implements CommandLineRunner, ExitCodeGenerator {
protected MyBatchConfigurer(BatchProperties properties, DataSource dataSource, TransactionManagerCustomizers transactionManagerCustomizers) {
super(properties, dataSource, transactionManagerCustomizers);
}
@Override
protected String determineIsolationLevel() {
return "ISOLATION_" + Isolation.READ_COMMITTED;
}
@Override
public void run(String... args) {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
问候。
回复 :
使用@EnableAutoConfiguration而不是:
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class })
Run Code Online (Sandbox Code Playgroud)
像这样,bean BatchProperties、DataSource 和 TransactionManagerCustomizers 将自动创建。
请参阅 Mahmoud 的回复,解释得很清楚
下面是使用示例,并且仅覆盖隔离级别
--application.properties
spring.application.name=SpringBatch
####### SPRING ##############
spring.main.banner-mode=off
spring.main.web-application-type=none
spring.batch.initialize-schema=always
spring.batch.job.enabled=false // Disable default if you want to control
########JDBC Datasource########
#connection timeout 10 min
spring.datasource.hikari.connection-timeout=600000
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.poolName=SpringBoot-HikariCP
spring.datasource.url=jdbc:oracle:thin:@ngecom.ae:1521:ngbilling
spring.datasource.username=ngbilling
springbatch.datasource.password=ngbilling
@SpringBootApplication
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(SsadapterApplication.class, args);
}
}
// Your manual batch scheduler
class BatchJobScheduler extends BasicBatchConfigurer {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private ApplicationArguments args;
@Autowired
private Job myJob;
protected BatchJobScheduler(BatchProperties properties, DataSource dataSource,
TransactionManagerCustomizers transactionManagerCustomizers) {
super(properties, dataSource, transactionManagerCustomizers);
}
@Override
protected String determineIsolationLevel() {
return "ISOLATION_" + Isolation.READ_COMMITTED;
}
//@Scheduled(cron = "${batch.cron}")
public void notScheduledJob() {
appId= args.getOptionValues("appId").get(0);
JobParameters params = new JobParametersBuilder().addLong("jobId"+appId, System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(myJob, params);
}
// Your batch Configuration And Spring will do everything for you to available
@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguration {
Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);
@Value("${batch.commit.chunk}")
private Integer chunkSize;
@Value("${batch.error.skipCount}")
private Integer skipErrorCount;
@Autowired
private Environment environment;
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
@Autowired
private ApplicationArguments args;
@Autowired
private JdbcTemplate jdbcTemplate;
@Bean
public Job myJob() throws Exception {
return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer())
.listener(new JobListener()).start(myStep()).build();
}
@Bean
public Step myStep() throws Exception {
return stepBuilderFactory.get("myStep").<InputObject, OutPutObject>chunk(chunkSize)
.reader(yourReader(null)).processor(yourProcessor()).writer(yourWriter())
//.faultTolerant()
//.skipLimit(skipErrorCount).skip(Exception.class)//.noSkip(FileNotFoundException.class)
.listener(invoiceSkipListener())
//.noRetry(Exception.class)
//.noRollback(Exception.class)
.build();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5333 次 |
| 最近记录: |