Skip to content

Commit 474b3f4

Browse files
committed
Restructure samples by feature
1 parent b1ac90d commit 474b3f4

File tree

187 files changed

+1300
-755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+1300
-755
lines changed

spring-batch-samples/README.md

Lines changed: 173 additions & 257 deletions
Large diffs are not rendered by default.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Reader Writer adapter Sample
2+
3+
### About
4+
5+
This sample shows the delegate pattern again, and also the
6+
`ItemReaderAdapter` which is used to adapt a POJO to the
7+
`ItemReader` interface.
8+
9+
## Run the sample
10+
11+
You can run the sample from the command line as following:
12+
13+
```
14+
$>cd spring-batch-samples
15+
$>../mvnw -Dtest=DelegatingJobFunctionalTests#testLaunchJob test
16+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Tasklet Adapter Sample
2+
3+
### About
4+
5+
This sample shows the delegate pattern again, to adapt an
6+
existing service to a `Tasklet`.
7+
8+
## Run the sample
9+
10+
You can run the sample from the command line as following:
11+
12+
```
13+
$>cd spring-batch-samples
14+
$>../mvnw -Dtest=TaskletAdapterJobFunctionalTests#testLaunchJob test
15+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2006-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.sample.adapter.tasklet;
17+
18+
import org.springframework.batch.core.scope.context.ChunkContext;
19+
20+
public class Task {
21+
22+
public boolean doWork(ChunkContext chunkContext) {
23+
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("done", "yes");
24+
return true;
25+
}
26+
27+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.rabbitmq;
17+
package org.springframework.batch.sample.amqp;
1818

1919
import javax.sql.DataSource;
2020

@@ -29,7 +29,6 @@
2929
import org.springframework.batch.item.ItemWriter;
3030
import org.springframework.batch.item.amqp.builder.AmqpItemReaderBuilder;
3131
import org.springframework.batch.item.amqp.builder.AmqpItemWriterBuilder;
32-
import org.springframework.batch.sample.rabbitmq.processor.MessageProcessor;
3332
import org.springframework.context.annotation.Bean;
3433
import org.springframework.context.annotation.Configuration;
3534
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.sample.rabbitmq.amqp;
16+
package org.springframework.batch.sample.amqp;
1717

1818
import org.springframework.amqp.core.AmqpTemplate;
1919
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@@ -34,8 +34,8 @@ private AmqpMessageProducer() {
3434

3535
private static final int SEND_MESSAGE_COUNT = 10;
3636

37-
private static final String[] BEAN_CONFIG = { "classpath:/META-INF/spring/jobs/messaging/rabbitmq-beans.xml",
38-
"classpath:/META-INF/spring/config-beans.xml" };
37+
private static final String[] BEAN_CONFIG = {
38+
"classpath:org/springframework/batch/sample/amqp/job/rabbitmq-beans.xml" };
3939

4040
public static void main(String[] args) {
4141
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(BEAN_CONFIG);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.sample.rabbitmq.processor;
16+
package org.springframework.batch.sample.amqp;
1717

1818
import org.springframework.batch.item.ItemProcessor;
1919
import org.springframework.lang.Nullable;

spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/README.md renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/amqp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ You can run the sample from the command line as following:
1919
```
2020
cd spring-batch-samples
2121
# Launch the test using the XML configuration
22-
../mvnw -Dtest=AMQPJobFunctionalTests#testLaunchJobWithXmlConfig test
22+
../mvnw -Dtest=AmqpJobFunctionalTests#testLaunchJobWithXmlConfig test
2323
# Launch the test using the Java configuration
24-
../mvnw -Dtest=AMQPJobFunctionalTests#testLaunchJobWithJavaConfig test
24+
../mvnw -Dtest=AmqpJobFunctionalTests#testLaunchJobWithJavaConfig test
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## BeanWrapperMapper Sample
2+
3+
### About
4+
5+
This sample shows the use of automatic mapping from fields in a file
6+
to a domain object. The `Trade` and `Person` objects needed
7+
by the job are created from the Spring configuration using prototype
8+
beans, and then their properties are set using the
9+
`BeanWrapperFieldSetMapper`, which sets properties of the
10+
prototype according to the field names in the file.
11+
12+
Nested property paths are resolved in the same way as normal Spring
13+
binding occurs, but with a little extra leeway in terms of spelling
14+
and capitalisation. Thus for instance, the `Trade` object has a
15+
property called `customer` (lower case), but the file has been
16+
configured to have a column name `CUSTOMER` (upper case), and
17+
the mapper will accept the values happily. Underscores instead of
18+
camel-casing (e.g. `CREDIT_CARD` instead of `creditCard`)
19+
also work.
20+
21+
### Run the sample
22+
23+
You can run the sample from the command line as following:
24+
25+
```
26+
$>cd spring-batch-samples
27+
$>../mvnw -Dtest=BeanWrapperMapperSampleJobFunctionalTests#testJobLaunch test
28+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.remotechunking;
17+
package org.springframework.batch.sample.chunking;
1818

1919
import javax.sql.DataSource;
2020

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.sample.remotechunking;
16+
package org.springframework.batch.sample.chunking;
1717

1818
import java.util.Arrays;
1919

@@ -51,7 +51,7 @@
5151
@EnableBatchProcessing
5252
@EnableBatchIntegration
5353
@EnableIntegration
54-
@PropertySource("classpath:remote-chunking.properties")
54+
@PropertySource("classpath:org/springframework/batch/sample/chunking/remote-chunking.properties")
5555
@Import(DataSourceConfiguration.class)
5656
public class ManagerConfiguration {
5757

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.sample.remotechunking;
16+
package org.springframework.batch.sample.chunking;
1717

1818
import jakarta.jms.JMSException;
1919
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
@@ -50,7 +50,7 @@
5050
@EnableBatchProcessing
5151
@EnableBatchIntegration
5252
@EnableIntegration
53-
@PropertySource("classpath:remote-chunking.properties")
53+
@PropertySource("classpath:org/springframework/batch/sample/chunking/remote-chunking.properties")
5454
@Import(DataSourceConfiguration.class)
5555
public class WorkerConfiguration {
5656

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.jmx;
17+
package org.springframework.batch.sample.common;
1818

1919
import org.aspectj.lang.JoinPoint;
2020
import org.springframework.batch.core.StepExecution;
21+
import org.springframework.batch.sample.misc.jmx.SimpleMessageApplicationEvent;
2122
import org.springframework.context.ApplicationEventPublisher;
2223
import org.springframework.context.ApplicationEventPublisherAware;
2324

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Composite ItemWriter Sample
2+
3+
### About
4+
5+
This shows a common use case using a composite pattern, composing
6+
instances of other framework readers or writers. It is also quite
7+
common for business-specific readers or writers to wrap
8+
off-the-shelf components in a similar way.
9+
10+
In this job the composite pattern is used just to make duplicate
11+
copies of the output data. The delegates for the
12+
`CompositeItemWriter` have to be separately registered as
13+
streams in the `Step` where they are used, in order for the step
14+
to be restartable. This is a common feature of all delegate
15+
patterns.
16+
17+
### Run the sample
18+
19+
You can run the sample from the command line as following:
20+
21+
```
22+
$>cd spring-batch-samples
23+
$>../mvnw -Dtest=CompositeItemWriterSampleFunctionalTests#testJobLaunch test
24+
```

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/person/Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22-
import org.springframework.batch.sample.domain.order.Address;
22+
import org.springframework.batch.sample.file.patternmatching.Address;
2323

2424
public class Person {
2525

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/person/PersonService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22-
import org.springframework.batch.sample.domain.order.Address;
22+
import org.springframework.batch.sample.file.patternmatching.Address;
2323

2424
/**
2525
* Custom class that contains logic that would normally be be contained in

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/multiline/AggregateItem.java renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/file/multilineaggregate/AggregateItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.sample.domain.multiline;
16+
package org.springframework.batch.sample.file.multilineaggregate;
1717

1818
/**
1919
* A wrapper type for an item that is used by {@link AggregateItemReader} to identify the
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.batch.sample.domain.multiline;
16+
package org.springframework.batch.sample.file.multilineaggregate;
1717

1818
import org.springframework.batch.item.file.mapping.FieldSetMapper;
1919
import org.springframework.batch.item.file.transform.FieldSet;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.domain.multiline;
17+
package org.springframework.batch.sample.file.multilineaggregate;
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Multiline Aggregate Sample
2+
3+
### About
4+
5+
The goal of this sample is to show some common tricks with multiline
6+
records in file input jobs.
7+
8+
The input file in this case consists of two groups of trades
9+
delimited by special lines in a file (BEGIN and END):
10+
11+
```
12+
BEGIN
13+
UK21341EAH4597898.34customer1
14+
UK21341EAH4611218.12customer2
15+
END
16+
BEGIN
17+
UK21341EAH4724512.78customer2
18+
UK21341EAH4810809.25customer3
19+
UK21341EAH4985423.39customer4
20+
END
21+
```
22+
23+
The goal of the job is to operate on the two groups, so the item
24+
type is naturally `List<Trade`>. To get these items delivered
25+
from an item reader we employ two components from Spring Batch: the
26+
`AggregateItemReader` and the
27+
`PrefixMatchingCompositeLineTokenizer`. The latter is
28+
responsible for recognising the difference between the trade data
29+
and the delimiter records. The former is responsible for
30+
aggregating the trades from each group into a `List` and handing
31+
out the list from its `read()` method. To help these components
32+
perform their responsibilities we also provide some business
33+
knowledge about the data in the form of a `FieldSetMapper`
34+
(`TradeFieldSetMapper`). The `TradeFieldSetMapper` checks
35+
its input for the delimiter fields (BEGIN, END) and if it detects
36+
them, returns the special tokens that `AggregateItemReader`
37+
needs. Otherwise it maps the input into a `Trade` object.
38+
39+
### Run the sample
40+
41+
You can run the sample from the command line as following:
42+
43+
```
44+
$>cd spring-batch-samples
45+
$>../mvnw -Dtest=MultilineAggregateJobFunctionalTests#testJobLaunch test
46+
```

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/order/Address.java renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/file/patternmatching/Address.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.domain.order;
17+
package org.springframework.batch.sample.file.patternmatching;
1818

1919
public class Address {
2020

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/order/BillingInfo.java renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/file/patternmatching/BillingInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.domain.order;
17+
package org.springframework.batch.sample.file.patternmatching;
1818

1919
public class BillingInfo {
2020

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/order/Customer.java renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/file/patternmatching/Customer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.domain.order;
17+
package org.springframework.batch.sample.file.patternmatching;
1818

1919
public class Customer {
2020

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/order/LineItem.java renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/file/patternmatching/LineItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.domain.order;
17+
package org.springframework.batch.sample.file.patternmatching;
1818

1919
import java.math.BigDecimal;
2020

spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/order/Order.java renamed to spring-batch-samples/src/main/java/org/springframework/batch/sample/file/patternmatching/Order.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.sample.domain.order;
17+
package org.springframework.batch.sample.file.patternmatching;
1818

1919
import java.math.BigDecimal;
2020
import java.util.Date;

0 commit comments

Comments
 (0)