Skip to content

Commit

Permalink
Suggested changes following code review - Tom
Browse files Browse the repository at this point in the history
* upgrade to latest Java SDK

* upgrade to latest SDK

* corrected paths

* REALLy corrected the path this time (my previous correction was incomplete)

* Dependencies should not be capitalized

* Renamed directory for consistency with the name used in the Go and TS versions of Temporal 101

* Rewrote the microservice (it now correctly handles characters beyond the US-ASCII subset and does not use a JDK-specific implementation)

* renamed Java package to match directory name (i.e., to be consistent with the Go and TS versions of 101)

* added Go code for the Workflow Worker and starter

* changed formatting to reflect Java coding conventions

* changed formatting to reflect Java coding conventions

* reformatted code per Java conventions and added @OverRide annotation to Workflow Implementation

* formatted code and corrected a spelling error in one of the comments

* reformatted code as per Java conventions and added an Override annotation where needed

* reformatted code as per Java conventions

* rewrote microservice to match that used in exercises (which can handle non-US-ASCII characters)

* updated all pom files to be more descriptive; also reformatted them
  • Loading branch information
tomwheeler committed Apr 27, 2023
1 parent fdab19c commit 609f532
Show file tree
Hide file tree
Showing 46 changed files with 2,567 additions and 1,002 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Directory Name | Exercise
:---------------------------- | :----------------------------
`exercises/hello-workflow` | [Exercise 1](exercises/hello-workflow/README.md)
`exercises/hello-web-ui` | [Exercise 2](exercises/hello-web-ui/README.md)
`exercises/greeting-workflow` | [Exercise 3](exercises/greeting-workflow/README.md)
`exercises/farewell-workflow` | [Exercise 3](exercises/farewell-workflow/README.md)
`exercises/finale-workflow` | [Exercise 4](exercises/finale-workflow/README.md)


Expand Down
193 changes: 98 additions & 95 deletions demos/service-workflow/pom.xml
Original file line number Diff line number Diff line change
@@ -1,110 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>farewellworkflow</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>io.temporal.learn</groupId>
<artifactId>greeting-workflow-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<name>greeting-workflow (demo)</name>
<url>https://learn.temporal.io/</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependencies>
<dependency>
<groupId>io.temporal</groupId>
<artifactId>temporal-sdk</artifactId>
<version>1.19.1</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.6</version>
</dependency>

<dependency>
<groupId>io.temporal</groupId>
<artifactId>temporal-sdk</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>io.temporal</groupId>
<artifactId>temporal-testing</artifactId>
<version>1.19.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>

<dependency>
<groupId>io.temporal</groupId>
<artifactId>temporal-testing</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-quick</artifactId>
<version>5.5.5</version>
</dependency>

</dependencies>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,81 +1,75 @@
package greetingworkflow;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import org.rapidoid.http.Req;
import org.rapidoid.http.ReqRespHandler;
import org.rapidoid.http.Resp;
import org.rapidoid.setup.On;
import org.rapidoid.u.U;

import java.util.Map;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Microservice {

public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(9999), 0);
server.createContext("/get-spanish-greeting", new spanishGreetingHandler());
server.createContext("/get-spanish-farewell", new spanishFarewellHandler());
server.setExecutor(null);
server.start();
System.out.println("Server is listening on port 9999");
}
// port number where this service will listen for incoming HTTP requests
public static final int PORT_NUMBER = 9999;

static class spanishGreetingHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String query = exchange.getRequestURI().getQuery();
String name = "";
String response = "";
int code = 200;
if (query != null) {
Map<String, String> queryMap = queryToMap(query);
name = queryMap.get("name");
}
if (name.equals("")) {
response = "Missing required 'name' parameter.";
code = 400;
} else {
response = String.format("Hola, %s!", name);
}
exchange.sendResponseHeaders(code, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
// IP address to which the service will be bound. Using a value of 0.0.0.0
// will make it available on all available interfaces, but you could use
// 127.0.0.1 to restrict it to the loopback interface
public static final String SERVER_IP = "0.0.0.0";

public static void main(String[] args) throws IOException {
// Start the service on the specified IP address and port
On.address(SERVER_IP).port(PORT_NUMBER);

// Define the service endpoints and handlers
On.get("/get-spanish-greeting").plain(new GreetingHandler());
On.get("/get-spanish-farewell").plain(new FarewellHandler());

// Also define a catch-all to return an HTTP 404 Not Found error if the URL
// path in the request didn't match an endpoint defined above. It's essential
// that this code remains at the end.
On.req((req, resp) -> {
String message = String.format("Error: Invalid endpoint address '%s'", req.path());
return req.response().result(message).code(404);
});
}

static class spanishFarewellHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String query = exchange.getRequestURI().getQuery();
String name = "";
String response = "";
int code = 200;
if (query != null) {
Map<String, String> queryMap = queryToMap(query);
name = queryMap.get("name");
}
if (name.equals("")) {
response = "Missing required 'name' parameter.";
code = 400;
} else {
response = String.format("Adios, %s!", name);
private static class GreetingHandler implements ReqRespHandler {

@Override
public Object execute(Req req, Resp resp) throws Exception {
Map<String, String> params = req.params();

if (!params.containsKey("name")) {
String message = "Error: Missing required 'name' parameter!";
return req.response().result(message).code(500);
}
exchange.sendResponseHeaders(code, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();

String name = params.get("name");
String response = String.format("¡Hola, %s!", name);
return U.str(response);
}

}

private static Map<String, String> queryToMap(String query) {
Map<String, String> result = new java.util.HashMap<>();
for (String param : query.split("&")) {
String[] pair = param.split("=");
if (pair.length > 1) {
result.put(pair[0], pair[1]);
} else {
result.put(pair[0], "");
private static class FarewellHandler implements ReqRespHandler {

@Override
public Object execute(Req req, Resp resp) throws Exception {
Map<String, String> params = req.params();

if (!params.containsKey("name")) {
String message = "Error: Missing required 'name' parameter!";
return req.response().result(message).code(500);
}

String name = params.get("name");
String response = String.format("¡Adiós, %s!", name);
return U.str(response);
}
return result;
}

}
}

0 comments on commit 609f532

Please sign in to comment.