Subscribing to a journal

An Event Sourced Entity’s journal contains events that capture all state changes. By subscribing to the journal (with the Event Sourced Entity’s type name), another component can receive all events emitted of that type.

Use case: send asynchronous messages

By combining the processing of a journal with publishing to brokers, a service can trigger other services asynchronously via messaging. To achieve this, create an Action that subscribes to a journal and publishes messages to a topic.

Use case: external calls

A service might need to trigger other systems when certain events happened to an Entity. An Action can be connected to the Entity’s journal and react on certain events to issue calls (eg. via HTTP or gRPC). To achieve this, create an Action that subscribes to a journal and let the implementation call other services.

Subscribing

To subscribe to an Event Sourced Entity’s log, define Protobuf rpc methods for all kinds of events in the Journal that you want to receive. Annotate these methods with the (akkaserverless.method).eventing annotation and specify the Entity type name of the Event Sourced Entity.

import "google/protobuf/empty.proto";
import "akkaserverless/annotations.proto";

package shopping.cart.actions;

service ToProductPopularityService {

    rpc ForwardAdded(ItemAdded) returns (google.protobuf.Empty) { (1)
        option (akkaserverless.method).eventing = { (2)
            in: {
              event_log:  "eventsourced-shopping-cart" (3)
            }
        };
    };

    rpc ForwardRemoved(ItemRemoved) returns (google.protobuf.Empty) {
        option (akkaserverless.method).eventing = {
            in: {
              event_log:  "eventsourced-shopping-cart"
            }
        };
    };
}
1 create rpc methods for all Protobuf types in the journal
2 annotate the methods with (akkaserverless.method).eventing
3 specify the Event Sourced Entity’s type name as journal source

There is nothing specific required in the implementation of these methods. The implementation will in most cases be an Action and forward a converted message to a different component (e.g. an Event Sourced Entity).

Accessing the Entity ID

For many use cases, a subscriber to an event log will trigger other services and needs to pass the entity ID to the receiver. The events of an Event Sourced entity do by design not include the entity ID, but it is made available to the subscriber via the CloudEvent metadata field subject.

Ignoring events

When listening to an event log, all events emitted by the Event Sourced entity must be matched by a Protobuf service method. In case your component is interested only in certain events, you may declare a method to receive all events that are not received by the other methods.

    // handle other events which are not managed above
    rpc CatchOthers(google.protobuf.Any) returns (google.protobuf.Empty) {
        option (akkaserverless.method).eventing = {
            in: {
                event_log:  "eventsourced-shopping-cart"
            }
        };
    };

The corresponding implementation must exist in the component.