pekko/akka-docs/java/code/docs/transactor/FriendlyCounter.java

40 lines
1 KiB
Java
Raw Normal View History

/**
2012-01-19 18:21:06 +01:00
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
*/
2010-11-12 16:09:31 +13:00
package docs.transactor;
2010-11-12 16:09:31 +13:00
//#class
import akka.actor.*;
import akka.transactor.*;
2010-11-12 16:09:31 +13:00
import java.util.Set;
2012-01-18 15:59:59 +13:00
import scala.concurrent.stm.Ref;
import scala.concurrent.stm.japi.STM;
2010-11-12 16:09:31 +13:00
public class FriendlyCounter extends UntypedTransactor {
Ref.View<Integer> count = STM.newRef(0);
2010-11-12 16:09:31 +13:00
@Override public Set<SendTo> coordinate(Object message) {
if (message instanceof Increment) {
Increment increment = (Increment) message;
if (increment.hasFriend())
return include(increment.getFriend(), new Increment());
}
return nobody();
}
2012-01-18 15:59:59 +13:00
public void atomically(Object message) {
2010-11-12 16:09:31 +13:00
if (message instanceof Increment) {
STM.increment(count, 1);
2010-11-12 16:09:31 +13:00
}
}
@Override public boolean normally(Object message) {
if ("GetCount".equals(message)) {
2012-01-18 15:59:59 +13:00
getSender().tell(count.get());
2010-11-12 16:09:31 +13:00
return true;
} else return false;
}
}
//#class