Class CompositeDiscovery

java.lang.Object
com.loomcache.server.discovery.CompositeDiscovery
All Implemented Interfaces:
DiscoveryStrategy

public class CompositeDiscovery extends Object implements DiscoveryStrategy
Composite discovery strategy that combines multiple discovery mechanisms.

Delegates to a list of underlying strategies and merges their results, deduplicating by nodeId. Useful for fallback scenarios: - Try multicast first - Fall back to static seeds - Fall back to environment variables

If any strategy fails to start, an error is logged but startup continues. Stopping a composite strategy stops all underlying strategies.

Thread-safe.

  • Constructor Details

    • CompositeDiscovery

      public CompositeDiscovery(List<DiscoveryStrategy> strategies)
      Create a composite discovery strategy with the given strategies.
      Parameters:
      strategies - list of strategies to combine (evaluated in order)
    • CompositeDiscovery

      public CompositeDiscovery(List<DiscoveryStrategy> strategies, long discoverPeersTimeoutMs)
      Create a composite discovery strategy with the given strategies and discovery timeout.
      Parameters:
      strategies - list of strategies to combine (evaluated in order)
      discoverPeersTimeoutMs - timeout in milliseconds for a discovery round
  • Method Details

    • start

      public void start() throws Exception
      Description copied from interface: DiscoveryStrategy
      Start the discovery strategy.

      Initializes the discovery mechanism and starts any background threads or I/O operations (e.g., watching files, listening to multicast, resolving DNS). Implementations must handle errors gracefully and should not leave resources in an inconsistent state if startup fails.

      Specified by:
      start in interface DiscoveryStrategy
      Throws:
      Exception - if startup fails; specific exceptions depend on implementation
    • stop

      public void stop()
      Description copied from interface: DiscoveryStrategy
      Stop the discovery strategy and release resources.

      Cleanly shuts down the discovery mechanism, including stopping any background threads and closing I/O resources. Safe to call multiple times and must be idempotent (calling twice should have no additional effect).

      Specified by:
      stop in interface DiscoveryStrategy
    • discoverPeers

      public List<PeerAddress> discoverPeers()
      Description copied from interface: DiscoveryStrategy
      Discover available peers.

      Returns a snapshot of currently known peers. If no peers are available, an empty list is returned (not null). The returned list is mutable and modifications should not affect internal strategy state.

      Specified by:
      discoverPeers in interface DiscoveryStrategy
      Returns:
      list of discovered peer addresses; empty list if none available
    • getName

      public String getName()
      Description copied from interface: DiscoveryStrategy
      Get the name of this discovery strategy.

      Used for logging, metrics, and debugging purposes.

      Specified by:
      getName in interface DiscoveryStrategy
      Returns:
      human-readable strategy name (e.g., "FileBasedDiscovery", "MulticastDiscovery")
    • toString

      public String toString()
      Overrides:
      toString in class Object