Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- public class DataSourceConfiguration {
- @Bean(name = "mainDataSource")
- @Primary
- @ConfigurationProperties(prefix="spring.datasource")
- public DataSource mainDataSource(){
- return DataSourceBuilder.create().build();
- }
- @Bean(name = "batchDataSource")
- public DataSource batchDataSource( @Value("${batch.datasource.url}") String url ){
- return DataSourceBuilder.create().url( url ).build();
- }
- }
- # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
- spring.datasource.url=jdbc:mariadb://localhost:3306/batch_poc
- spring.datasource.username=root
- spring.datasource.password=
- spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
- spring.datasource.max-age=10000
- spring.datasource.initialize=false
- # JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
- spring.jpa.generate-ddl=false
- spring.jpa.show-sql=true
- spring.jpa.database=MYSQL
- # SPRING BATCH (BatchDatabaseInitializer)
- spring.batch.initializer.enabled=false
- # ----------------------------------------
- # PROJECT SPECIFIC PROPERTIES
- # ----------------------------------------
- # BATCH DATASOURCE
- batch.datasource.url=jdbc:hsqldb:file:C:/tmp/hsqldb/batchdb
- @Configuration
- @EnableBatchProcessing
- public class BatchConfiguration {
- private static final Logger LOG = Logger.getLogger( BatchConfiguration.class );
- @Bean
- public BatchConfigurer configurer(){
- return new CustomBatchConfigurer();
- }
- @Bean
- public Job importElementsJob( JobBuilderFactory jobs, Step step1 ){
- return jobs.get("importElementsJob")
- .incrementer( new RunIdIncrementer() )
- .flow( step1 )
- .end()
- .build();
- }
- @Bean
- public Step step1( StepBuilderFactory stepBuilderFactory, ItemReader<InputElement> reader,
- ItemWriter<List<Entity>> writer, ItemProcessor<InputElement, List<Entity>> processor ){
- return stepBuilderFactory.get("step1")
- .<InputElement, List<Entity>> chunk(100)
- .reader( reader )
- .processor( processor )
- .writer( writer )
- .build();
- }
- @Bean
- public ItemReader<InputElement> reader() throws IOException {
- return new CustomItemReader();
- }
- @Bean
- public ItemProcessor<InputElement, List<Entity>> processor(){
- return new CutsomItemProcessor();
- }
- @Bean
- public ItemWriter<List<Entity>> writer(){
- return new CustomItemWriter();
- }
- }
- public class CustomBatchConfigurer extends DefaultBatchConfigurer {
- @Override
- @Autowired
- public void setDataSource( @Qualifier("batchDataSource") DataSource dataSource) {
- super.setDataSource(dataSource);
- }
- }
- public class CustomItemWriter implements ItemWriter<List<Entity>> {
- private static final Logger LOG = Logger.getLogger( EntityWriter.class );
- @Autowired
- private EntityRepository entityRepository;
- @Override
- public void write(List<? extends List<Entity>> items)
- throws Exception {
- if( items != null && !items.isEmpty() ){
- for( List<Entity> entities : items ){
- for( Entity entity : entities ){
- Entity fromDb = entityRepository.findById( entity.getId() );
- // Insert
- if( fromDb == null ){
- entityRepository.save( entity );
- }
- // Update
- else {
- // TODO : entityManager.merge()
- }
- }
- }
- }
- }
- }
- public class CustomBatchConfigurer implements BatchConfigurer {
- private static final Logger LOG = Logger.getLogger( CustomBatchConfigurer.class );
- private final EntityManagerFactory entityManagerFactory;
- private PlatformTransactionManager transactionManager;
- private JobRepository jobRepository;
- private JobLauncher jobLauncher;
- private JobExplorer jobExplorer;
- /**
- * Create a new {@link CustomBatchConfigurer} instance.
- * @param entityManagerFactory the entity manager factory
- */
- public CustomBatchConfigurer( EntityManagerFactory entityManagerFactory ) {
- this.entityManagerFactory = entityManagerFactory;
- }
- @Override
- public JobRepository getJobRepository() {
- return this.jobRepository;
- }
- @Override
- public PlatformTransactionManager getTransactionManager() {
- return this.transactionManager;
- }
- @Override
- public JobLauncher getJobLauncher() {
- return this.jobLauncher;
- }
- @Override
- public JobExplorer getJobExplorer() throws Exception {
- return this.jobExplorer;
- }
- @PostConstruct
- public void initialize() {
- try {
- // transactionManager:
- LOG.info("Forcing the use of a JPA transactionManager");
- if( this.entityManagerFactory == null ){
- throw new Exception("Unable to initialize batch configurer : entityManagerFactory must not be null");
- }
- this.transactionManager = new JpaTransactionManager( this.entityManagerFactory );
- // jobRepository:
- LOG.info("Forcing the use of a Map based JobRepository");
- MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean( this.transactionManager );
- jobRepositoryFactory.afterPropertiesSet();
- this.jobRepository = jobRepositoryFactory.getObject();
- // jobLauncher:
- SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
- jobLauncher.setJobRepository(getJobRepository());
- jobLauncher.afterPropertiesSet();
- this.jobLauncher = jobLauncher;
- // jobExplorer:
- MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
- jobExplorerFactory.afterPropertiesSet();
- this.jobExplorer = jobExplorerFactory.getObject();
- }
- catch (Exception ex) {
- throw new IllegalStateException("Unable to initialize Spring Batch", ex);
- }
- }
- }
- @Configuration
- @EnableBatchProcessing
- public class BatchConfiguration {
- @Bean
- public BatchConfigurer configurer( EntityManagerFactory entityManagerFactory ){
- return new CustomBatchConfigurer( entityManagerFactory );
- }
- [...]
- }
- # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
- spring.datasource.url=jdbc:mariadb://localhost:3306/inotr_poc
- spring.datasource.username=root
- spring.datasource.password=adsn123
- spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
- spring.datasource.max-age=10000
- spring.datasource.initialize=true
- # JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
- spring.jpa.generate-ddl=false
- spring.jpa.show-sql=true
- spring.jpa.database=MYSQL
- # SPRING BATCH (BatchDatabaseInitializer)
- spring.batch.initializer.enabled=false
- @PostConstruct
- public void initialize() {
- try {
- // transactionManager:
- LOGGER.info("Forcing the use of ResourcelessTransactionManager for batch db");
- this.transactionManager = new ResourcelessTransactionManager();
- //the rest of the code follows...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement