Class View<S>

Object
kalix.javasdk.view.View<S>
Type Parameters:
S - The type of the state for this view.

public abstract class View<S> extends Object
Kalix applications follow the Command Query Responsibility Segregation (CQRS) pattern (see https://developer.lightbend.com/docs/akka-guide/concepts/cqrs.html).

Kalix' Entities represent the command side where you change the state of your model in a strictly consistent and isolated manner. Kalix' Views represent the query side of your application. Views are optimized for reads and allow you to query your model by fields other than the entity identifier.

When implementing a View, you define what will be its internal state (your view model) and how you want to query it. The Query string defines which fields will be indexed and how the query will be executed.

The query is executed by the Kalix when a request is made to the View.

Views are updated in response to Event Sourced Entity events, Value Entity state changes or messages from a Topic.

Each incoming change is handled by a command handler. Command handlers are methods returning an View.UpdateEffect. The command handler is responsible for updating the View state.

  • Constructor Details

    • View

      public View()
  • Method Details

    • updateContext

      protected final UpdateContext updateContext()
      Additional context and metadata for an update handler.

      It will throw an exception if accessed from constructor.

    • _internalSetUpdateContext

      public void _internalSetUpdateContext(Optional<UpdateContext> context)
      INTERNAL API
    • _internalSetViewState

      public void _internalSetViewState(S state)
      INTERNAL API
    • viewState

      @ApiMayChange protected final S viewState()
      Returns the view state (row) for this invocation as currently stored in Kalix.

      Note that modifying the state directly will not update it in storage. To save the state, one must call {effects().updateState()}.

      This method can only be called when handling a command. Calling it outside a method (eg: in the constructor) will raise a IllegalStateException exception.

      Throws:
      IllegalStateException - if accessed outside a handler method
    • effects

      protected final View.UpdateEffect.Builder<S> effects()
    • emptyState

      public S emptyState()
      The default implementation of this method returns null. It can be overridden to return a more sensible initial state.
      Returns:
      an empty state object or `null` to hand to the process method when an event for a previously unknown subject id is seen.