Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. @Configuration
  2. public class DataSourceConfiguration {
  3.  
  4. @Bean(name = "mainDataSource")
  5. @Primary
  6. @ConfigurationProperties(prefix="spring.datasource")
  7. public DataSource mainDataSource(){
  8. return DataSourceBuilder.create().build();
  9. }
  10.  
  11. @Bean(name = "batchDataSource")
  12. public DataSource batchDataSource( @Value("${batch.datasource.url}") String url ){
  13. return DataSourceBuilder.create().url( url ).build();
  14. }
  15. }
  16.  
  17. # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
  18. spring.datasource.url=jdbc:mariadb://localhost:3306/batch_poc
  19. spring.datasource.username=root
  20. spring.datasource.password=
  21. spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
  22. spring.datasource.max-age=10000
  23.  
  24. spring.datasource.initialize=false
  25.  
  26. # JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
  27. spring.jpa.generate-ddl=false
  28. spring.jpa.show-sql=true
  29. spring.jpa.database=MYSQL
  30.  
  31. # SPRING BATCH (BatchDatabaseInitializer)
  32. spring.batch.initializer.enabled=false
  33.  
  34. # ----------------------------------------
  35. # PROJECT SPECIFIC PROPERTIES
  36. # ----------------------------------------
  37.  
  38. # BATCH DATASOURCE
  39. batch.datasource.url=jdbc:hsqldb:file:C:/tmp/hsqldb/batchdb
  40.  
  41. @Configuration
  42. @EnableBatchProcessing
  43. public class BatchConfiguration {
  44.  
  45. private static final Logger LOG = Logger.getLogger( BatchConfiguration.class );
  46.  
  47. @Bean
  48. public BatchConfigurer configurer(){
  49. return new CustomBatchConfigurer();
  50. }
  51.  
  52. @Bean
  53. public Job importElementsJob( JobBuilderFactory jobs, Step step1 ){
  54. return jobs.get("importElementsJob")
  55. .incrementer( new RunIdIncrementer() )
  56. .flow( step1 )
  57. .end()
  58. .build();
  59. }
  60.  
  61. @Bean
  62. public Step step1( StepBuilderFactory stepBuilderFactory, ItemReader<InputElement> reader,
  63. ItemWriter<List<Entity>> writer, ItemProcessor<InputElement, List<Entity>> processor ){
  64.  
  65. return stepBuilderFactory.get("step1")
  66. .<InputElement, List<Entity>> chunk(100)
  67. .reader( reader )
  68. .processor( processor )
  69. .writer( writer )
  70. .build();
  71. }
  72.  
  73. @Bean
  74. public ItemReader<InputElement> reader() throws IOException {
  75. return new CustomItemReader();
  76. }
  77.  
  78. @Bean
  79. public ItemProcessor<InputElement, List<Entity>> processor(){
  80. return new CutsomItemProcessor();
  81. }
  82.  
  83. @Bean
  84. public ItemWriter<List<Entity>> writer(){
  85. return new CustomItemWriter();
  86. }
  87.  
  88. }
  89.  
  90. public class CustomBatchConfigurer extends DefaultBatchConfigurer {
  91.  
  92. @Override
  93. @Autowired
  94. public void setDataSource( @Qualifier("batchDataSource") DataSource dataSource) {
  95. super.setDataSource(dataSource);
  96. }
  97.  
  98. }
  99.  
  100. public class CustomItemWriter implements ItemWriter<List<Entity>> {
  101.  
  102. private static final Logger LOG = Logger.getLogger( EntityWriter.class );
  103.  
  104. @Autowired
  105. private EntityRepository entityRepository;
  106.  
  107. @Override
  108. public void write(List<? extends List<Entity>> items)
  109. throws Exception {
  110. if( items != null && !items.isEmpty() ){
  111.  
  112. for( List<Entity> entities : items ){
  113. for( Entity entity : entities ){
  114. Entity fromDb = entityRepository.findById( entity.getId() );
  115.  
  116. // Insert
  117. if( fromDb == null ){
  118. entityRepository.save( entity );
  119. }
  120.  
  121. // Update
  122. else {
  123. // TODO : entityManager.merge()
  124. }
  125. }
  126. }
  127.  
  128. }
  129. }
  130.  
  131. }
  132.  
  133. public class CustomBatchConfigurer implements BatchConfigurer {
  134.  
  135. private static final Logger LOG = Logger.getLogger( CustomBatchConfigurer.class );
  136.  
  137. private final EntityManagerFactory entityManagerFactory;
  138.  
  139. private PlatformTransactionManager transactionManager;
  140.  
  141. private JobRepository jobRepository;
  142.  
  143. private JobLauncher jobLauncher;
  144.  
  145. private JobExplorer jobExplorer;
  146.  
  147. /**
  148. * Create a new {@link CustomBatchConfigurer} instance.
  149. * @param entityManagerFactory the entity manager factory
  150. */
  151. public CustomBatchConfigurer( EntityManagerFactory entityManagerFactory ) {
  152. this.entityManagerFactory = entityManagerFactory;
  153. }
  154.  
  155. @Override
  156. public JobRepository getJobRepository() {
  157. return this.jobRepository;
  158. }
  159.  
  160. @Override
  161. public PlatformTransactionManager getTransactionManager() {
  162. return this.transactionManager;
  163. }
  164.  
  165. @Override
  166. public JobLauncher getJobLauncher() {
  167. return this.jobLauncher;
  168. }
  169.  
  170. @Override
  171. public JobExplorer getJobExplorer() throws Exception {
  172. return this.jobExplorer;
  173. }
  174.  
  175. @PostConstruct
  176. public void initialize() {
  177. try {
  178. // transactionManager:
  179. LOG.info("Forcing the use of a JPA transactionManager");
  180. if( this.entityManagerFactory == null ){
  181. throw new Exception("Unable to initialize batch configurer : entityManagerFactory must not be null");
  182. }
  183. this.transactionManager = new JpaTransactionManager( this.entityManagerFactory );
  184.  
  185. // jobRepository:
  186. LOG.info("Forcing the use of a Map based JobRepository");
  187. MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean( this.transactionManager );
  188. jobRepositoryFactory.afterPropertiesSet();
  189. this.jobRepository = jobRepositoryFactory.getObject();
  190.  
  191. // jobLauncher:
  192. SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
  193. jobLauncher.setJobRepository(getJobRepository());
  194. jobLauncher.afterPropertiesSet();
  195. this.jobLauncher = jobLauncher;
  196.  
  197. // jobExplorer:
  198. MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
  199. jobExplorerFactory.afterPropertiesSet();
  200. this.jobExplorer = jobExplorerFactory.getObject();
  201. }
  202. catch (Exception ex) {
  203. throw new IllegalStateException("Unable to initialize Spring Batch", ex);
  204. }
  205. }
  206.  
  207. }
  208.  
  209. @Configuration
  210. @EnableBatchProcessing
  211. public class BatchConfiguration {
  212.  
  213. @Bean
  214. public BatchConfigurer configurer( EntityManagerFactory entityManagerFactory ){
  215. return new CustomBatchConfigurer( entityManagerFactory );
  216. }
  217.  
  218. [...]
  219.  
  220. }
  221.  
  222. # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
  223. spring.datasource.url=jdbc:mariadb://localhost:3306/inotr_poc
  224. spring.datasource.username=root
  225. spring.datasource.password=adsn123
  226. spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
  227. spring.datasource.max-age=10000
  228.  
  229. spring.datasource.initialize=true
  230.  
  231. # JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
  232. spring.jpa.generate-ddl=false
  233. spring.jpa.show-sql=true
  234. spring.jpa.database=MYSQL
  235.  
  236. # SPRING BATCH (BatchDatabaseInitializer)
  237. spring.batch.initializer.enabled=false
  238.  
  239. @PostConstruct
  240. public void initialize() {
  241. try {
  242. // transactionManager:
  243. LOGGER.info("Forcing the use of ResourcelessTransactionManager for batch db");
  244.  
  245. this.transactionManager = new ResourcelessTransactionManager();
  246. //the rest of the code follows...
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement