Evento Framework
  • Introduction
  • Architecture Overview
    • Distributed Systems & Microservices
    • Recative Manifesto & Reactive Principles
    • State-of-the-art Patterns
      • DDD (Domain Driven Design)
      • CQRS (Command Query Responsability Separation)
      • Event Sourcing
      • Messaging
      • Saga
  • GETTING STARTED
    • Quick Start
    • TodoList - RECQ Tutorial
      • Problem Description and Requirement Gathering
      • RECQ Payload Design
      • RECQ Components Design
      • Set up your Development Environment
      • RECQ Payload Evento Implementation
        • Domain Commands
        • Domain Events
        • Views
        • Queries
      • RECQ Components Evento Implementation with Spring Data
        • TodoListAggregate
        • TodoList Model with Spring Data
        • TodoListProjector
        • TodoListProjection
        • TodoList Invoker
      • Expose the RECQ architecture with Spring Web
      • Test Your App
    • Extend TodoList - Handle Complexity Tutorial
      • Unique identifier generation
      • Extends behaviors with Observer and Services
      • Cross Domain Consistency with Sagas
      • Handle Real time data updates with MQTT and Save-Notify Pattern
  • RECQ Patterns
    • RECQ Patterns
    • RECQ System Pattern
      • Component
      • Message Gateway
      • System State Store
    • RECQ Communication Pattern
      • Component to Component
      • Component to System State Store
      • System State Store to Component
    • RECQ Component Pattern
      • Aggregate
      • Projector
      • Projection
      • Service
      • Invoker
      • Saga
      • Observer
  • Evento Framework
    • Evento Framework Introcution
    • Payload and Messages
      • Command
        • Domain Command
        • Service Command
      • Event
        • Domain and Service Event
      • Query and View
    • @Component
      • @Aggregate
        • Aggregate State
        • @AggregateCommandHandler
        • @EventSourcingHandler
      • @Projector
        • Projector @EventHandler
      • @Projection
        • @QueryHandler
      • @Service
        • @CommandHandler
      • @Invoker
      • @Saga
        • SagaState
        • @SagaEventHandler
      • @Observer
    • Dead Event Queues
    • EventoBundle
      • EventoServerMessageBusConfiguration
      • ConsumerStateStore
        • InMemoryConsumerStateStore
        • PostgresConsumerStateStore
        • MysqlConsumerStateStore
      • Context
      • TracingAgend and @Track
        • SentryTracingAgent
      • Autoscaling Protocol
        • ThreadCountAutoscalingProtocol
      • Injector and @Component
  • Evento Server
    • Evento Server Introduction
    • SetUp Evento Server
      • Advanced Options
      • Evento Event Store Modes: APES and CPES
    • Evento Server Cluster
    • Bundle Deploy Script
  • EVENTO GUI
    • Explore RECQ Systems Visually
    • GUI Auth
    • Payload Catalog
    • Component Catalog
    • Bundle Catalog
    • Cluster Status (Experimental)
    • Flows
      • Performance Evaluation
    • Application Graph
    • System State Store
  • Evento CLI
    • Evento CLI Introduction
    • Update Version
    • Publish
Powered by GitBook
On this page
  1. Evento Framework
  2. @Component
  3. @Service

@CommandHandler

Chapter: Handling Commands with @CommandHandler within Services

Within Evento applications built on the RECQ architecture, services play a crucial role in interacting with the outside world. This chapter explores the @CommandHandler annotation, a vital tool for defining methods within services that handle incoming commands.

Understanding @CommandHandler

The @CommandHandler annotation marks a method within your @Service class as a command handler. This method is responsible for processing a specific command object, potentially validating its contents, and performing the necessary actions based on the command's intent.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Handler
public @interface CommandHandler {
}

Here's a breakdown of the annotation's definition:

  • @Retention(RetentionPolicy.RUNTIME): Ensures the annotation information is retained at runtime, allowing Evento's CommandGateway to discover and execute these methods when commands arrive.

  • @Target(ElementType.METHOD): Specifies that the annotation can only be applied to method declarations within your service class.

  • @Handler (Optional): In some frameworks like Evento, @CommandHandler might inherit from a base annotation like @Handler for consistency in marking different handler types.

Structure of a @CommandHandler Method

A well-structured @CommandHandler method typically adheres to the following pattern:

  1. Command Object as First Parameter: The first parameter of the method must be a subtype of the Command class representing the specific type of command being handled (e.g., NotificationSendCommand).

  2. Optional Additional Parameters: Depending on your specific needs, you might include additional parameters like:

    • CommandGateway (Optional): This allows sending subsequent commands from within the current command handler (advanced usage).

    • CommandMessage (Optional): Provides access to details about the received command message, including metadata and timestamp.

    • Metadata (Optional): Represents metadata associated with the command (often already included in the CommandMessage object).

    • Instant (Optional): Represents the timestamp of the command message.

  3. Command Validation (Optional): You might include logic to validate the received command's structure and data integrity before proceeding.

  4. Action Execution: The core logic of the command handler involves performing the necessary actions based on the command. This might involve:

    • Interacting with external APIs (e.g., sending a notification through an email service).

    • Updating internal state (for stateful services).

  5. Event Emission (Optional): In some scenarios, the command handler might emit domain events after performing its operation to update the system state.

Example: @CommandHandler in Action

@CommandHandler
NotificationSentEvent handle(NotificationSendCommand command,
							 CommandGateway commandGateway,
							 CommandMessage<NotificationSendCommand> commandMessage) {
	if(command.getBody() == null){
		throw new RuntimeException("error.body.null");
	}
	Utils.logMethodFlow(this, "handle", command, "BEGIN");
	String notificationId = service.send(command.getBody());
	Utils.logMethodFlow(this, "handle", command, "END");
	return new NotificationSentEvent(notificationId, command.getBody());
}

@CommandHandler
void handle(NotificationSendSilentCommand command) {
	Utils.logMethodFlow(this, "handle", command, "BEGIN");
	service.send(command.getBody());
	Utils.logMethodFlow(this, "handle", command, "END");
}

The provided code example showcases a NotificationService class:

  • Both methods are annotated with @CommandHandler, indicating they handle commands.

  • The first method handles NotificationSendCommand, validates the message body, sends the notification through an external service, and emits a NotificationSentEvent.

  • The second method handles NotificationSendSilentCommand and simply sends the notification without emitting an event (potentially a stateless operation).

Key Takeaways

  • @CommandHandler empowers you to define methods within services that act as handlers for specific commands.

  • These methods validate, process, and potentially trigger actions based on the incoming commands.

  • Understanding @CommandHandler is essential for building services that effectively handle commands and interact with the external world within your Evento applications.

Remember, the additional parameters like CommandGateway, CommandMessage, Metadata, and Instant provide flexibility for more advanced scenarios, which might be covered in separate chapters.

Previous@ServiceNext@Invoker

Last updated 1 year ago