Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's new in Spring Batch 5?

What's new in Spring Batch 5?

Mahmoud Ben Hassine

May 18, 2023
Tweet

More Decks by Mahmoud Ben Hassine

Other Decks in Technology

Transcript

  1. DEPENDENCIES UPDATES • Spring Framework 6 • Spring Integration 6

    • Spring Data 3 • Spring AMQP 3 • Spring for Apache Kafka 3 • Spring LDAP 3 • Spring Retry 2 • Micrometer 1.10
  2. DEPENDENCIES UPDATES • Spring Framework 6 • Spring Integration 6

    • Spring Data 3 • Spring AMQP 3 • Spring for Apache Kafka 3 • Spring LDAP 3 • Spring Retry 2 • Micrometer 1.10 https://github.com/spring-projects/spring-framework/wiki
  3. JAVA VERSION UPDATES • Java 17+ • Jakarta EE 9+

    https://www.youtube.com/watch?v=P7SI9mLwiqw
  4. JAVA VERSION UPDATES • Java 17+ • Jakarta EE 9+

    https://www.youtube.com/watch?v=P7SI9mLwiqw "You should expect to see these benefits without having to change anything in your code, just by updating to a newer Java version." Aurelio García-Ribeyro - Senior Director of Product Management - Java Platform Group
  5. JAVA RECORDS SUPPORT UPDATES • Several improvements since 4.3 o

    Bug fixes o Addition of RecordFieldExtractor for Java Records (similar to BeanWrapperFieldExtractor for Java Beans) • Usage of java.lang.Record APIs to improve configuration o Automatic configuration of mappers in item readers based on item type o Automatic configuration of extractors in item writers based on item type
  6. JAVA RECORDS SUPPORT UPDATES // given class Person {int id;

    String name;} // when FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>() .resource(new FileSystemResource("/data/persons.csv")) .targetType(Person.class) // set other properties .build(); // then // assertThat(reader.getFieldSetMapper()).isInstanceOf(BeanWrapperFieldSetMapper.class);
  7. JAVA RECORDS SUPPORT UPDATES // given record Person (int id,

    String name) {} // when FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>() .resource(new FileSystemResource("/data/persons.csv")) .targetType(Person.class) // set other properties .build(); // then // assertThat(reader.getFieldSetMapper()).isInstanceOf(RecordFieldSetMapper.class);
  8. JAVA RECORDS SUPPORT UPDATES public class FlatFileItemReaderBuilder<T> { private Class<T>

    targetType; public FlatFileItemReader<T> build() { FlatFileItemReader<T> reader = new FlatFileItemReader(); if (this.targetType.isRecord()) { reader.setFieldSetMapper(new RecordFieldSetMapper(this.targetType); } else { reader.setFieldSetMapper(new BeanWrapperFieldSetMapper(this.targetType); } } }
  9. @EnableBatchProcessing IMPROVEMENTS @Configuration @EnableBatchProcessing public class MyJobConfiguration { @Bean public

    Job job(JobRepository jobRepository) { return new JobBuilder(“myJob”, jobRepository) // define job flow as needed .build(); } }
  10. @EnableBatchProcessing IMPROVEMENTS @Configuration @EnableBatchProcessing public class MyJobConfiguration { @Bean public

    Job job(JobRepository jobRepository) { … } @Bean public BatchConfigurer batchConfigurer(DataSource dataSource) { return new DefaultBatchConfigurer() { @Override public JobRepository getJobRepository() { return new MyCustomJobRepository(dataSource); } }; } }
  11. @EnableBatchProcessing IMPROVEMENTS @Configuration @EnableBatchProcessing(dataSourceRef = "batchDataSource") public class MyJobConfiguration {

    @Bean public Job job(JobRepository jobRepository) { … } @Bean public DataSource batchDataSource() { return new MyDataSource(); } }
  12. @EnableBatchProcessing IMPROVEMENTS @Configuration @EnableBatchProcessing public class MyJobConfiguration { @Bean public

    BatchConfigurer batchConfigurer() { return new DefaultBatchConfigurer() { private Jackson2ExecutionContextStringSerializer customSerializer() { Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer(); // customize serializer return serializer; } @Override public JobRepository getJobRepository() { JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean(); factoryBean.setSerializer(customSerializer()); try { return factoryBean.getObject(); } catch (Exception e) { throw new RuntimeException(e); } } … @Override public JobExplorer getJobExplorer() { JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean(); factoryBean.setSerializer(customSerializer()); // set other properties try { return factoryBean.getObject(); } catch (Exception e) { throw new RuntimeException(e); } } }; }
  13. @EnableBatchProcessing IMPROVEMENTS @Configuration @EnableBatchProcessing(executionContextSerializerRef = "myCustomSerializer") public class MyJobConfiguration {

    @Bean public Jackson2ExecutionContextStringSerializer myCustomSerializer() { Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer(); // customize serializer return serializer; } }
  14. ADDITION OF DefaultBatchConfiguration @Configuration public class MyJobConfiguration extends DefaultBatchConfiguration {

    @Bean public Job job(JobRepository jobRepository) { return new JobBuilder(“myJob”, jobRepository) // define job flow as needed .build(); } }
  15. ADDITION OF DefaultBatchConfiguration @Configuration public class MyJobConfiguration extends DefaultBatchConfiguration {

    @Bean public Job job(JobRepository jobRepository) { return new JobBuilder(“myJob”, jobRepository) // define job flow as needed .build(); } @Override public DataSource getDataSource() { return new MyDataSource(); } }
  16. PARAMETER CONVERSION IMPROVEMENTS Notation in v4: [+|-]parameterName(parameterType)=parameterValue $>java -jar my-job.jar

    -schedule.date(date)=2023-01-26 Incompatible with Spring Boot Incompatible with Environment variables
  17. PARAMETER CONVERSION IMPROVEMENTS Notation in v4: [+|-]parameterName(parameterType)=parameterValue $>java -jar my-job.jar

    -schedule.date(date)=2023-01-26 Incompatible with Spring Boot Incompatible with Environment variables Ambiguous parameter type
  18. PARAMETER CONVERSION IMPROVEMENTS Notation in v5 (extended): parameterName='{ "value": "parameterValue",

    "type": "parameterType", "identifying": "booleanValue" }' $>java -jar my-job.jar schedule.date='{value:"2023-01-26", type:"java.time.LocalDate"}'
  19. PARAMETER CONVERSION IMPROVEMENTS Notation in v5 (extended): parameterName=‘{ "value": "parameterValue",

    "type":"parameterType", "identifying": "booleanValue” }' $>java -jar my-job.jar schedule.date='{value:"2023-01-26", type:"java.time.LocalDate"}'
  20. PARAMETER TYPES IMPROVEMENTS • Supported types in v4: String, Long,

    Double, Date • Supported types in v5: Any type! $>java -jar export-orders-job.jar customer=foo,my.domain.Customer
  21. HOW TO SUPPORT CUSTOM PARAMETER TYPES? @Configuration @EnableBatchProcessing public class

    MyJobConfiguration { @Bean public Job job(JobRepository jobRepository) { return new MyJob(jobRepository); } }
  22. HOW TO SUPPORT CUSTOM PARAMETER TYPES? @Configuration @EnableBatchProcessing(conversionServiceRef = "myCustomConversionService")

    public class MyJobConfiguration { @Bean public Job job(JobRepository jobRepository) { return new MyJob(jobRepository); } @Bean public ConfigurableConversionService myCustomConversionService() { DefaultConversionService conversionService = new DefaultConversionService(); // add converters from String to my.domain.Customer and vice-versa in conversionService return conversionService; } }
  23. EXECUTION CONTEXT IMPROVEMENTS • Serialization format updates ◦ Base64 instead

    of JSON ◦ Jackson is now optional • Meta-data improvements ◦ New “batch.version” attribute in the context { batch.stepType: “..”, batch.restart: “..”, .. batch.version: “5.0.0” }
  24. SCHEMA UPDATES • Usage of sequences instead of tables for

    SQLServer • Addition of column CREATE_TIME in BATCH_STEP_EXECUTION • Addition of PARAMETER_NAME, PARAMETER_TYPE and PARAMETER_VALUE in BATCH_JOB_EXECUTION_PARAMS • Removal of BATCH_CONFIGURATION_LOCATION in BATCH_JOB_EXECUTION https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide
  25. BATCH METRICS IMPROVEMENTS • Several improvements since 4.3 ◦ Bug

    fixes ◦ Consistent tag names • Upgraded the usage of Observability APIs to Micrometer 1.10 • New metrics ◦ Currently active step ◦ Job launch count
  26. HOW TO ENABLE BATCH METRICS? @Configuration @EnableBatchProcessing public class MyJobConfiguration

    { @Bean public Job job() { return new MyJob(); } @Bean public ObservationRegistry observationRegistry() { ObservationRegistry registry = ObservationRegistry.create(); // configure the registry as needed return registry; } }
  27. HOW TO ENABLE BATCH TRACING? @Configuration @EnableBatchProcessing public class MyJobConfiguration

    { @Bean public Job job() { return new MyJob(); } @Bean public ObservationRegistry observationRegistry() { ObservationRegistry registry = ObservationRegistry.create(); // configure the registry as needed return registry; } }
  28. HOW TO ENABLE BATCH TRACING? @Configuration @EnableBatchProcessing public class MyJobConfiguration

    { @Bean public Job job() { return new MyJob(); } @Bean public ObservationRegistry observationRegistry(Tracer tracer) { ObservationRegistry registry = ObservationRegistry.create(); registry.observationConfig().observationHandler( new DefaultTracingObservationHandler(tracer)); return registry; } }
  29. GRAALVM NATIVE SUPPORT IMPROVEMENTS • Several improvements since v4.3 ◦

    Bug fixes ◦ Performance improvements ◦ Tooling improvements • Addition of native hints for AOT processing by Spring ◦ Reflection hints ◦ Resource hints ◦ Proxy hints ◦ Serialization hints
  30. HELLO-WORLD NATIVE PERFORMANCE 1248 44 0 200 400 600 800

    1000 1200 1400 Startup time (ms) JVM Native 26 1 0 5 10 15 20 25 30 Total execution time (ms) JVM Native https://github.com/fmbenhassine/spring-batch-lab/tree/main/talks/spring-batch-native/hello-world
  31. FILE-TO-DB (1M ITEMS) NATIVE PERFORMANCE 905 41 0 100 200

    300 400 500 600 700 800 900 1000 Startup time (ms) JVM Native 37 15 0 5 10 15 20 25 30 35 40 Total execution time (s) JVM Native https://github.com/fmbenhassine/spring-batch-lab/tree/main/talks/spring-batch-native/file-to-db
  32. GRAALVM NATIVE IMAGE COMPILATION $> curl https://start.spring.io/starter.zip \ -d dependencies=h2,batch,native

    \ -d type=maven-project \ -o my-batch-job.zip $> unzip my-batch-job.zip $> # EDIT your Spring Batch app as needed &> mvn -Pnative native:compile &> ./target/my-batch-job
  33. OTHER FEATURES • New Maven Bill Of Materials • UTF-8

    by default in all areas of the framework • Transaction support in JobExplorer and JobOperator • Automatic registration of JobOperator with @EnableBatchProcessing • SystemCommandTasklet improvements (new CommandRunner interface) • Test utilities improvements + migration to Junit 5 • New documentation backend
  34. REMOVED FEATURES • SQLFire support removal • GemFire support removal

    • JSR-352 implementation removal • Deprecated APIs removal https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide#pruning https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide#removed-apis
  35. SUPPORT POLICY • Same support policy as VMware Tanzu •

    Same support period as Spring Boot ◦ Spring Batch 5.0 (Spring Boot 3.0) ◦ Spring Batch 4.3 (Spring Boot 2.7) https://spring.io/projects/spring-batch#support
  36. WHAT’S NEXT? • Support for 5.0.x and 4.3.x • Spring

    Batch 5.1 (Spring Framework 6.1) ◦ Better getting started experience ◦ Virtual Threads (Project Loom) support ◦ Revisit the chunk-oriented processing model implementation Those are goals, not commitments!
  37. 60 Our vision Empower Spring developers to continuously learn, innovate

    and solve complex problems. Core features • Project-based courses with hands-on labs • In-browser code editing and debugging • Certification prep courses • Spring Certified Professional certification Sign up today Get started for free at https://spring.academy. For conference attendees Use SPRINGIO-2023 at checkout by June 4th for a 20% discount on a Spring Academy Pro subscription.
  38. THANK YOU! Q&A Mahmoud Ben Hassine Spring Batch Lead, VMware

    @fmbenhassine https://docs.spring.io/spring-batch/docs/5.0.0/reference/html/whatsnew.html https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide