I am fairly new to Spring batch so maybe I am missing something. After running this spring batch I cannot seem to persist the data(List) in database(Order_Summary_Details Table) This is my SpringBatchConfig class-
package com.spring.data.jpa.arkabrata.Config;
import com.spring.data.jpa.arkabrata.Entity.OrderSummary;
import com.spring.data.jpa.arkabrata.Repository.OrderSummaryRepository;
import com.spring.data.jpa.arkabrata.Service.OrderProductParticipantServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
@Configuration
@EnableBatchProcessing
public class SpringBatchConfig {
@Autowired
private OrderProductParticipantServiceImpl orderProductParticipantService;
public SpringBatchConfig() throws SQLException {
}
@Bean
@StepScope
public OrderProductReader itemReader(@Value("#{jobParameters['orderDate']}") String orderDate) throws Exception {
log.info("Entering OrderProductReader with orderDate value-->"+" "+orderDate);
List<Map<String, Object>> groupedData = orderProductParticipantService.getDataByDate(orderDate);
OrderProductReader orderProductReader = new OrderProductReader(groupedData);
log.info("Leaving OrderProductReader-->");
List<Map<String,Object>> returnedValue= orderProductReader.read();
System.out.println("returnedValue = " + returnedValue);
return orderProductReader;
}
@Bean
@StepScope
public OrderAmountProcessor processor() throws Exception {
log.info("Entering OrderAmountProcessor-->");
List<Map<String, Object>> groupedData = orderProductParticipantService.getDataByDate("1970-01-01");
OrderAmountProcessor orderAmountProcessor = new OrderAmountProcessor();
List<OrderSummary> returnedValue= orderAmountProcessor.process(groupedData);
System.out.println("returnedValue = " + returnedValue);
log.info("Leaving OrderAmountProcessor-->");
return orderAmountProcessor;
}
@Bean
@StepScope
public OrderSummaryItemWriter writer(){
log.info("At writer, getting inside OrderSummaryItemWriter--->");
return new OrderSummaryItemWriter();
}
@Bean
public Step step(JobRepository jobRepository,
PlatformTransactionManager transactionManager) throws Exception {
return new StepBuilder("step", jobRepository)
.<List<Map<String, Object>>, List<OrderSummary>>chunk(10, transactionManager)
.reader(itemReader("1970-01-01"))
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job myJob(JobRepository jobRepository, Step step) {
return new JobBuilder("myJob", jobRepository)
.start(step)
.build();
}
}
This is my ItemReader class-
package com.spring.data.jpa.arkabrata.Config;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import java.util.List;
import java.util.Map;
public class OrderProductReader implements ItemReader<List<Map<String, Object>>> {
private final List<Map<String, Object>> resultList;
public OrderProductReader(List<Map<String, Object>> resultList) {
this.resultList = resultList;
}
@Override
public List<Map<String, Object>> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
return resultList;
}
}
My ItemProcessor class-
package com.spring.data.jpa.arkabrata.Config;
import com.spring.data.jpa.arkabrata.Entity.OrderSummary;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemProcessor;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
public class OrderAmountProcessor implements ItemProcessor<List<Map<String, Object>>, List<OrderSummary>> {
@Override
public List<OrderSummary> process(List<Map<String, Object>> orderProductDetails) throws Exception {
List<OrderSummary> orderSummaryList = new ArrayList<>();
for (Map<String, Object> orderProductDetail : orderProductDetails)
{
log.info("I AM INSIDE PROCESS METHOD");
OrderSummary orderSummary = new OrderSummary();
Long totalQuantity = (Long) orderProductDetail.get("quantity");
BigDecimal price = (BigDecimal) orderProductDetail.get("price");
String productName = (String) orderProductDetail.get("productName");
Date orderDate = new Date(19700101);
if (orderProductDetail.get("orderDate") instanceof Date) {
orderDate = (Date) orderProductDetail.get("orderDate");
}
orderSummary.setProductQuantity(totalQuantity);
orderSummary.setAmount(price.multiply(BigDecimal.valueOf(totalQuantity)));
orderSummary.setOrderDate(orderDate);
orderSummary.setProductName(productName);
/*Long totalQuantity = ord
BigDecimal totalPrice = orderProductParticipant.getPrice();
orderSummary.setAmount(totalPrice.multiply(BigDecimal.valueOf(totalQuantity)));
orderSummary.setOrderDate(orderProductParticipant.getOrder().getOrderDate());
orderSummary.setProductName(orderProductParticipant.getProduct().getProductName());
orderSummary.setProductQuantity(orderProductParticipant.getQuantity());*/
orderSummaryList.add(orderSummary);
}
return orderSummaryList;
}
}
And my ItemWriter class-
package com.spring.data.jpa.arkabrata.Config;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.spring.data.jpa.arkabrata.Entity.OrderSummary;
import com.spring.data.jpa.arkabrata.Repository.OrderSummaryRepository;
import java.util.List;
@Component
public class OrderSummaryItemWriter implements ItemWriter<List<OrderSummary>> {
@Autowired
private OrderSummaryRepository orderSummaryRepository;
@Override
public void write(Chunk<? extends List<OrderSummary>> items) throws Exception {
for (List<OrderSummary> chunk : items) {
orderSummaryRepository.saveAll(chunk);
}
}
}
Can anyone please let me know what I am doing wrong? Even if I don't use stepscope and pass the parameter in ItemReader manually the debug points atleast go to the itemreader,writer and processor methods. The itemreader() method also returns value as expected. but from the processor it all goes down and the processor is not returning anything apparently so the writer is also saving nothing.
If i use stepScope annotation then the debug doesn't even go inside those methods. Can you help me how to properly setup these classes so I can persist data in database properly?