Skip to content

Commit 09bc739

Browse files
updating generic Exception and linewrappings.
1 parent 57b27f6 commit 09bc739

7 files changed

Lines changed: 27 additions & 23 deletions

File tree

‎spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/App.java‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.baeldung.spring_batch_intro;
22

3-
import javax.swing.Spring;
4-
53
import org.springframework.batch.core.Job;
64
import org.springframework.batch.core.JobExecution;
75
import org.springframework.batch.core.JobParameters;
@@ -10,18 +8,18 @@
108
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
119
import org.springframework.context.support.ClassPathXmlApplicationContext;
1210

11+
1312
public class App {
1413
public static void main(String[] args) {
1514
// Spring Java config
16-
AnnotationConfigApplicationContext context = new
17-
AnnotationConfigApplicationContext();
18-
context.register(SpringConfig.class);
19-
context.register(SpringBatchConfig.class);
20-
context.refresh();
15+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
16+
context.register(SpringConfig.class);
17+
context.register(SpringBatchConfig.class);
18+
context.refresh();
2119

2220
// Spring xml config
23-
// ApplicationContext context = new ClassPathXmlApplicationContext(
24-
// "spring-batch-intro.xml");
21+
// ApplicationContext context = new ClassPathXmlApplicationContext(
22+
// "spring-batch-intro.xml");
2523

2624
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
2725
Job job = (Job) context.getBean("firstBatchJob");

‎spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class SpringBatchConfig {
4141

4242
@Bean
4343
public ItemReader<Transaction> itemReader()
44-
throws UnexpectedInputException, ParseException, Exception {
44+
throws UnexpectedInputException, ParseException {
4545
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
4646
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
4747
String[] tokens = { "username", "userid", "transactiondate", "amount" };
@@ -61,7 +61,7 @@ public ItemProcessor<Transaction, Transaction> itemProcessor() {
6161

6262
@Bean
6363
public ItemWriter<Transaction> itemWriter(Marshaller marshaller)
64-
throws MalformedURLException {
64+
throws MalformedURLException {
6565
StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<Transaction>();
6666
itemWriter.setMarshaller(marshaller);
6767
itemWriter.setRootTagName("transactionRecord");
@@ -78,10 +78,10 @@ public Marshaller marshaller() {
7878

7979
@Bean
8080
protected Step step1(ItemReader<Transaction> reader,
81-
ItemProcessor<Transaction, Transaction> processor,
82-
ItemWriter<Transaction> writer) {
81+
ItemProcessor<Transaction, Transaction> processor,
82+
ItemWriter<Transaction> writer) {
8383
return steps.get("step1").<Transaction, Transaction> chunk(10)
84-
.reader(reader).processor(processor).writer(writer).build();
84+
.reader(reader).processor(processor).writer(writer).build();
8585
}
8686

8787
@Bean(name = "firstBatchJob")

‎spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public DataSource dataSource() {
3939

4040
@Bean
4141
public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
42-
throws MalformedURLException {
42+
throws MalformedURLException {
4343
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
4444

4545
databasePopulator.addScript(dropReopsitoryTables);
@@ -57,6 +57,8 @@ private JobRepository getJobRepository() throws Exception {
5757
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
5858
factory.setDataSource(dataSource());
5959
factory.setTransactionManager(getTransactionManager());
60+
// JobRepositoryFactoryBean's methods Throws Generic Exception,
61+
// it would have been better to have a specific one
6062
factory.afterPropertiesSet();
6163
return (JobRepository) factory.getObject();
6264
}
@@ -67,6 +69,8 @@ private PlatformTransactionManager getTransactionManager() {
6769

6870
public JobLauncher getJobLauncher() throws Exception {
6971
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
72+
// SimpleJobLauncher's methods Throws Generic Exception,
73+
// it would have been better to have a specific one
7074
jobLauncher.setJobRepository(getJobRepository());
7175
jobLauncher.afterPropertiesSet();
7276
return jobLauncher;

‎spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class Transaction {
1212
private Date transactionDate;
1313
private double amount;
1414

15+
/* getters and setters for the attributes */
16+
1517
public String getUsername() {
1618
return username;
1719
}
@@ -47,8 +49,8 @@ public void setAmount(double amount) {
4749
@Override
4850
public String toString() {
4951
return "Transaction [username=" + username + ", userId=" + userId
50-
+ ", transactionDate=" + transactionDate + ", amount=" + amount
51-
+ "]";
52+
+ ", transactionDate=" + transactionDate + ", amount=" + amount
53+
+ "]";
5254
}
5355

5456
}

‎spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import org.springframework.batch.item.ItemProcessor;
55

66
public class CustomItemProcessor implements
7-
ItemProcessor<Transaction, Transaction> {
7+
ItemProcessor<Transaction, Transaction> {
88

9-
public Transaction process(Transaction item) throws Exception {
9+
public Transaction process(Transaction item) {
1010
System.out.println("Processing..." + item);
1111
return item;
1212
}

‎spring-batch-intro/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
1414

1515
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
1616
Transaction transaction = new Transaction();
17-
17+
// you can either use the indices or custom names
18+
// I personally prefer the custom names easy for debugging and
19+
// validating the pipelines
1820
transaction.setUsername(fieldSet.readString("username"));
19-
transaction.setUserId(fieldSet.readInt(1));
21+
transaction.setUserId(fieldSet.readInt("userid"));
2022
transaction.setAmount(fieldSet.readDouble(3));
2123
// Converting the date
2224
String dateString = fieldSet.readString(2);

‎spring-batch-intro/src/main/resources/spring-batch-intro.xml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
<import resource="spring.xml" />
1010

11-
<bean id="record" class="org.baeldung.spring_batch_intro.model.Transaction"></bean>
12-
1311
<bean id="itemReader"
1412
class="org.springframework.batch.item.file.FlatFileItemReader">
1513

0 commit comments

Comments
 (0)