|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.sample.jpa; |
| 17 | + |
| 18 | +import java.math.BigDecimal; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import javax.sql.DataSource; |
| 22 | +import jakarta.persistence.EntityManagerFactory; |
| 23 | + |
| 24 | +import org.springframework.batch.core.Job; |
| 25 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 26 | +import org.springframework.batch.core.configuration.annotation.StepScope; |
| 27 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 28 | +import org.springframework.batch.core.repository.JobRepository; |
| 29 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 30 | +import org.springframework.batch.item.data.RepositoryItemReader; |
| 31 | +import org.springframework.batch.item.data.RepositoryItemWriter; |
| 32 | +import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder; |
| 33 | +import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder; |
| 34 | +import org.springframework.batch.item.database.JpaItemWriter; |
| 35 | +import org.springframework.batch.item.database.JpaPagingItemReader; |
| 36 | +import org.springframework.batch.sample.domain.trade.CustomerCredit; |
| 37 | +import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor; |
| 38 | +import org.springframework.beans.factory.annotation.Value; |
| 39 | +import org.springframework.context.annotation.Bean; |
| 40 | +import org.springframework.context.annotation.Configuration; |
| 41 | +import org.springframework.data.domain.Sort; |
| 42 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 43 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 44 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 45 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 46 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 47 | +import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; |
| 48 | +import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; |
| 49 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 50 | + |
| 51 | +/** |
| 52 | + * Hibernate JPA dialect does not support custom tx isolation levels => overwrite with |
| 53 | + * ISOLATION_DEFAULT. |
| 54 | + * |
| 55 | + * @author Mahmoud Ben Hassine |
| 56 | + */ |
| 57 | +@Configuration |
| 58 | +@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT") |
| 59 | +@EnableJpaRepositories(basePackages = "org.springframework.batch.sample.jpa") |
| 60 | +public class JpaRepositoryJobConfiguration { |
| 61 | + |
| 62 | + @Bean |
| 63 | + @StepScope |
| 64 | + public RepositoryItemReader<CustomerCredit> itemReader(@Value("#{jobParameters['credit']}") Double credit, |
| 65 | + CustomerCreditPagingAndSortingRepository repository) { |
| 66 | + return new RepositoryItemReaderBuilder<CustomerCredit>().name("itemReader") |
| 67 | + .pageSize(2) |
| 68 | + .methodName("findByCreditGreaterThan") |
| 69 | + .repository(repository) |
| 70 | + .arguments(BigDecimal.valueOf(credit)) |
| 71 | + .sorts(Map.of("id", Sort.Direction.ASC)) |
| 72 | + .build(); |
| 73 | + } |
| 74 | + |
| 75 | + @Bean |
| 76 | + public RepositoryItemWriter<CustomerCredit> itemWriter(CustomerCreditCrudRepository repository) { |
| 77 | + return new RepositoryItemWriterBuilder<CustomerCredit>().repository(repository).methodName("save").build(); |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + public Job job(JobRepository jobRepository, JpaTransactionManager transactionManager, |
| 82 | + RepositoryItemReader<CustomerCredit> itemReader, RepositoryItemWriter<CustomerCredit> itemWriter) { |
| 83 | + return new JobBuilder("ioSampleJob", jobRepository) |
| 84 | + .start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager) |
| 85 | + .reader(itemReader) |
| 86 | + .processor(new CustomerCreditIncreaseProcessor()) |
| 87 | + .writer(itemWriter) |
| 88 | + .build()) |
| 89 | + .build(); |
| 90 | + } |
| 91 | + |
| 92 | + // Infrastructure beans |
| 93 | + |
| 94 | + @Bean |
| 95 | + public DataSource dataSource() { |
| 96 | + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) |
| 97 | + .addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") |
| 98 | + .addScript("/org/springframework/batch/core/schema-hsqldb.sql") |
| 99 | + .addScript("/org/springframework/batch/sample/jpa/sql/schema.sql") |
| 100 | + .build(); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { |
| 105 | + return new JpaTransactionManager(entityManagerFactory); |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + public EntityManagerFactory entityManagerFactory(PersistenceUnitManager persistenceUnitManager, |
| 110 | + DataSource dataSource) { |
| 111 | + LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); |
| 112 | + factoryBean.setDataSource(dataSource); |
| 113 | + factoryBean.setPersistenceUnitManager(persistenceUnitManager); |
| 114 | + factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); |
| 115 | + factoryBean.afterPropertiesSet(); |
| 116 | + return factoryBean.getObject(); |
| 117 | + } |
| 118 | + |
| 119 | + @Bean |
| 120 | + public PersistenceUnitManager persistenceUnitManager(DataSource dataSource) { |
| 121 | + DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); |
| 122 | + persistenceUnitManager.setDefaultDataSource(dataSource); |
| 123 | + persistenceUnitManager.afterPropertiesSet(); |
| 124 | + return persistenceUnitManager; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments