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; package org.baeldung.spring_batch_intro;
import javax.swing.Spring;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters; 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.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App { public class App {
public static void main(String[] args) { public static void main(String[] args) {
// Spring Java config // Spring Java config
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
AnnotationConfigApplicationContext();
context.register(SpringConfig.class); context.register(SpringConfig.class);
context.register(SpringBatchConfig.class); context.register(SpringBatchConfig.class);
context.refresh(); context.refresh();

View File

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

View File

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

View File

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

View File

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

View File

@ -14,9 +14,11 @@ public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Transaction transaction = new Transaction(); 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.setUsername(fieldSet.readString("username"));
transaction.setUserId(fieldSet.readInt(1)); transaction.setUserId(fieldSet.readInt("userid"));
transaction.setAmount(fieldSet.readDouble(3)); transaction.setAmount(fieldSet.readDouble(3));
// Converting the date // Converting the date
String dateString = fieldSet.readString(2); String dateString = fieldSet.readString(2);

View File

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