Skip to content

Commit

Permalink
Unbreak docs
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren committed Sep 7, 2021
1 parent 9bef805 commit 01ee0d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
16 changes: 8 additions & 8 deletions docs/src/modules/java/pages/actions.adoc
Expand Up @@ -16,14 +16,14 @@ The class implementing an Action needs to be annotated with the `@Action` annota

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/product/ToProductPopularityAction.java[tag=annotation]
include::example$java-eventing-shopping-cart/src/main/java/shopping/product/actions/ToProductPopularityServiceAction.java[tag=annotation]
----

Action methods implementing services require the `@Handler` annotation and may have `ActionContext` as their second parameter.

[source,java,indent=2]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/product/ToProductPopularityAction.java[tag=methods]
include::example$java-eventing-shopping-cart/src/main/java/shopping/product/actions/ToProductPopularityServiceAction.java[tag=methods]
----

To connect the Protobuf service definition with the implementation as an Action, register the implementing class with Akka Serverless:
Expand All @@ -41,19 +41,19 @@ The publisher (or source) may publish an arbitrary number of replies.

=== Forwarding a call from an Action

Action methods that want to forward calls to other service methods use `Reply<T>` as return type. With `Reply<T>` the method can choose to
Action methods that want to forward calls to other service methods use `Effect<T>` as return type. With `Effect<T>` the method can choose to

- reply with a value (`Reply.message`)
- send no reply (`Reply.noReply`)
- forward the call (`Reply.forward`)
- reply with a value (`effects().reply(T)`)
- send no reply (`effects().noReply()`)
- forward the call (`effects().forward()`)

To forward to a different method, look up the target method via the `ActionContext` by specifying the full Protobuf service name, method name and passing the parameter type's class.

[source,java,indent=2]
----
include::java:example$java-eventing-shopping-cart/src/main/java/shopping/product/ToProductPopularityAction.java[tag=forwardRemoved]
include::java:example$java-eventing-shopping-cart/src/main/java/shopping/product/actions/ToProductPopularityServiceAction.java[tag=forwardRemoved]
----
<1> Use `ActionReply<Empty>` as return type and accept an `ActionContext` as second parameter
<1> Use `Effect<Empty>` as return type
<2> Create the parameter the target method accepts
<3> Use the fully-qualified gRPC service name of the target method
<4> Specify the Protobuf rpc method name
Expand Down
30 changes: 8 additions & 22 deletions docs/src/modules/java/pages/eventsourced.adoc
Expand Up @@ -9,7 +9,7 @@ Create an Event Sourced Entity by annotating it with the link:{attachmentsdir}/a

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=class]
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/domain/ShoppingCartEntity.java[tag=class]
----

The link:{attachmentsdir}/api/com/akkaserverless/javasdk/eventsourcedentity/EventSourcedEntity.html#entityType()[`entityType` {tab-icon}, window="new"] provides a namespace for journal events. Use the simple name for the Entity class. To have a more unique reference, the example above uses `eventsourced-shopping-cart`.
Expand All @@ -34,7 +34,7 @@ Each Entity should store its state locally in a mutable variable, either a mutab

[source,java,indent=4]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=state]
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/domain/ShoppingCartEntity.java[tag=state]
----

== Constructing
Expand All @@ -43,7 +43,7 @@ Akka Serverless constructs instances of the Event Sourced Entity class on demand

[source,java,indent=4]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=constructor]
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/domain/ShoppingCartEntity.java[tag=constructor]
----

== Handling commands
Expand All @@ -56,7 +56,7 @@ The following shows the implementation of the `GetCart` command handler:

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=getCart]
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/domain/ShoppingCartEntity.java[tag=getCart]
----

== Emitting events
Expand All @@ -67,7 +67,7 @@ IMPORTANT: The **only** way for a command handler to modify Entity state is by e

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=addItem]
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/domain/ShoppingCartEntity.java[tag=addItem]
----

