updating generic Exception and linewrappings.

This commit is contained in:
Devendra Desale 2015-12-14 14:35:09 +08:00
parent 57b27f6c9c
commit 09bc7399dd
7 changed files with 27 additions and 23 deletions

View File

@ -1,7 +1,5 @@
package org.baeldung.spring_batch_intro;
import javax.swing.Spring;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
@ -10,11 +8,11 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
// Spring Java config
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SpringConfig.class);
context.register(SpringBatchConfig.class);
context.refresh();

View File

@ -41,7 +41,7 @@ public class SpringBatchConfig {
@Bean
public ItemReader<Transaction> itemReader()
throws UnexpectedInputException, ParseException, Exception {
throws UnexpectedInputException, ParseException {
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
String[] tokens = { "username", "userid", "transactiondate", "amount" };

View File

@ -57,6 +57,8 @@ public class SpringConfig {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource());
factory.setTransactionManager(getTransactionManager());
// JobRepositoryFactoryBean's methods Throws Generic Exception,
// it would have been better to have a specific one
factory.afterPropertiesSet();
return (JobRepository) factory.getObject();
}
@ -67,6 +69,8 @@ public class SpringConfig {
public JobLauncher getJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
// SimpleJobLauncher's methods Throws Generic Exception,
// it would have been better to have a specific one
jobLauncher.setJobRepository(getJobRepository());
jobLauncher.afterPropertiesSet();
return jobLauncher;

View File

@ -12,6 +12,8 @@ public class Transaction {
private Date transactionDate;
private double amount;
/* getters and setters for the attributes */
public String getUsername() {
return username;
}

View File

@ -6,7 +6,7 @@ import org.springframework.batch.item.ItemProcessor;
public class CustomItemProcessor implements
ItemProcessor<Transaction, Transaction> {
public Transaction process(Transaction item) throws Exception {
public Transaction process(Transaction item) {
System.out.println("Processing..." + item);
return item;
}

View File

@ -14,9 +14,11 @@ public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Transaction transaction = new Transaction();
// you can either use the indices or custom names
// I personally prefer the custom names easy for debugging and
// validating the pipelines
transaction.setUsername(fieldSet.readString("username"));
transaction.setUserId(fieldSet.readInt(1));
transaction.setUserId(fieldSet.readInt("userid"));
transaction.setAmount(fieldSet.readDouble(3));
// Converting the date
String dateString = fieldSet.readString(2);

View File

@ -8,8 +8,6 @@
<import resource="spring.xml" />
<bean id="record" class="org.baeldung.spring_batch_intro.model.Transaction"></bean>
<bean id="itemReader"
class="org.springframework.batch.item.file.FlatFileItemReader">