Skip to content

Commit 02e2672

Browse files
authored
Merge pull request #18807 from psevestre/master
[BAEL-9430] Getting Started with the Temporal Workflow Engine in Java
2 parents ec578d1 + c0f326c commit 02e2672

20 files changed

+661
-0
lines changed

saas-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<module>twilio</module>
2626
<module>twilio-whatsapp</module>
2727
<module>twitter4j</module>
28+
<module>temporal</module>
2829
</modules>
2930

3031
<build>

saas-modules/temporal/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>temporal</artifactId>
7+
<version>1.0</version>
8+
<name>temporal</name>
9+
<description>Temporal Workflow Engine Tutorial</description>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>saas-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.temporal</groupId>
20+
<artifactId>temporal-sdk</artifactId>
21+
<version>${temporal.version}</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>io.temporal</groupId>
26+
<artifactId>temporal-testing</artifactId>
27+
<version>${temporal.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-surefire-plugin</artifactId>
38+
<version>${maven-surefire-plugin.version}</version>
39+
<configuration>
40+
<argLine>
41+
--add-opens java.base/java.lang=ALL-UNNAMED
42+
</argLine>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
<properties>
49+
<temporal.version>1.31.0</temporal.version>
50+
</properties>
51+
52+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.temporal.worker;
2+
3+
import io.temporal.worker.Worker;
4+
5+
/**
6+
* Interface for registering Workflows and Activities to a Temporal Worker.
7+
*/
8+
public interface TemporalWorkerRegistrar {
9+
void register(Worker worker);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.temporal.workflows.hello;
2+
3+
import io.temporal.workflow.WorkflowInterface;
4+
import io.temporal.workflow.WorkflowMethod;
5+
6+
@WorkflowInterface
7+
public interface HelloWorkflow {
8+
@WorkflowMethod
9+
String hello(String person);
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.temporal.workflows.hello;
2+
3+
import io.temporal.client.WorkflowClient;
4+
import io.temporal.serviceclient.WorkflowServiceStubs;
5+
import io.temporal.worker.Worker;
6+
import io.temporal.worker.WorkerFactory;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
public class HelloWorkflowApplication {
11+
12+
private static final String QUEUE_NAME = "say-hello-queue";
13+
private static final Logger log = LoggerFactory.getLogger(HelloWorkflowApplication.class);
14+
15+
public static void main(String[] args) {
16+
17+
log.info("Creating worker...");
18+
var service = WorkflowServiceStubs.newLocalServiceStubs();
19+
var client = WorkflowClient.newInstance(service);
20+
var factory = WorkerFactory.newInstance(client);
21+
var worker = factory.newWorker(QUEUE_NAME);
22+
23+
log.info("Registering workflows and activities...");
24+
HelloWorkflowRegistrar.newInstance().register(worker);
25+
26+
log.info("Starting worker...");
27+
factory.start();
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.temporal.workflows.hello;
2+
3+
import com.baeldung.temporal.workflows.hello.activities.SayHelloActivity;
4+
import io.temporal.activity.ActivityOptions;
5+
import io.temporal.workflow.Workflow;
6+
7+
import java.time.Duration;
8+
9+
public class HelloWorkflowImpl implements HelloWorkflow {
10+
11+
private final SayHelloActivity activity = Workflow.newActivityStub(
12+
SayHelloActivity.class,
13+
ActivityOptions.newBuilder()
14+
.setStartToCloseTimeout(Duration.ofSeconds(10))
15+
.build()
16+
);
17+
18+
@Override
19+
public String hello(String person) {
20+
return activity.sayHello(person);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.temporal.workflows.hello;
2+
3+
import com.baeldung.temporal.worker.TemporalWorkerRegistrar;
4+
import com.baeldung.temporal.workflows.hello.activities.SayHelloActivityImpl;
5+
import io.temporal.worker.Worker;
6+
7+
public class HelloWorkflowRegistrar implements TemporalWorkerRegistrar {
8+
9+
private HelloWorkflowRegistrar() {}
10+
11+
@Override
12+
public void register(Worker worker) {
13+
worker.registerWorkflowImplementationTypes(HelloWorkflowImpl.class);
14+
worker.registerActivitiesImplementations(new SayHelloActivityImpl());
15+
}
16+
17+
public static HelloWorkflowRegistrar newInstance() {
18+
return new HelloWorkflowRegistrar();
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.temporal.workflows.hello.activities;
2+
3+
import io.temporal.activity.ActivityInterface;
4+
import io.temporal.activity.ActivityMethod;
5+
6+
@ActivityInterface
7+
public interface SayHelloActivity {
8+
@ActivityMethod
9+
String sayHello(String person);
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.temporal.workflows.hello.activities;
2+
3+
import io.temporal.activity.Activity;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
public class SayHelloActivityImpl implements SayHelloActivity {
8+
private static final Logger log = LoggerFactory.getLogger(SayHelloActivityImpl.class);
9+
10+
public String sayHello(String person) {
11+
log.info("Saying hello to {}", person);
12+
return "Hello, " + person;
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.temporal.workflows.hellov2;
2+
3+
import com.baeldung.temporal.worker.TemporalWorkerRegistrar;
4+
import com.baeldung.temporal.workflows.hellov2.activities.HelloV2ActivitiesImpl;
5+
import io.temporal.worker.Worker;
6+
7+
public class HelloV2WorkerRegistrar implements TemporalWorkerRegistrar {
8+
9+
10+
private HelloV2WorkerRegistrar() {
11+
}
12+
13+
@Override
14+
public void register(Worker worker) {
15+
worker.registerWorkflowImplementationTypes(HelloWorkflowV2Impl.class);
16+
worker.registerActivitiesImplementations(new HelloV2ActivitiesImpl());
17+
}
18+
19+
public static HelloV2WorkerRegistrar newInstance() {
20+
return new HelloV2WorkerRegistrar();
21+
}
22+
}

0 commit comments

Comments
 (0)