Search in sources :

Example 1 with Actor

use of actor4j.core.actors.Actor in project actor4j-core by relvaner.

the class ActorSystemImpl method addActor.

public UUID addActor(Class<? extends Actor> clazz, Object... args) throws ActorInitializationException {
    ActorCell cell = generateCell(clazz);
    container.registerConstructorInjector(cell.id, clazz, args);
    Actor actor = null;
    try {
        actor = (Actor) container.getInstance(cell.id);
        cell.actor = actor;
    } catch (Exception e) {
        e.printStackTrace();
        executerService.safetyManager.notifyErrorHandler(new ActorInitializationException(), "initialization", null);
    }
    return (actor != null) ? user_addCell(cell) : UUID_ZERO;
}
Also used : ActorInitializationException(actor4j.core.exceptions.ActorInitializationException) ResourceActor(actor4j.core.actors.ResourceActor) Actor(actor4j.core.actors.Actor) PseudoActor(actor4j.core.actors.PseudoActor) ActorInitializationException(actor4j.core.exceptions.ActorInitializationException)

Example 2 with Actor

use of actor4j.core.actors.Actor in project actor4j-core by relvaner.

the class ActorBalancingOnCreation method balance.

public void balance(Map<UUID, Long> cellsMap, List<ActorThread> actorThreads, Map<UUID, Long> groupsMap, Map<UUID, Integer> groupsDistributedMap, Map<UUID, ActorCell> cells) {
    List<UUID> buffer = new LinkedList<>();
    for (UUID id : cells.keySet()) buffer.add(id);
    int i = 0, j = 0;
    for (ActorCell cell : cells.values()) {
        Actor actor = cell.getActor();
        if (actor instanceof ActorGroupMember) {
            Long id = groupsMap.get(((ActorGroupMember) actor).getGroupId());
            if (id == null) {
                id = actorThreads.get(i).getId();
                groupsMap.put(((ActorGroupMember) actor).getGroupId(), id);
                i++;
                if (i == actorThreads.size())
                    i = 0;
            }
            if (buffer.remove(cell.getId()))
                cellsMap.put(cell.getId(), id);
        } else if (actor instanceof ActorDistributedGroupMember) {
            Integer threadIndex = groupsDistributedMap.get(((ActorDistributedGroupMember) actor).getGroupId());
            Long id = null;
            if (threadIndex == null) {
                id = actorThreads.get(j).getId();
                groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), j);
            } else {
                threadIndex++;
                if (threadIndex == actorThreads.size())
                    threadIndex = 0;
                id = actorThreads.get(threadIndex).getId();
                groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), threadIndex);
            }
            if (buffer.remove(cell.getId()))
                cellsMap.put(cell.getId(), id);
            j++;
            if (j == actorThreads.size())
                j = 0;
        }
    }
    i = 0;
    for (UUID id : buffer) {
        cellsMap.put(id, actorThreads.get(i).getId());
        i++;
        if (i == actorThreads.size())
            i = 0;
    }
/*
		int i=0;
		for (UUID id : system.cells.keySet()) {
			cellsMap.put(id, actorThreads.get(i).getId());
			i++;
			if (i==actorThreads.size())
				i = 0;
		}
		*/
}
Also used : ActorCell(actor4j.core.ActorCell) ActorGroupMember(actor4j.core.actors.ActorGroupMember) Actor(actor4j.core.actors.Actor) ActorDistributedGroupMember(actor4j.core.actors.ActorDistributedGroupMember) UUID(java.util.UUID) LinkedList(java.util.LinkedList)

Example 3 with Actor

use of actor4j.core.actors.Actor in project actor4j-core by relvaner.

the class RestartProtocol method postRestart.

protected void postRestart(Exception reason) {
    cell.postStop();
    try {
        Actor newActor = (Actor) cell.getSystem().getContainer().getInstance(cell.getId());
        newActor.setCell(cell);
        cell.setActor(newActor);
        cell.postRestart(reason);
        logger().info(String.format("%s - System: actor (%s) restarted", cell.getSystem().getName(), actorLabel(cell.getActor())));
    } catch (Exception e) {
        // never must occur
        throw new ActorInitializationException();
    }
}
Also used : ActorInitializationException(actor4j.core.exceptions.ActorInitializationException) Actor(actor4j.core.actors.Actor) ActorInitializationException(actor4j.core.exceptions.ActorInitializationException)

Example 4 with Actor

use of actor4j.core.actors.Actor in project actor4j-core by relvaner.

the class ActorCell method addChild.

public UUID addChild(Class<? extends Actor> clazz, Object... args) throws ActorInitializationException {
    ActorCell cell = system.generateCell(clazz);
    system.container.registerConstructorInjector(cell.id, clazz, args);
    try {
        Actor child = (Actor) system.container.getInstance(cell.id);
        cell.actor = child;
    } catch (Exception e) {
        throw new ActorInitializationException();
    }
    return internal_addChild(cell);
}
Also used : ActorInitializationException(actor4j.core.exceptions.ActorInitializationException) Actor(actor4j.core.actors.Actor) PersistentActor(actor4j.core.actors.PersistentActor) PersistenceServiceActor(actor4j.core.persistence.actor.PersistenceServiceActor) ActorInitializationException(actor4j.core.exceptions.ActorInitializationException) ActorKilledException(actor4j.core.exceptions.ActorKilledException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with Actor

use of actor4j.core.actors.Actor in project actor4j-core by relvaner.

the class ActorSystemImpl method internal_addCell.

protected UUID internal_addCell(ActorCell cell) {
    Actor actor = cell.actor;
    if (actor instanceof PseudoActor)
        pseudoCells.put(cell.id, cell);
    else {
        actor.setCell(cell);
        cells.put(cell.id, cell);
        if (actor instanceof ResourceActor)
            resourceCells.put(cell.id, false);
        if (executerService.isStarted()) {
            messageDispatcher.registerCell(cell);
            /* preStart */
            cell.preStart();
        }
    }
    return cell.id;
}
Also used : ResourceActor(actor4j.core.actors.ResourceActor) ResourceActor(actor4j.core.actors.ResourceActor) Actor(actor4j.core.actors.Actor) PseudoActor(actor4j.core.actors.PseudoActor) PseudoActor(actor4j.core.actors.PseudoActor)

Aggregations

Actor (actor4j.core.actors.Actor)16 UUID (java.util.UUID)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 ActorFactory (actor4j.core.utils.ActorFactory)8 ActorMessage (actor4j.core.messages.ActorMessage)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Consumer (java.util.function.Consumer)5 ActorSystem (actor4j.core.ActorSystem)3 PseudoActor (actor4j.core.actors.PseudoActor)3 ActorInitializationException (actor4j.core.exceptions.ActorInitializationException)3 ActorCell (actor4j.core.ActorCell)2 ActorDistributedGroupMember (actor4j.core.actors.ActorDistributedGroupMember)2 ActorGroupMember (actor4j.core.actors.ActorGroupMember)2 ResourceActor (actor4j.core.actors.ResourceActor)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ActorThread (actor4j.core.ActorThread)1 ActorTimer (actor4j.core.ActorTimer)1 EmbeddedActor (actor4j.core.actors.EmbeddedActor)1 PersistentActor (actor4j.core.actors.PersistentActor)1