This command handler also validates the command, ensuring the quantity of items added is greater than zero. Invoking link:{attachmentsdir}/api/com/akkaserverless/javasdk/ClientActionContext.html#fail(java.lang.String)[`context.fail` {tab-icon}, window="new"] fails the command - this method throws - no need to explicitly throw an exception.
Expand All @@ -91,28 +91,14 @@ The following example shows an event handler for the `ItemAdded` event with a ut

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=itemAdded]
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/domain/ShoppingCartEntity.java[tag=itemAdded]
----

== Producing and handling snapshots
== Snapshots

Snapshots are an important optimization for Event Sourced Entities that emit many events. Rather than reading the entire journal upon loading or restart, Akka Serverless can initiate them from a snapshot.

To produce a snapshot, declare a method annotated with link:{attachmentsdir}/api/com/akkaserverless/javasdk/eventsourcedentity/Snapshot.html[`@Snapshot` {tab-icon}, window="new"]. It takes a context class of type link:{attachmentsdir}/api/com/akkaserverless/javasdk/eventsourcedentity/SnapshotContext.html[`SnapshotContext` {tab-icon}, window="new"], and must return a snapshot of the current state in serializable form.

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=snapshot]
----

When the Event Sourced Entity is loaded again, the snapshot will be loaded before any other events are received, and passed to a snapshot handler. Snapshot handlers are declared by annotating a method with link:{attachmentsdir}/api/com/akkaserverless/javasdk/eventsourcedentity/SnapshotHandler.html[`@SnapshotHandler` {tab-icon}, window="new"], and it can take a context class of type link:{attachmentsdir}/api/com/akkaserverless/javasdk/eventsourcedentity/SnapshotContext.html[`SnapshotContext` {tab-icon}, window="new"].

Multiple snapshot handlers may be defined to handle different types of snapshots. The type matching is done in the same way as for events.

[source,java,indent=0]
----
include::example$java-eventing-shopping-cart/src/main/java/shopping/cart/ShoppingCartEntity.java[tag=handleSnapshot]
----
Snapshots are handled automatically by Akka Serverless without any specific code required. By default, a snapshot is taken for every 100:th event emitted. Tuning how often a snapshot of the state is taken can be configured using config or programmatically handing a `EventSourcedEntityOptions` to the provider when registering the entity.

== Registering the Entity

Expand Down
Expand Up @@ -5,6 +5,8 @@

package shopping;

// tag::ToProductPopularityAction[]
// tag::RegisterEventSourcedEntity[]
import com.akkaserverless.javasdk.AkkaServerless;
// end::RegisterEventSourcedEntity[]
// end::ToProductPopularityAction[]
Expand Down Expand Up @@ -50,13 +52,16 @@ public static AkkaServerless createAkkaServerless() {
// consume shopping cart events published to 'shopping-cart-events' topic
ShoppingCartAnalyticsServiceAction::new,
// view of the shopping carts
ShoppingCartViewServiceView::new);
ShoppingCartViewServiceView::new
// tag::ToProductPopularityAction[]
// tag::RegisterEventSourcedEntity[]
);
}

public static void main(String[] args) throws Exception {
LOG.info("starting the Akka Serverless service");
createAkkaServerless().start();
}
}
}
// end::RegisterEventSourcedEntity[]
// end::ToProductPopularityAction[]
Expand Up @@ -37,7 +37,7 @@ public ToProductPopularityServiceAction(ActionCreationContext creationContext) {

// tag::methods[]
@Override
public Effect<Empty> forwardAdded(ShoppingCartDomain.ItemAdded itemAdded) {
public Effect<Empty> forwardAdded(ShoppingCartDomain.ItemAdded itemAdded) { // <1>
// end::methods[]

ProductPopularityApi.IncreasePopularity increase =
Expand Down

0 comments on commit 01ee0d7

Please sign in to comment.