2011-12-21 21:32:20 +13:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-21 21:32:20 +13:00
|
|
|
*/
|
|
|
|
|
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.transactor;
|
2011-12-21 21:32:20 +13:00
|
|
|
|
|
|
|
|
//#class
|
|
|
|
|
import akka.transactor.*;
|
2012-01-18 15:59:59 +13:00
|
|
|
import scala.concurrent.stm.Ref;
|
2012-02-06 11:34:47 +13:00
|
|
|
import scala.concurrent.stm.japi.STM;
|
2011-12-21 21:32:20 +13:00
|
|
|
|
|
|
|
|
public class Counter extends UntypedTransactor {
|
2012-02-06 11:34:47 +13:00
|
|
|
Ref.View<Integer> count = STM.newRef(0);
|
2011-12-21 21:32:20 +13:00
|
|
|
|
2012-01-18 15:59:59 +13:00
|
|
|
public void atomically(Object message) {
|
2011-12-21 21:32:20 +13:00
|
|
|
if (message instanceof Increment) {
|
2012-02-06 11:34:47 +13:00
|
|
|
STM.increment(count, 1);
|
2011-12-21 21:32:20 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override public boolean normally(Object message) {
|
|
|
|
|
if ("GetCount".equals(message)) {
|
2012-01-18 15:59:59 +13:00
|
|
|
getSender().tell(count.get());
|
2011-12-21 21:32:20 +13:00
|
|
|
return true;
|
|
|
|
|
} else return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#class
|