Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Started actors as objects with local and grid varieties. #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/XOOM-Auto-Dispatch.pdf
Binary file not shown.
38 changes: 0 additions & 38 deletions src/main/java/io/vlingo/xoom/Boot.java

This file was deleted.

116 changes: 116 additions & 0 deletions src/main/java/io/vlingo/xoom/Xoom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright © 2012-2020 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;

import java.util.concurrent.atomic.AtomicReference;

import io.vlingo.actors.Grid;
import io.vlingo.actors.Logger;
import io.vlingo.actors.World;
import io.vlingo.xoom.object.ObjectDescriptor;

public class Xoom {
private static final String DefaultWorldName = "vlingo-xoom";
private static final String GridPostfix = "-grid";

private static final AtomicReference<Xoom> xoom = new AtomicReference<>();

private Grid grid;
@SuppressWarnings("unused")
private ShutdownHook shutdownHook;
private World world;

public static void main(final String[] args) {
final String name = args.length > 0 ? args[0] : DefaultWorldName;

start(name);
}

/**
* Answer the {@code Xoom}.
* @return Xoom
*/
public static Xoom xoom() {
return xoom.get();
}

/**
* Answers a new {@code World} with the given {@code name} and that is configured with
* the contents of the {@code vlingo-zoom.properties} file.
* @param name the {@code String} name to assign to the new {@code World} instance
* @return {@code World}
*/
public static synchronized Xoom start(final String name) {
if (xoom.get() == null) {
xoom.set(new Xoom(name));
}
return xoom.get();
}

/**
* Answer a new object of type {@code T} from the {@code ObjectDescriptor<T, ?>}
* using my {@code grid}.
* @param <T> the protocol of the object
* @param descriptor the {@code ObjectDescriptor<T, ?>} through which the new object instance will be obtained
* @return T
*/
public <T> T objectInstance(final ObjectDescriptor<T, ?> descriptor) {
return descriptor.objectInstance(grid);
}

/**
* Answer my {@code Grid}.
* @return Grid
*/
public Grid grid() {
return xoom.get().grid;
}

/**
* Answer my {@code World}.
* @return World
*/
public World world() {
return xoom.get().world;
}

/**
* Construct my state with a {@code name}.
* @param name the String name of my World
*/
private Xoom(final String name) {
try {
this.world = World.start(name);
this.grid = Grid.start(world, name + GridPostfix);
this.shutdownHook = new ShutdownHook();
} catch (Exception e) {
System.out.println("Xoom failed to start because: " + e.getMessage());
e.printStackTrace();
}
}

private final class ShutdownHook {
private ShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (xoom.get() != null) {
final World world = xoom.get().world;
final Logger logger = world.defaultLogger();
final String name = world.name();

logger.info("\n==========");
logger.info("Stopping Xoom: '" + name + "' ...");
world.terminate();
xoom.set(null);
logger.info("Stopped Xoom: '" + name + "'");
} else {
System.out.println("Stopped unstarted Xoom."); // this should be unreacable
}
}));
}
}
}
72 changes: 72 additions & 0 deletions src/main/java/io/vlingo/xoom/object/ObjectDescriptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright © 2012-2020 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.object;

import io.vlingo.actors.Actor;
import io.vlingo.actors.Grid;
import io.vlingo.actors.Stage;
import io.vlingo.actors.World;

/**
* A descriptor used to create runtime objects.
* @param <P> the {@code Class<P>} of the protocol
* @param <T> the {@code Class<T>} of the concrete actor type
*/
public class ObjectDescriptor<P, T extends Actor> {
private final Class<P> protocol;
private final Class<T> type;

/**
* Answer a new {@code ObjectDescriptor}.
* @param <P> the {@code Class<P>} of the protocol
* @param <T> the {@code Class<T>} of the concrete actor type
* @param protocol the {@code Class<P>} protocol that the the object supports
* @param type the {@code Class<T>} concrete Actor that implements the protocol
* @return {@code ObjectDescriptor<P, T>}
*/
public static <P, T extends Actor> ObjectDescriptor<P, T> descriptorFor(final Class<P> protocol, final Class<T> type) {
return new ObjectDescriptor<P, T>(protocol, type);
}

/**
* Constructs my state.
* @param protocol the {@code Class<P>} protocol that the the object supports
* @param type the {@code Class<T>} concrete Actor that implements the protocol
*/
public ObjectDescriptor(final Class<P> protocol, final Class<T> type) {
this.protocol = protocol;
this.type = type;
}

/**
* Answer a new instance of the {@code P} object within the {@code grid}. Note that
* being in the {@code grid} the new object could be partitioned onto any node in
* the {@code grid} cluster.
* @param grid the Grid within which the object is instantiated
* @return P
*/
public P objectInstance(final Grid grid) {
return grid.actorFor(protocol, type);
}

public P objectInstance(final Stage stage) {
return stage.actorFor(protocol, type);
}

public P objectInstance(final World world) {
return objectInstance(world.stage());
}

public Class<P> protocol() {
return protocol;
}

public Class<T> type() {
return type;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/codegen/EventSourcedEntity.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class ${entityName} extends EventSourced implements ${aggregateProt
* @param currentVersion the int value of my current version; may be helpful in determining if snapshot is needed
*/
@Override
protected <AccountState> void restoreSnapshot(final ${stateName} snapshot, final int currentVersion) {
protected <${stateName}> void restoreSnapshot(final ${stateName} snapshot, final int currentVersion) {
// OVERRIDE FOR SNAPSHOT SUPPORT
// See: https://docs.vlingo.io/vlingo-lattice/entity-cqrs#eventsourced
}
Expand All @@ -50,7 +50,7 @@ public final class ${entityName} extends EventSourced implements ${aggregateProt
* Answer the valid {@code ${stateName}} instance if a snapshot should
* be taken and persisted along with applied {@code Source<T>} instance(s).
*
* @return AccountState
* @return ${stateName}
*/
@Override
protected ${stateName} snapshot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.xoom;

import io.vlingo.actors.World;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import io.vlingo.actors.World;

public class BootTest {
public class XoomTest {

private static final String BootWorldName = "test-boot";

@Test
public void testThatWorldBoots() {
Boot.main(new String[] { BootWorldName });
Xoom.main(new String[] { BootWorldName });

final World world = Boot.xoomBootWorld();
final World world = Xoom.xoom().world();

assertEquals(BootWorldName, world.name());
}

@Test
public void testThatWorldStarts() {
final World world = Boot.start(BootWorldName);
final World world = Xoom.start(BootWorldName).world();

assertEquals(BootWorldName, world.name());
}
Expand Down