Types of Actions

1. SyncRequest

This represents a request sent by a client using the client API.

The Action Handler can return a Value as part of the response which the client can use.

Example

Client.java
Table table = client.getTable("my_app", "my_table");
Grain grain = table.getGrain(Key.of("u123"));

// The following will invoke the "handleUserEvents" action on the Grain.
grain.invoke("getUserAccountBalance", Value.of(userAccountId));
ActionHandler.java
public ActionResult getUserAccountBalance(Action action, GrainContext context) {
    String accountID = ((SyncRequest)action).getPayload().asString();

    // Get account balance...
    long accountBalance = ...
    
    return ActionResult.success(action, Value.of(accountBalance));
}

2. GrainRequest

This represents a message sent by a Grain to another Grain.

This message can be an operation to either insert something into another Grain's state, or invoke an Action on it.

Here are all the supported operations (Java, Python):

  1. Put - insert a payload into another Grain's value.

  2. MapPut - insert a payload into another Grain's map.

  3. MapDelete - delete an entry from another Grain's map.

  4. MapDeleteRange - delete a range of entries from another Grain's map.

  5. Invoke - invoke an Action on another Grain.

Optionally, a sender Grain can also choose to receive a callback once the operation has been completed by providing a ExpectGrainResponse object to the sendToGrain method (Java, Python). The ExpectGrainResponse object contains the following:

  1. responseAction - the callback action to invoke on the sender Grain with the response.

    1. This callback action will receive a GrainResponse Action from the receiver Grain.

  2. senderCookie - a payload to identify the callback.

  3. onErrorOnly - indicating if the callback action should be invoked only if the resulting operation resulted in a failure.

Example

SendMessage.java
public ActionResult sendMessageToUser(Action action, GrainContext context) {
    // The following is being used to send a message to another Grain.
    // The receving Grain's "receiveMessage" action handler will be invoked along with the userMessageJson.
    GrainOp.Invoke sendMessage = new GrainOp.Invoke("receiveMessage", Value.of(userMessageJson));

    context.sendToGrain("user_table", Key.of(userId), sendMessage, null);

    return ActionResult.success(action);
}
ReceiveMessage.java
public ActionResult receiveMessage(Action action, GrainContext context) {

    // The following message will contain the payload (`userMessageJson`) sent from the other grain.
    Value message = ((GrainRequest)action).getPayload();
    ...
}

3. GrainResponse

This represents a reply that a Grain receives after sending a message (GrainRequest) and expecting a response.

Example

Handler.java
public ActionResult sendMessageToAnotherGrain(Action action, GrainContext context) {
    // The following is being used to send a message to another Grain and expects a callback once the message has been received and processed.
    context.sendToGrain("user_table", Key.of(userId), sendMessageOp, new ExpectGrainResponse("handleMessageCallback", Value.of(messageId), false);
    ...
}

public ActionResult handleMessageCallback(Action action, GrainContext context) {
    // The following method will be invoked once the userId Grain has processed the message sent from `sendMessageToAnotherGrain`.
    GrainResponse response = GrainResponse(action);
    
    String cookie = response.getCookieValue().asString();
    Status status = response.getStatus(); // Status.OK if successful
    Value result = response.getResult(); // Result sent back from receiving Grain
}

4. TopicEvent

This represents an event delivered to this grain as part of a Topic Subscription.

Example

Client.java
Topic topic = client.getTopic("my_app", "my_topic");

// The following will append the provided Event into `my_topic`. 
topic.append(new Event(Key.of(eventId), Value.of(payload)));
EventHandler.java
public ActionResult handleTopicEvent(Action action, GrainContext context) {
    // This will contain the payload sent by the client.
    Value payload = ((TopicEvent)action).getPayload();
}

5. AppendResponse

This represents the response received by a Grain after appending an event to a Topic.

Example

Handler.java
public ActionResult appendEvent(Action action, GrainContext context) {
    
    // The following will append the provided event into a topic.
    context.sendToTopic("user_table", Key.of(userId), Value.of(eventPayload),
     new ExpectAppendResponse("handleAppendFailure", Value.of(messageId));
}

public ActionResult handleAppendFailure(Action action, GrainContext context) {
    // The following method will be invoked once the topic has received the event sent from `appendEvent`.
    AppendResponse response = AppendResponse(action);
    
    String cookie = response.getCookieValue().asString();
    Status status = response.getStatus(); // Status.OK if successful
}

Last updated