Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Bean
- ItemWriter<Event> writer() throws SQLException {
- return new CustomJdbcBatchDataWriter();
- }
- @Bean
- public TaskExecutor taskExecutor(){
- SimpleAsyncTaskExecutor asyncTaskExecutor=new SimpleAsyncTaskExecutor("spring_batch");
- asyncTaskExecutor.setConcurrencyLimit(threadsAmount);
- return asyncTaskExecutor;
- }
- @Bean
- public Job importUserJob(JobCompletionNotificationListener listener) throws Exception {
- return jobBuilderFactory.get("importUserJob")
- .incrementer(new RunIdIncrementer())
- .listener(listener)
- .flow(step1())
- .end()
- .build();
- }
- @Bean
- public Step step1() throws Exception {
- return stepBuilderFactory.get("step1")
- .<Event, Event>chunk(chunkSize)
- .reader(reader())
- .processor(processor())
- .writer(writer())
- .taskExecutor(taskExecutor())
- .build();
- }
- @Override
- public void write(List<? extends Event> items) throws Exception {
- try (
- Connection connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/batch?useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true",
- "root", "123123") ) {
- connection.setAutoCommit(false);
- String sql = "INSERT INTO events VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- PreparedStatement ps = connection.prepareStatement(sql);
- for (Event p : items) {
- try {
- ps.setString(1, p.getId());
- //Setting rest of data into prepared statement
- ...
- ps.addBatch();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- ps.executeBatch();
- connection.commit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement