During this exercise, you will create an Activity interface, an Activity class that implements the interface, register it with the Worker, and modify a Workflow to execute it.
Before continuing, kill any Worker instances still running from any previous exercises (you can do this by pressing Ctrl-C in the terminals where they are running).
As with other exercises, you should make your changes in the practice
directory. Look for "TODO" comments, which will provide hints about what
you'll need to change. If you get stuck and need additional hints, or
if you want to check your work, look at the completed example in the
solution directory.
The GreetingActivities.java file defines an interface with the Activity method signatures.
The GreetingActivitiesImpl.java implements the Activity methods and any other utility methods.
GreetingActivitiesImpl.java contains a function (GreetInSpanish) that uses a
microservice to get a customized greeting in Spanish. This file also contains a utility method
(callService) that the Activity uses to call the microservice.
- Open the
GreetingActivities.javafile (located in thepractice/src/main/java/farewellworkflowsubdirectory) in the editor. - Define a new Activity method signature (using any valid name you like) that will get a custom farewell message from the microservice.
- Save your changes to the file
- Open the
GreetingActivitiesImpl.javafile (located in thepractice/src/main/java/farewellworkflowsubdirectory) in the editor. - Create a new Activity that will get a custom farewell message from the microservice.
- Copy the
greetInSpanishmethod - Rename the new method using the name that matches the new method signature you defined in
GreetingActivities.java. - Change
get-spanish-greetingin this new method toget-spanish-farewell - Save your changes to this file
- Copy the
- Open the
GreetingWorker.javafile (located in thepractice/src/main/java/greetingworkersubdirectory) in the editor - Modify the line below the
//TODO:comment to register your new Activities with the Worker (hint: you'll create a new object of the Activity Implementation). - Save your changes to this file
- Open the
GreetingWorkflowImpl.javafile (located in thepractice/src/main/java/greetingworkersubdirectory) in the editor - Look for the TODO message, uncomment the line below it, and then change that line to execute the Activity function you created
- Save your changes to this file
All commands below must be run from the practice subdirectory. Ensure your temporal cluster is running
before executing the following commands:
- Start the microservice by running
mvn exec:java -Dexec.mainClass="farewellworkflow.Microservice"in a terminal - In another terminal, start your Worker by running
mvn exec:java -Dexec.mainClass="farewellworkflow.GreetingWorker" - In a third terminal, execute your Workflow by running
mvn exec:java -Dexec.mainClass="farewellworkflow.Starter" -Dexec.args="Mason"(replacingMasonwith your own name)
If there is time remaining, experiment with Activity failures and retries by stopping the microservice (press Ctrl-C in its terminal) and re-running the Workflow. Look at the Web UI to see the status of the Workflow and its Activities. After a few seconds, restart the microservice by running the same command used to start it earlier. You should find that the Workflow will now complete successfully following the next Activity retry.