Skip to content

Commit c5ac83e

Browse files
authored
JBatch example for Helidon 3.x (helidon-io#3927)
* JBatch example for Helidon 3.x * Copyright and style fixes
1 parent 5d7e61e commit c5ac83e

19 files changed

Lines changed: 752 additions & 1 deletion

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ user.txt
7373
ObjectStore/
7474
PutObjectStoreDirHere/
7575
package-lock.json
76+
RUNTIMEDB/

examples/jbatch/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Helidon + jBatch
2+
3+
Minimal Helidon MP + jBatch PoC.
4+
5+
## Build and run
6+
7+
With JDK11+
8+
```bash
9+
mvn package
10+
java -jar target/helidon-jbatch-example.jar
11+
```
12+
13+
## Exercise the application
14+
15+
```
16+
curl -X GET http://localhost:8080/batch
17+
```

examples/jbatch/pom.xml

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2022 Oracle and/or its affiliates.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>io.helidon.applications</groupId>
25+
<artifactId>helidon-mp</artifactId>
26+
<version>3.0.0-SNAPSHOT</version>
27+
<relativePath>../../applications/mp/pom.xml</relativePath>
28+
</parent>
29+
<groupId>io.helidon.examples.jbatch</groupId>
30+
<artifactId>helidon-examples-jbatch</artifactId>
31+
<name>jbatch-example</name>
32+
33+
<properties>
34+
<version.lib.jbatch-api>2.1.0-M2</version.lib.jbatch-api>
35+
<version.lib.jbatch.container>2.1.0-M2</version.lib.jbatch.container>
36+
<version.lib.derby>10.13.1.1</version.lib.derby>
37+
<version.lib.jaxb-api>3.0.1</version.lib.jaxb-api>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>io.helidon.microprofile.bundles</groupId>
43+
<artifactId>helidon-microprofile-core</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>jakarta.json</groupId>
47+
<artifactId>jakarta.json-api</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>jakarta.transaction</groupId>
51+
<artifactId>jakarta.transaction-api</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>jakarta.batch</groupId>
55+
<artifactId>jakarta.batch-api</artifactId>
56+
<version>${version.lib.jbatch-api}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.ibm.jbatch</groupId>
60+
<artifactId>com.ibm.jbatch.spi</artifactId>
61+
<version>${version.lib.jbatch.container}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.glassfish.jaxb</groupId>
65+
<artifactId>jaxb-runtime</artifactId>
66+
<version>${version.lib.jaxb-api}</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>jakarta.xml.bind</groupId>
70+
<artifactId>jakarta.xml.bind-api</artifactId>
71+
<version>${version.lib.jaxb-api}</version>
72+
</dependency>
73+
74+
75+
<dependency>
76+
<groupId>jakarta.activation</groupId>
77+
<artifactId>jakarta.activation-api</artifactId>
78+
<scope>runtime</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.jboss</groupId>
82+
<artifactId>jandex</artifactId>
83+
<scope>runtime</scope>
84+
<optional>true</optional>
85+
</dependency>
86+
<dependency>
87+
<groupId>com.ibm.jbatch</groupId>
88+
<artifactId>com.ibm.jbatch.container</artifactId>
89+
<version>${version.lib.jbatch.container}</version>
90+
<scope>runtime</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.apache.derby</groupId>
94+
<artifactId>derby</artifactId>
95+
<version>${version.lib.derby}</version>
96+
<scope>runtime</scope>
97+
</dependency>
98+
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter-api</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
<dependency>
105+
<groupId>io.helidon.microprofile.tests</groupId>
106+
<artifactId>helidon-microprofile-tests-junit5</artifactId>
107+
<scope>test</scope>
108+
</dependency>
109+
</dependencies>
110+
111+
<build>
112+
<plugins>
113+
<plugin>
114+
<groupId>org.apache.maven.plugins</groupId>
115+
<artifactId>maven-dependency-plugin</artifactId>
116+
<executions>
117+
<execution>
118+
<id>copy-libs</id>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
<plugin>
123+
<groupId>org.jboss.jandex</groupId>
124+
<artifactId>jandex-maven-plugin</artifactId>
125+
<executions>
126+
<execution>
127+
<id>make-index</id>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
<plugin>
132+
<groupId>io.helidon.build-tools</groupId>
133+
<artifactId>helidon-maven-plugin</artifactId>
134+
<executions>
135+
<execution>
136+
<id>third-party-license-report</id>
137+
</execution>
138+
</executions>
139+
</plugin>
140+
</plugins>
141+
</build>
142+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates.
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+
* http://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+
*/
17+
package io.helidon.jbatch.example;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Properties;
24+
25+
import com.ibm.jbatch.spi.BatchSPIManager;
26+
import jakarta.batch.operations.JobOperator;
27+
import jakarta.batch.runtime.JobExecution;
28+
import jakarta.batch.runtime.StepExecution;
29+
import jakarta.enterprise.context.ApplicationScoped;
30+
import jakarta.json.Json;
31+
import jakarta.json.JsonBuilderFactory;
32+
import jakarta.json.JsonObject;
33+
import jakarta.ws.rs.GET;
34+
import jakarta.ws.rs.Path;
35+
import jakarta.ws.rs.PathParam;
36+
import jakarta.ws.rs.Produces;
37+
import jakarta.ws.rs.core.MediaType;
38+
39+
40+
import static jakarta.batch.runtime.BatchRuntime.getJobOperator;
41+
42+
43+
/**
44+
* Trigger a batch process using resource.
45+
*/
46+
@Path("/batch")
47+
@ApplicationScoped
48+
public class BatchResource {
49+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
50+
51+
private JobOperator jobOperator;
52+
53+
/**
54+
* Run a JBatch process when endpoint called.
55+
* @return JsonObject with the result.
56+
*/
57+
@GET
58+
@Produces(MediaType.APPLICATION_JSON)
59+
public JsonObject executeBatch() {
60+
61+
BatchSPIManager batchSPIManager = BatchSPIManager.getInstance();
62+
batchSPIManager.registerPlatformMode(BatchSPIManager.PlatformMode.SE);
63+
batchSPIManager.registerExecutorServiceProvider(new HelidonExecutorServiceProvider());
64+
65+
jobOperator = getJobOperator();
66+
Long executionId = jobOperator.start("myJob", new Properties());
67+
68+
return JSON.createObjectBuilder()
69+
.add("Started a job with Execution ID: ", executionId)
70+
.build();
71+
}
72+
73+
/**
74+
* Check the job status.
75+
* @param executionId the job ID.
76+
* @return JsonObject with status.
77+
*/
78+
@GET
79+
@Path("/status/{execution-id}")
80+
public JsonObject status(@PathParam("execution-id") Long executionId){
81+
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
82+
83+
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
84+
List<String> executedSteps = new ArrayList<>();
85+
for (StepExecution stepExecution : stepExecutions) {
86+
executedSteps.add(stepExecution.getStepName());
87+
}
88+
89+
return JSON.createObjectBuilder()
90+
.add("Steps executed", Arrays.toString(executedSteps.toArray()))
91+
.add("Status", jobExecution.getBatchStatus().toString())
92+
.build();
93+
}
94+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates.
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+
* http://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+
*/
17+
package io.helidon.jbatch.example;
18+
19+
import java.util.concurrent.ExecutorService;
20+
21+
import io.helidon.common.configurable.ThreadPoolSupplier;
22+
23+
import com.ibm.jbatch.spi.ExecutorServiceProvider;
24+
25+
26+
/**
27+
* Executor service for batch processing.
28+
*/
29+
public class HelidonExecutorServiceProvider implements ExecutorServiceProvider {
30+
@Override
31+
public ExecutorService getExecutorService() {
32+
return ThreadPoolSupplier.builder().corePoolSize(2).build().get();
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates.
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+
* http://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+
*/
17+
package io.helidon.jbatch.example.jobs;
18+
19+
import jakarta.batch.api.AbstractBatchlet;
20+
21+
/**
22+
* Batchlet example.
23+
*/
24+
public class MyBatchlet extends AbstractBatchlet {
25+
26+
/**
27+
* Run inside a batchlet.
28+
*
29+
* @return String with status.
30+
*/
31+
@Override
32+
public String process() {
33+
System.out.println("Running inside a batchlet");
34+
return "COMPLETED";
35+
}
36+
37+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates.
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+
* http://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+
*/
17+
package io.helidon.jbatch.example.jobs;
18+
19+
/**
20+
* Example of an Input Record.
21+
*/
22+
public class MyInputRecord {
23+
private int id;
24+
25+
/**
26+
* Constructor for Input Record.
27+
* @param id
28+
*/
29+
public MyInputRecord(int id) {
30+
this.id = id;
31+
}
32+
33+
public int getId() {
34+
return id;
35+
}
36+
37+
public void setId(int id) {
38+
this.id = id;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "MyInputRecord: " + id;
44+
}
45+
}

0 commit comments

Comments
 (0)