Differences
This shows you the differences between two versions of the page.
spring_batch [2015/03/17 09:38] |
spring_batch [2021/04/05 11:23] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Spring Batch ====== | ||
+ | ===== Execute with Test Resources ===== | ||
+ | To execute a Spring Batch programm with test resource using Maven the '' | ||
+ | |||
+ | mvn exec:java -Dexec.classpathScope=" | ||
+ | |||
+ | ===== Output ===== | ||
+ | |||
+ | ==== No Output ==== | ||
+ | Sometimes there is nothing to be written as there is no output. A NoopItemWriter can be created like this: | ||
+ | |||
+ | <sxh java> | ||
+ | public class NoopItemWriter< | ||
+ | | ||
+ | @Override | ||
+ | public void write(List<? | ||
+ | // nothing to do here | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | The item writer is added to the launch context file. | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ===== Input ===== | ||
+ | |||
+ | ==== Declare Comment Lines ==== | ||
+ | Depending on the system and file comments may start with very different characters. These can be specified in the item writer (if it is supported by the implementation) like this: | ||
+ | |||
+ | <sxh xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== Skipping Empty Lines ==== | ||
+ | The default reader implementation does not skip empty lines. There is a reader implementation from Robert Kasanicky where empty lines are treated as comments. | ||
+ | |||
+ | The implementation can be found {{:: | ||
+ | |||
+ | ==== Skipping First Lines ==== | ||
+ | CSV files often start with the column names. Generally these must be skipped when processing the file. If you are using the [[http:// | ||
+ | |||
+ | < | ||
+ | |||
+ | |||
+ | ==== CSV File Input ==== | ||
+ | Reading a delimited file with Spring Batch is a combination of using a file item reader, line mapper and line tokenizer. | ||
+ | |||
+ | <sxh xml;title: Example> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <!-- field delimiter of your csv file --> | ||
+ | < | ||
+ | <!-- property names of your bean , the order of the names is important --> | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | <!-- the fields are mapped to properties of your bean --> | ||
+ | < | ||
+ | <!-- the model your data is mapped to --> | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== Tab Delimited Values ==== | ||
+ | To declare the tab character as a delimiter " | ||
+ | |||
+ | <sxh xml> | ||
+ | < | ||
+ | < | ||
+ | <!-- field delimiter of your csv file --> | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Don't forget to add the namespance to the head of the xml file. | ||
+ | |||
+ | <sxh xml> | ||
+ | < | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | xsi: | ||
+ | http:// | ||
+ | http:// | ||
+ | http:// | ||
+ | http:// | ||
+ | http:// | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Create Jar with Dependencies ===== | ||
+ | To embed all dependencies and create an executable jar the pom.xml must be extended by the maven-assenbly-plugin. | ||
+ | |||
+ | <sxh xml> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | org.springframework.batch.core.launch.support.CommandLineJobRunner | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Create the jar with executing the following line on the command line | ||
+ | |||
+ | mvn clean compile assembly: | ||
+ | | ||
+ | Execute the program with | ||
+ | |||
+ | java -jar program-jar-with-dependencies.jar launch-context.xml jobid | ||
+ | |||
+ | {{tag> |