forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add java configuration for the multi-line job sample
Issue spring-projects#3663
- Loading branch information
1 parent
092b238
commit c606354
Showing
10 changed files
with
404 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
.../main/java/org/springframework/batch/sample/file/multiline/MultiLineJobConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.springframework.batch.sample.file.multiline; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.file.FlatFileItemReader; | ||
import org.springframework.batch.item.file.FlatFileItemWriter; | ||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; | ||
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; | ||
import org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper; | ||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; | ||
import org.springframework.batch.item.file.transform.FieldSet; | ||
import org.springframework.batch.item.file.transform.PassThroughLineAggregator; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.WritableResource; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.jdbc.support.JdbcTransactionManager; | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
public class MultiLineJobConfiguration { | ||
|
||
@Bean | ||
@StepScope | ||
public MultiLineTradeItemReader itemReader(@Value("#{jobParameters[inputFile]}") Resource resource) { | ||
FlatFileItemReader<FieldSet> delegate = new FlatFileItemReaderBuilder<FieldSet>().name("delegateItemReader") | ||
.resource(resource) | ||
.lineTokenizer(new DelimitedLineTokenizer()) | ||
.fieldSetMapper(new PassThroughFieldSetMapper()) | ||
.build(); | ||
MultiLineTradeItemReader reader = new MultiLineTradeItemReader(); | ||
reader.setDelegate(delegate); | ||
return reader; | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public MultiLineTradeItemWriter itemWriter(@Value("#{jobParameters[outputFile]}") WritableResource resource) { | ||
FlatFileItemWriter<String> delegate = new FlatFileItemWriterBuilder<String>().name("delegateItemWriter") | ||
.resource(resource) | ||
.lineAggregator(new PassThroughLineAggregator<>()) | ||
.build(); | ||
MultiLineTradeItemWriter writer = new MultiLineTradeItemWriter(); | ||
writer.setDelegate(delegate); | ||
return writer; | ||
} | ||
|
||
@Bean | ||
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager, | ||
MultiLineTradeItemReader itemReader, MultiLineTradeItemWriter itemWriter) { | ||
return new JobBuilder("ioSampleJob", jobRepository) | ||
.start(new StepBuilder("step1", jobRepository).<Trade, Trade>chunk(2, transactionManager) | ||
.reader(itemReader) | ||
.writer(itemWriter) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
// Infrastructure beans | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) | ||
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") | ||
.addScript("/org/springframework/batch/core/schema-hsqldb.sql") | ||
.generateUniqueName(true) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JdbcTransactionManager transactionManager(DataSource dataSource) { | ||
return new JdbcTransactionManager(dataSource); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...samples/src/main/java/org/springframework/batch/sample/file/multiline/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
### MultiLine Input Job | ||
|
||
## About | ||
|
||
The goal of this sample is to show how to process input files where a single logical | ||
item spans multiple physical line: | ||
|
||
``` | ||
BEGIN | ||
INFO,UK21341EAH45,customer1 | ||
AMNT,978,98.34 | ||
END | ||
BEGIN | ||
INFO,UK21341EAH46,customer2 | ||
AMNT,112,18.12 | ||
END | ||
... | ||
``` | ||
|
||
## Run the sample | ||
|
||
You can run the sample from the command line as following: | ||
|
||
``` | ||
$>cd spring-batch-samples | ||
# Launch the sample using the XML configuration | ||
$>../mvnw -Dtest=MultiLineFunctionalTests#testLaunchJobWithXmlConfig test | ||
# Launch the sample using the Java configuration | ||
$>../mvnw -Dtest=MultiLineFunctionalTests#testLaunchJobWithJavaConfig test | ||
``` | ||
|
169 changes: 169 additions & 0 deletions
169
...ng-batch-samples/src/main/java/org/springframework/batch/sample/file/multiline/Trade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright 2006-2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.batch.sample.file.multiline; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
|
||
/** | ||
* @author Rob Harrop | ||
* @author Dave Syer | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class Trade implements Serializable { | ||
|
||
private String isin = ""; | ||
|
||
private long quantity = 0; | ||
|
||
private BigDecimal price = BigDecimal.ZERO; | ||
|
||
private String customer = ""; | ||
|
||
private Long id; | ||
|
||
private long version = 0; | ||
|
||
public Trade() { | ||
} | ||
|
||
public Trade(String isin, long quantity, BigDecimal price, String customer) { | ||
this.isin = isin; | ||
this.quantity = quantity; | ||
this.price = price; | ||
this.customer = customer; | ||
} | ||
|
||
/** | ||
* @param id id of the trade | ||
*/ | ||
public Trade(long id) { | ||
this.id = id; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public long getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(long version) { | ||
this.version = version; | ||
} | ||
|
||
public void setCustomer(String customer) { | ||
this.customer = customer; | ||
} | ||
|
||
public void setIsin(String isin) { | ||
this.isin = isin; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public void setQuantity(long quantity) { | ||
this.quantity = quantity; | ||
} | ||
|
||
public String getIsin() { | ||
return isin; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public long getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public String getCustomer() { | ||
return customer; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Trade: [isin=" + this.isin + ",quantity=" + this.quantity + ",price=" + this.price + ",customer=" | ||
+ this.customer + "]"; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((customer == null) ? 0 : customer.hashCode()); | ||
result = prime * result + ((isin == null) ? 0 : isin.hashCode()); | ||
result = prime * result + ((price == null) ? 0 : price.hashCode()); | ||
result = prime * result + (int) (quantity ^ (quantity >>> 32)); | ||
result = prime * result + (int) (version ^ (version >>> 32)); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
Trade other = (Trade) obj; | ||
if (customer == null) { | ||
if (other.customer != null) { | ||
return false; | ||
} | ||
} | ||
else if (!customer.equals(other.customer)) { | ||
return false; | ||
} | ||
if (isin == null) { | ||
if (other.isin != null) { | ||
return false; | ||
} | ||
} | ||
else if (!isin.equals(other.isin)) { | ||
return false; | ||
} | ||
if (price == null) { | ||
if (other.price != null) { | ||
return false; | ||
} | ||
} | ||
else if (!price.equals(other.price)) { | ||
return false; | ||
} | ||
if (quantity != other.quantity) { | ||
return false; | ||
} | ||
if (version != other.version) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.