Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. @Bean
  2. ItemWriter<Event> writer() throws SQLException {
  3. return new CustomJdbcBatchDataWriter();
  4. }
  5.  
  6. @Bean
  7. public TaskExecutor taskExecutor(){
  8. SimpleAsyncTaskExecutor asyncTaskExecutor=new SimpleAsyncTaskExecutor("spring_batch");
  9. asyncTaskExecutor.setConcurrencyLimit(threadsAmount);
  10. return asyncTaskExecutor;
  11. }
  12.  
  13. @Bean
  14. public Job importUserJob(JobCompletionNotificationListener listener) throws Exception {
  15. return jobBuilderFactory.get("importUserJob")
  16. .incrementer(new RunIdIncrementer())
  17. .listener(listener)
  18. .flow(step1())
  19. .end()
  20. .build();
  21. }
  22.  
  23. @Bean
  24. public Step step1() throws Exception {
  25. return stepBuilderFactory.get("step1")
  26. .<Event, Event>chunk(chunkSize)
  27. .reader(reader())
  28. .processor(processor())
  29. .writer(writer())
  30. .taskExecutor(taskExecutor())
  31. .build();
  32. }
  33.  
  34. @Override
  35. public void write(List<? extends Event> items) throws Exception {
  36. try (
  37. Connection connection = DriverManager.getConnection(
  38. "jdbc:mysql://localhost:3306/batch?useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true",
  39. "root", "123123") ) {
  40. connection.setAutoCommit(false);
  41.  
  42.  
  43. String sql = "INSERT INTO events VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  44. PreparedStatement ps = connection.prepareStatement(sql);
  45.  
  46. for (Event p : items) {
  47. try {
  48. ps.setString(1, p.getId());
  49. //Setting rest of data into prepared statement
  50. ...
  51. ps.addBatch();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. ps.executeBatch();
  58. connection.commit();
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement