Class SpringCloudCommandBusConnector

java.lang.Object
io.axoniq.framework.springcloud.command.SpringCloudCommandBusConnector
All Implemented Interfaces:
CommandBusConnector, DescribableComponent

public class SpringCloudCommandBusConnector extends Object implements CommandBusConnector
A CommandBusConnector distributing commands across the nodes discovered through Spring Cloud Discovery.

With no server to route for it, this connector routes itself. Every dispatch resolves a Member from the SpringCloudMemberDiscovery's consistent-hash ring, using the command's routing key and name, and then either hands the command to this application's own handler or sends it to the resolved member over HTTP. That is the shape of the Axon Framework 5 contract: DistributedCommandBus passes a dispatch straight through, so routing is the connector's job.

Two consequences of that are worth being explicit about:

  • A command routed to this application is delivered through the CommandBusConnector.Handler registered by DistributedCommandBus, never by calling a local CommandBus directly. That handler owns the priority-ordered executor and the local segment, so bypassing it would lose command priority and the bus's own interception.
  • subscribe(QualifiedName, int) completes immediately, recording the subscription locally and publishing the updated capabilities. It deliberately does not wait for other members to observe the change: DistributedCommandBus joins this future on the subscribing thread, and other members learn of the subscription when they next refresh its capabilities regardless.
This connector is wired by SpringCloudConfigurationEnhancer, wrapped in a PayloadConvertingCommandBusConnector that converts payloads to String on the way out. It is not meant to be constructed directly by application code.
Since:
5.4.0
Author:
Allard Buijze
  • Constructor Details

    • SpringCloudCommandBusConnector

      public SpringCloudCommandBusConnector(SpringCloudMemberDiscovery discovery, IncomingCommandInvoker invoker, RemoteCommandDispatcher dispatcher, @Nullable MessageConverter converter)
      Constructs a SpringCloudCommandBusConnector routing with the given discovery.
      Parameters:
      discovery - the discovery holding the consistent-hash ring commands are routed with
      invoker - the component invoking the local handler for commands from other members
      dispatcher - the dispatcher sending commands to other members
      converter - the converter attached to commands routed to this application, so that a locally routed command carries the same conversion capability as one that travelled over the wire, or null when none is available
  • Method Details

    • start

      public void start()
      Starts this connector, allowing commands to be dispatched through it.

      Performed in the Phase.INBOUND_COMMAND_CONNECTOR phase.

    • dispatch

      public CompletableFuture<CommandResultMessage> dispatch(CommandMessage command, @Nullable ProcessingContext processingContext)
      Description copied from interface: CommandBusConnector
      Dispatches the given command to the appropriate command bus, which may be local or remote.
      Specified by:
      dispatch in interface CommandBusConnector
      Parameters:
      command - the command message to dispatch
      processingContext - the processing context for the command
      Returns:
      a CompletableFuture that will complete with the result of the command handling
    • subscribe

      public CompletableFuture<Void> subscribe(QualifiedName commandName, int loadFactor)
      Description copied from interface: CommandBusConnector
      Subscribes to a command with the given commandName and a loadFactor.

      Subscribing a commandName this connector is already subscribed to replaces the earlier subscription, adopting the given loadFactor. A single unsubscribe of that commandName undoes any number of subscriptions to it.

      Specified by:
      subscribe in interface CommandBusConnector
      Parameters:
      commandName - The QualifiedName of the command to subscribe to.
      loadFactor - The load factor for the command, which can be used to control the distribution of command handling across multiple instances. The load factor should be a positive integer.
      Returns:
      A CompletableFuture that completes successfully when this connector subscribed to the given commandName with the given loadFactor.
    • unsubscribe

      public boolean unsubscribe(QualifiedName commandName)
      Description copied from interface: CommandBusConnector
      Unsubscribes from a command with the given commandName.

      Undoes any number of subscriptions to the given commandName, leaving no command of that name routed to this connector.

      Specified by:
      unsubscribe in interface CommandBusConnector
      Parameters:
      commandName - The QualifiedName of the command to unsubscribe from.
      Returns:
      true if the unsubscription was successful, false otherwise.
    • onIncomingCommand

      public void onIncomingCommand(CommandBusConnector.Handler handler)
      Description copied from interface: CommandBusConnector
      Registers a handler that will be called when an incoming command is received. The handler should process the command and call the provided ResultCallback to indicate success or failure.
      Specified by:
      onIncomingCommand in interface CommandBusConnector
      Parameters:
      handler - A BiConsumer that takes a CommandMessage and a CommandBusConnector.ResultCallback.
    • disconnect

      public CompletableFuture<Void> disconnect()
      Stops advertising the commands this member handles, so that other members stop routing them here.

      Performed in the Phase.INBOUND_COMMAND_CONNECTOR phase. Incoming commands arrive over HTTP, so it is the web container's own graceful shutdown that stops new requests; this connector's part is to publish empty capabilities, so that members still running route around it on their next discovery round rather than sending commands into a closing container.

      Returns:
      a future that completes once this member no longer advertises any command
    • shutdownDispatching

      public CompletableFuture<Void> shutdownDispatching()
      Stops dispatching new commands, and waits for the ones already dispatched to be answered.

      Performed in the Phase.OUTBOUND_COMMAND_CONNECTORS phase.

      Returns:
      a future that completes once every dispatched command has been answered
    • describeTo

      public void describeTo(ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.