updating generic Exception and linewrappings.

This commit is contained in:
Devendra Desale 2015-12-14 14:35:09 +08:00 committed by David Morley
parent cd3f3d7551
commit a6e0acdb01
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,18 +8,18 @@ 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();
context.register(SpringConfig.class);
context.register(SpringBatchConfig.class);
context.refresh();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SpringConfig.class);
context.register(SpringBatchConfig.class);
context.refresh();
// Spring xml config
// ApplicationContext context = new ClassPathXmlApplicationContext(
// "spring-batch-intro.xml");
// ApplicationContext context = new ClassPathXmlApplicationContext(
// "spring-batch-intro.xml");
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("firstBatchJob");

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

View File

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