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,18 +8,18 @@ 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();
// Spring xml config // Spring xml config
// ApplicationContext context = new ClassPathXmlApplicationContext( // ApplicationContext context = new ClassPathXmlApplicationContext(
// "spring-batch-intro.xml"); // "spring-batch-intro.xml");
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("firstBatchJob"); Job job = (Job) context.getBean("firstBatchJob");

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" };
@ -61,7 +61,7 @@ public class SpringBatchConfig {
@Bean @Bean
public ItemWriter<Transaction> itemWriter(Marshaller marshaller) public ItemWriter<Transaction> itemWriter(Marshaller marshaller)
throws MalformedURLException { throws MalformedURLException {
StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<Transaction>(); StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<Transaction>();
itemWriter.setMarshaller(marshaller); itemWriter.setMarshaller(marshaller);
itemWriter.setRootTagName("transactionRecord"); itemWriter.setRootTagName("transactionRecord");
@ -78,10 +78,10 @@ public class SpringBatchConfig {
@Bean @Bean
protected Step step1(ItemReader<Transaction> reader, protected Step step1(ItemReader<Transaction> reader,
ItemProcessor<Transaction, Transaction> processor, ItemProcessor<Transaction, Transaction> processor,
ItemWriter<Transaction> writer) { ItemWriter<Transaction> writer) {
return steps.get("step1").<Transaction, Transaction> chunk(10) return steps.get("step1").<Transaction, Transaction> chunk(10)
.reader(reader).processor(processor).writer(writer).build(); .reader(reader).processor(processor).writer(writer).build();
} }
@Bean(name = "firstBatchJob") @Bean(name = "firstBatchJob")

View File

@ -39,7 +39,7 @@ public class SpringConfig {
@Bean @Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
throws MalformedURLException { throws MalformedURLException {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(dropReopsitoryTables); databasePopulator.addScript(dropReopsitoryTables);
@ -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;
} }
@ -47,8 +49,8 @@ public class Transaction {
@Override @Override
public String toString() { public String toString() {
return "Transaction [username=" + username + ", userId=" + userId return "Transaction [username=" + username + ", userId=" + userId
+ ", transactionDate=" + transactionDate + ", amount=" + amount + ", transactionDate=" + transactionDate + ", amount=" + amount
+ "]"; + "]";
} }
} }

View File

@ -4,9 +4,9 @@ import org.baeldung.spring_batch_intro.model.Transaction;
import org.springframework.batch.item.ItemProcessor; 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">