co.paralleluniverse.actors.ActorRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quasar-actors Show documentation
Show all versions of quasar-actors Show documentation
Fibers, Channels and Actors for the JVM
/*
* Quasar: lightweight threads and actors for the JVM.
* Copyright (C) 2013, Parallel Universe Software Co. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
package co.paralleluniverse.actors;
import co.paralleluniverse.common.util.Exceptions;
import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.common.util.ServiceUtil;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import jsr166e.ConcurrentHashMapV8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pron
*/
public class ActorRegistry {
// TODO: there are probably race conditions here
private static final Logger LOG = LoggerFactory.getLogger(ActorRegistry.class);
private static final ConcurrentMap registeredActors = new ConcurrentHashMapV8();
private static final GlobalRegistry globalRegistry = ServiceUtil.loadSingletonServiceOrNull(GlobalRegistry.class);
static {
LOG.info("Global registry is {}", globalRegistry);
}
static Object register(Actor, ?> actor) {
final String name = actor.getName();
if (name == null)
throw new IllegalArgumentException("name is null");
// atomically register
final ActorRef ref = actor.ref();
final Entry old = registeredActors.get(name);
if (old != null && old.actor == actor.ref())
return old.globalId;
if (old != null && LocalActorUtil.isLocal(old.actor) && !LocalActorUtil.isDone(old.actor))
throw new RegistrationException("Actor " + old + " is not dead and is already registered under " + name);
if (old != null)
LOG.info("Re-registering {}: old was {}", name, old);
if (old != null && !registeredActors.remove(name, old))
throw new RegistrationException("Concurrent registration under the name " + name);
final Entry entry = new Entry(null, ref);
if (registeredActors.putIfAbsent(name, entry) != null)
throw new RegistrationException("Concurrent registration under the name " + name);
LOG.info("Registering {}: {}", name, actor);
final Object globalId = globalRegistry != null ? registerGlobal(actor.ref()) : name;
entry.globalId = globalId;
actor.monitor();
return globalId;
}
static private Object registerGlobal(final ActorRef> actor) {
try {
return new Fiber