Spring Batch — call existing Service

On this example, we’ll see how to call the existing service and get its data in the ItemReader and process it.

ItemReaderAdapter — Invokes a custom method on a delegate plain old Java object which itself provides an item.

CustomerService.java

@Component
public class CustomerService {
private int curIndex;
private List<Customer> customers;


private String[] firstNames = {"Michael", "Warren", "Ann", "Terrence", "Erica", "Laura", "Steve", "Larry"};
private String middleInitial = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String[] lastNames = {"Gates", "Darrow", "Donnelly", "Jobs", "Buffett", "Ellison", "Obama"};

private String[] streets = {"4th Street", "Wall Street", "Fifth Avenue", "Mt. Lee Drive", "Jeopardy Lane",
"Infinite Loop Drive", "Farnam Street", "Isabella Ave", "S. Greenwood Ave"};

private String[] cities = {"Chicago", "New York", "Hollywood", "Aurora", "Omaha", "Atherton"};
private String[] states = {"IL", "NY", "CA", "NE"};

private Random generator = new Random();

public CustomerService() {
curIndex = 0;
customers = new ArrayList<>();
for (int i = 0; i < 100; i++) {
customers.add(buildCustomer());
}
}

private Customer buildCustomer() {
Customer customer = new Customer();
customer.setId((long) generator.nextInt(Integer.MAX_VALUE));
customer.setFirstName(firstNames[generator.nextInt(firstNames.length - 1)]);
customer.setMiddleInitial(String.valueOf(middleInitial.charAt(generator.nextInt(middleInitial.length() - 1))));
customer.setLastName(lastNames[generator.nextInt(lastNames.length - 1)]);
customer.setAddress(generator.nextInt(9999) + " " + streets[generator.nextInt(streets.length - 1)]);
customer.setCity(cities[generator.nextInt(cities.length - 1)]);
customer.setState(states[generator.nextInt(states.length - 1)]);
customer.setZipCode(String.valueOf(generator.nextInt(99999)));
return customer;
}

public Customer getCustomer() {
Customer cust = null;
if (curIndex < customers.size()) {
cust = customers.get(curIndex);
curIndex++;
}
return cust;
}
}

Customer.java

@Data
@NoArgsConstructor
public class Customer {
private Long id;
private String city;
private String state;
private String zipCode;
private String address;
private String lastName;
private String firstName;
private String middleInitial;

@Override
public String toString() {
return "Customer{" +
"id=" + id + ", firstName='" + firstName + '\'' + ", middleInitial='" + middleInitial + '\'' +
", lastName='" + lastName + '\'' + ", city='" + city + '\'' +
", state='" + state + '\'' + ", zipCode='" + zipCode + '\'' + '}';
}
}

ExistingServiceApplication.java

@EnableBatchProcessing
@SpringBootApplication
public class ExistingServiceApplication {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Bean
public ItemReaderAdapter<Customer> customerItemReader(CustomerService customerService) {
ItemReaderAdapter<Customer> adapter = new ItemReaderAdapter<>();
adapter.setTargetObject(customerService);
adapter.setTargetMethod("getCustomer");
return adapter;

}

@Bean
public ItemWriter<Customer> itemWriter() {
return (items) -> items.parallelStream().forEach(out::println);
}

@Bean
public Step copyFileStep() {
return this.stepBuilderFactory.get("copyFileStep")
.<Customer, Customer>chunk(10)
.reader(this.customerItemReader(null))
.writer(this.itemWriter())
.build();
}


@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(this.copyFileStep())
.build();
}

public static void main(String[] args) {
SpringApplication.run(ExistingServiceApplication.class, args);
}
}

--

--

--

Java Developer and enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Setting up Google Colab for DeepLearning experiments

Download In %$PDF Linux Socket Programming by Example Read ^book !ePub

Keeping Secret Keys Out of Version Control in Flutter

Multiple Regression in Pure SQL

How to Use Liberating Structures for a Retrospective

Setting up the fast.ai deep learning course on OSX with Anaconda

Adding commits with git

How to delete words from column name using Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
PA

PA

Java Developer and enthusiast

More from Medium

Spring WebFlux — Rest API with Annotated Controllers

Spring Data JDBC: Implementing Domain Driven Design Aggregate

SpringBoot Kafka Tutorial With Code

Spring Security