All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlingo.xoom.cluster.model.ClusterControl Maven / Gradle / Ivy

// Copyright © 2012-2022 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.xoom.cluster.model;

import io.vlingo.xoom.actors.*;
import io.vlingo.xoom.cluster.model.application.ClusterApplication;
import io.vlingo.xoom.common.Tuple2;

public interface ClusterControl {
  void shutDown();

  static Tuple2 instance(
      final World world,
      final ClusterApplication.ClusterApplicationInstantiator instantiator,
      final Properties properties,
      final String nodeName) {

    final String stageName = properties.clusterApplicationStageName();
    final Stage stage = world.stageNamed(stageName);

    return instance(world, stage, instantiator, properties, nodeName);
  }

  static Tuple2 instance(
      final World world,
      final Stage stage,
      final ClusterApplication.ClusterApplicationInstantiator instantiator,
      final Properties properties,
      final String nodeName) {

    final ClusterInitializer initializer = new ClusterInitializer(nodeName, properties, world.defaultLogger());
    instantiator.node(initializer.localNode());

    final ClusterApplication application = ClusterApplication.instance(stage, instantiator);

    final Definition definition =
        new Definition(
            ClusterActor.class,
            new ClusterControl.ClusterInstantiator(initializer, application),
            "cluster-actor-" + nodeName);

    ClusterControl control = world.actorFor(ClusterControl.class, definition);

    return Tuple2.from(control, world.defaultLogger());
  }

  class ClusterInstantiator implements ActorInstantiator {
    private static final long serialVersionUID = -5766576644564817563L;

    final ClusterApplication application;
    final ClusterInitializer initializer;

    public ClusterInstantiator(final ClusterInitializer initializer, final ClusterApplication application) {
      this.initializer = initializer;
      this.application = application;
    }

    @Override
    public ClusterActor instantiate() {
      try {
        return new ClusterActor(initializer, application);
      } catch (Exception e) {
        throw new IllegalArgumentException("Failed to instantiate " + type() + " because: " + e.getMessage(), e);
      }
    }

    @Override
    public Class type() {
      return ClusterActor.class;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy