Skip to main content

Introduction to Activities

An Activity is a normal function that executes a single, well-defined action (either short or long running), which handles operations that can fail. Here are some examples:

  • Sending e-mails
  • API calls
  • Database writes
  • Network requests

An Activity involves code that is prone to failure because if it fails (let’s say the API is down), Temporal automatically retries it over and over until it succeeds or until your customized retry or timeout configuration is hit.

Here are two Activities: one for withdrawing money and one for depositing money.

import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityMethod;

@ActivityInterface
public interface ReimbursementActivities {
@ActivityMethod
boolean withdrawMoney(double amount);

@ActivityMethod
boolean depositMoney(double amount);
}
public class ReimbursementActivitiesImpl implements ReimbursementActivities {
@Override
public boolean withdrawMoney(double amount) {
// throw new RuntimeException("Bank service temporarily unavailable");
System.out.println("Successfully withdrawn $" + amount);
return true;
}

@Override
public boolean depositMoney(double amount) {
System.out.println("Successfully desposited $" + amount);
return true;
}
}
3 / 9