akka学习教程(九) STM软件事务内存

akka系列文章目录

  • akka学习教程(十四) akka分布式实战
  • akka学习教程(十三) akka分布式
  • akka学习教程(十二) Spring与Akka的集成
  • akka学习教程(十一) akka持久化
  • akka学习教程(十) agent
  • akka学习教程(九) STM软件事务内存
  • akka学习教程(八) Actor中的Future-询问模式
  • akka学习教程(七) 内置状态转换Procedure
  • akka学习教程(六) 路由器Router
  • akka学习教程(五) inbox消息收件箱
  • akka学习教程(四) actor生命周期
  • akka学习教程(三) 不可变对象
  • akka学习教程(二)HelloWord
  • akka学习教程(一)简介

软件事务内存(STM):顾名思义,这是事务。与关系型数据库中的事务类似,具有ACID属性。
在分布式任务中,有可能会有和事务相关的处理,这里将举例说明AKKA中STM的用法。

假设公司给员工发工资业务,CompanyActor是公司actor, EmployeeActor是员工actor,公司账户减钱,员工账户加钱,这就要求事务。代码中有重要注释。

package akka.stm;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.transactor.Coordinated;
import akka.util.Timeout;
import com.typesafe.config.ConfigFactory;
import scala.concurrent.Await;

import java.util.concurrent.TimeUnit;

/**
 * Created by liubenlong on 2017/1/16.
 */
public class STMMain {
    public static ActorRef companyActor = null;
    public static ActorRef employeeActor = null;


    public static void main(String [] args) throws Exception {
        ActorSystem system = ActorSystem.create("stm", ConfigFactory.load("akka.conf"));
        companyActor = system.actorOf(Props.create(CompanyActor.class), "CompanyActor");
        employeeActor = system.actorOf(Props.create(EmployeeActor.class), "EmployeeActor");

        Timeout timeout = new Timeout(1, TimeUnit.SECONDS);

        for(int i = 0 ; i < 23; i ++){
            companyActor.tell(new Coordinated(i, timeout), ActorRef.noSender());

            Thread.sleep(200);

            int companyCount = (int) Await.result(Patterns.ask(companyActor, "getCount", timeout), timeout.duration());
            int employeeCount = (int) Await.result(Patterns.ask(employeeActor, "getCount", timeout), timeout.duration());

            System.out.println("companyCount = " + companyCount + ";employeeCount = " + employeeCount);
            System.out.println("-----------------------");
        }

    }
}
package akka.stm;

import akka.actor.*;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.transactor.Coordinated;
import scala.concurrent.stm.Ref;
import scala.concurrent.stm.japi.STM;

/**
 * Created by liubenlong on 2017/1/12.
 */
public class CompanyActor extends UntypedActor {

    private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    private Ref.View count = STM.newRef(100);//定义账户余额

    @Override
    public void onReceive(Object o) throws Throwable {
        if(o instanceof Coordinated){
            Coordinated coordinated = (Coordinated) o;
            int downCount = (int) coordinated.getMessage();//传递过来的参数,减多少。
            STMMain.employeeActor.tell(coordinated.coordinate(downCount), getSelf());//通知employeeActor增加费用

            try {//注意这里异常要及时处理,否则异常会一直扩散,导致回退到系统刚启动时的初始状态!
                coordinated.atomic(() -> {
                    if(count.get() < downCount) throw new RuntimeException("余额不足!");
                    STM.increment(count, -downCount);//减余额
                });
            }catch (Exception e){
                e.printStackTrace();
            }
        }else if("getCount".equals(o)){
            getSender().tell(count.get(), getSelf());
        }else{
            unhandled(o);
        }
    }

}
package akka.stm;

import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.transactor.Coordinated;
import scala.concurrent.stm.Ref;
import scala.concurrent.stm.japi.STM;

/**
 * Created by liubenlong on 2017/1/12.
 */
public class EmployeeActor extends UntypedActor {

    private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    private Ref.View count = STM.newRef(20);//员工账户

    @Override
    public void onReceive(Object o) throws Throwable {
        if(o instanceof Coordinated){
            Coordinated coordinated = (Coordinated) o;
            int downCount = (int) coordinated.getMessage();//员工增加的工资

            try {//注意这里异常要及时处理,否则异常会一直扩散,导致回退到系统刚启动时的初始状态!
                coordinated.atomic(() -> STM.increment(count, downCount));//Employee增加工资
            }catch (Exception e){
                e.printStackTrace();
            }

        }else if("getCount".equals(o)){
            getSender().tell(count.get(), getSelf());
        }else{
            unhandled(o);
        }
    }

}

运行结果:

"D:\Program Files\Java\jdk1.8.0_111\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\Program Files (x86)\ideaIU-2016.2.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\workspace_git_idea\luffi-master\luffi\target\test-classes;D:\workspace_git_idea\luffi-master\luffi\target\classes;D:\LocalRepository\commons-fileupload\commons-fileupload\1.3.2\commons-fileupload-1.3.2.jar;D:\LocalRepository\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\LocalRepository\org\apache\httpcomponents\httpmime\4.3.1\httpmime-4.3.1.jar;D:\LocalRepository\org\apache\httpcomponents\httpclient\4.3.1\httpclient-4.3.1.jar;D:\LocalRepository\org\apache\httpcomponents\httpcore\4.3\httpcore-4.3.jar;D:\LocalRepository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\LocalRepository\commons-codec\commons-codec\1.6\commons-codec-1.6.jar;D:\LocalRepository\com\alibaba\fastjson\1.2.17\fastjson-1.2.17.jar;D:\LocalRepository\com\letv\openapi\openapi-sdk-java\1.0.0-SNAPSHOT\openapi-sdk-java-1.0.0-20160721.094717-1.jar;D:\LocalRepository\org\slf4j\slf4j-api\1.7.5\slf4j-api-1.7.5.jar;D:\workspace_git_idea\luffi-master\luffi-api\target\classes;D:\workspace_git_idea\luffi-master\luffi-model\target\classes;D:\LocalRepository\com\github\pagehelper\pagehelper\4.1.1\pagehelper-4.1.1.jar;D:\LocalRepository\com\github\jsqlparser\jsqlparser\0.9.4\jsqlparser-0.9.4.jar;D:\LocalRepository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;D:\LocalRepository\javax\ws\rs\javax.ws.rs-api\2.0.1\javax.ws.rs-api-2.0.1.jar;D:\LocalRepository\javax\annotation\javax.annotation-api\1.2\javax.annotation-api-1.2.jar;D:\workspace_git_idea\luffi-master\luffi-repository\target\classes;D:\LocalRepository\tk\mybatis\mapper\3.3.8\mapper-3.3.8.jar;D:\LocalRepository\javax\persistence\persistence-api\1.0\persistence-api-1.0.jar;D:\LocalRepository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.1.1\mybatis-spring-boot-starter-1.1.1.jar;D:\LocalRepository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.1.1\mybatis-spring-boot-autoconfigure-1.1.1.jar;D:\LocalRepository\org\mybatis\mybatis\3.4.0\mybatis-3.4.0.jar;D:\LocalRepository\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;D:\LocalRepository\org\springframework\boot\spring-boot-starter-jdbc\1.3.3.RELEASE\spring-boot-starter-jdbc-1.3.3.RELEASE.jar;D:\LocalRepository\org\apache\tomcat\tomcat-jdbc\8.0.32\tomcat-jdbc-8.0.32.jar;D:\LocalRepository\org\apache\tomcat\tomcat-juli\8.0.32\tomcat-juli-8.0.32.jar;D:\LocalRepository\org\springframework\spring-jdbc\4.2.5.RELEASE\spring-jdbc-4.2.5.RELEASE.jar;D:\LocalRepository\org\springframework\spring-tx\4.2.5.RELEASE\spring-tx-4.2.5.RELEASE.jar;D:\workspace_git_idea\luffi-master\luffi-common\target\classes;D:\LocalRepository\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;D:\LocalRepository\org\apache\zookeeper\zookeeper\3.4.8\zookeeper-3.4.8.jar;D:\LocalRepository\jline\jline\0.9.94\jline-0.9.94.jar;D:\LocalRepository\io\netty\netty\3.7.0.Final\netty-3.7.0.Final.jar;D:\LocalRepository\com\101tec\zkclient\0.9\zkclient-0.9.jar;D:\LocalRepository\log4j\log4j\1.2.15\log4j-1.2.15.jar;D:\LocalRepository\javax\mail\mail\1.4\mail-1.4.jar;D:\LocalRepository\com\alibaba\dubbo\2.8.4.lehi\dubbo-2.8.4.lehi.jar;D:\LocalRepository\org\springframework\spring-beans\4.2.4.RELEASE\spring-beans-4.2.4.RELEASE.jar;D:\LocalRepository\org\springframework\spring-core\4.2.4.RELEASE\spring-core-4.2.4.RELEASE.jar;D:\LocalRepository\org\springframework\spring-context\4.2.4.RELEASE\spring-context-4.2.4.RELEASE.jar;D:\LocalRepository\org\springframework\spring-aop\4.2.4.RELEASE\spring-aop-4.2.4.RELEASE.jar;D:\LocalRepository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\LocalRepository\org\springframework\spring-expression\4.2.4.RELEASE\spring-expression-4.2.4.RELEASE.jar;D:\LocalRepository\org\apache\logging\log4j\log4j-core\2.5\log4j-core-2.5.jar;D:\LocalRepository\org\apache\logging\log4j\log4j-api\2.5\log4j-api-2.5.jar;D:\LocalRepository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;D:\LocalRepository\org\springframework\spring-web\4.2.4.RELEASE\spring-web-4.2.4.RELEASE.jar;D:\LocalRepository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\LocalRepository\commons-pool\commons-pool\1.6\commons-pool-1.6.jar;D:\LocalRepository\org\apache\curator\curator-framework\2.5.0\curator-framework-2.5.0.jar;D:\LocalRepository\org\apache\curator\curator-client\2.5.0\curator-client-2.5.0.jar;D:\LocalRepository\com\google\guava\guava\16.0.1\guava-16.0.1.jar;D:\LocalRepository\org\springframework\boot\spring-boot-starter\1.3.6.RELEASE\spring-boot-starter-1.3.6.RELEASE.jar;D:\LocalRepository\org\springframework\boot\spring-boot\1.3.6.RELEASE\spring-boot-1.3.6.RELEASE.jar;D:\LocalRepository\org\springframework\boot\spring-boot-autoconfigure\1.3.6.RELEASE\spring-boot-autoconfigure-1.3.6.RELEASE.jar;D:\LocalRepository\org\yaml\snakeyaml\1.16\snakeyaml-1.16.jar;D:\LocalRepository\org\springframework\boot\spring-boot-starter-web\1.3.6.RELEASE\spring-boot-starter-web-1.3.6.RELEASE.jar;D:\LocalRepository\org\springframework\boot\spring-boot-starter-validation\1.3.6.RELEASE\spring-boot-starter-validation-1.3.6.RELEASE.jar;D:\LocalRepository\org\apache\tomcat\embed\tomcat-embed-el\8.0.36\tomcat-embed-el-8.0.36.jar;D:\LocalRepository\com\fasterxml\jackson\core\jackson-databind\2.6.7\jackson-databind-2.6.7.jar;D:\LocalRepository\com\fasterxml\jackson\core\jackson-annotations\2.6.0\jackson-annotations-2.6.0.jar;D:\LocalRepository\com\fasterxml\jackson\core\jackson-core\2.6.7\jackson-core-2.6.7.jar;D:\LocalRepository\org\springframework\spring-webmvc\4.2.7.RELEASE\spring-webmvc-4.2.7.RELEASE.jar;D:\LocalRepository\org\springframework\boot\spring-boot-starter-log4j2\1.3.6.RELEASE\spring-boot-starter-log4j2-1.3.6.RELEASE.jar;D:\LocalRepository\org\apache\logging\log4j\log4j-slf4j-impl\2.4.1\log4j-slf4j-impl-2.4.1.jar;D:\LocalRepository\org\slf4j\jcl-over-slf4j\1.7.21\jcl-over-slf4j-1.7.21.jar;D:\LocalRepository\org\slf4j\jul-to-slf4j\1.7.21\jul-to-slf4j-1.7.21.jar;D:\LocalRepository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar;D:\LocalRepository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;D:\LocalRepository\javax\el\javax.el-api\2.2.4\javax.el-api-2.2.4.jar;D:\LocalRepository\org\glassfish\web\javax.el\2.2.4\javax.el-2.2.4.jar;D:\LocalRepository\org\hibernate\hibernate-validator\5.2.4.Final\hibernate-validator-5.2.4.Final.jar;D:\LocalRepository\org\jboss\logging\jboss-logging\3.2.1.Final\jboss-logging-3.2.1.Final.jar;D:\LocalRepository\com\fasterxml\classmate\1.1.0\classmate-1.1.0.jar;D:\LocalRepository\org\jboss\resteasy\resteasy-jaxrs\3.0.9.Final\resteasy-jaxrs-3.0.9.Final.jar;D:\LocalRepository\org\jboss\resteasy\jaxrs-api\3.0.9.Final\jaxrs-api-3.0.9.Final.jar;D:\LocalRepository\org\jboss\spec\javax\annotation\jboss-annotations-api_1.1_spec\1.0.1.Final\jboss-annotations-api_1.1_spec-1.0.1.Final.jar;D:\LocalRepository\javax\activation\activation\1.1\activation-1.1.jar;D:\LocalRepository\net\jcip\jcip-annotations\1.0\jcip-annotations-1.0.jar;D:\LocalRepository\org\jboss\resteasy\resteasy-client\3.0.9.Final\resteasy-client-3.0.9.Final.jar;D:\LocalRepository\org\jboss\resteasy\resteasy-netty\3.0.14.Final\resteasy-netty-3.0.14.Final.jar;D:\LocalRepository\org\jboss\resteasy\resteasy-jdk-http\3.0.14.Final\resteasy-jdk-http-3.0.14.Final.jar;D:\LocalRepository\org\jboss\resteasy\resteasy-jackson-provider\3.0.14.Final\resteasy-jackson-provider-3.0.14.Final.jar;D:\LocalRepository\org\codehaus\jackson\jackson-core-asl\1.9.12\jackson-core-asl-1.9.12.jar;D:\LocalRepository\org\codehaus\jackson\jackson-mapper-asl\1.9.12\jackson-mapper-asl-1.9.12.jar;D:\LocalRepository\org\codehaus\jackson\jackson-jaxrs\1.9.12\jackson-jaxrs-1.9.12.jar;D:\LocalRepository\org\codehaus\jackson\jackson-xc\1.9.12\jackson-xc-1.9.12.jar;D:\LocalRepository\org\jboss\resteasy\resteasy-jaxb-provider\3.0.16.Final\resteasy-jaxb-provider-3.0.16.Final.jar;D:\LocalRepository\com\sun\xml\bind\jaxb-impl\2.2.7\jaxb-impl-2.2.7.jar;D:\LocalRepository\com\sun\xml\bind\jaxb-core\2.2.7\jaxb-core-2.2.7.jar;D:\LocalRepository\javax\xml\bind\jaxb-api\2.2.7\jaxb-api-2.2.7.jar;D:\LocalRepository\com\sun\istack\istack-commons-runtime\2.16\istack-commons-runtime-2.16.jar;D:\LocalRepository\com\sun\xml\fastinfoset\FastInfoset\1.2.12\FastInfoset-1.2.12.jar;D:\LocalRepository\javax\xml\bind\jsr173_api\1.0\jsr173_api-1.0.jar;D:\LocalRepository\org\apache\tomcat\embed\tomcat-embed-core\8.0.11\tomcat-embed-core-8.0.11.jar;D:\LocalRepository\org\apache\tomcat\embed\tomcat-embed-logging-juli\8.0.11\tomcat-embed-logging-juli-8.0.11.jar;D:\LocalRepository\junit\junit\4.12\junit-4.12.jar;D:\LocalRepository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\LocalRepository\org\springframework\spring-test\4.2.4.RELEASE\spring-test-4.2.4.RELEASE.jar;D:\LocalRepository\org\apache\thrift\libthrift\0.9.3\libthrift-0.9.3.jar;D:\LocalRepository\redis\clients\jedis\2.9.0\jedis-2.9.0.jar;D:\LocalRepository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;D:\LocalRepository\org\springframework\spring-context-support\4.2.4.RELEASE\spring-context-support-4.2.4.RELEASE.jar;D:\LocalRepository\org\quartz-scheduler\quartz\2.2.3\quartz-2.2.3.jar;D:\LocalRepository\c3p0\c3p0\0.9.1.1\c3p0-0.9.1.1.jar;D:\LocalRepository\com\typesafe\akka\akka-actor_2.11\2.4.16\akka-actor_2.11-2.4.16.jar;D:\LocalRepository\org\scala-lang\scala-library\2.11.8\scala-library-2.11.8.jar;D:\LocalRepository\com\typesafe\config\1.3.0\config-1.3.0.jar;D:\LocalRepository\org\scala-lang\modules\scala-java8-compat_2.11\0.7.0\scala-java8-compat_2.11-0.7.0.jar;D:\LocalRepository\com\typesafe\akka\akka-transactor_2.10\2.3.16\akka-transactor_2.10-2.3.16.jar;D:\LocalRepository\com\typesafe\akka\akka-actor_2.10\2.3.16\akka-actor_2.10-2.3.16.jar;D:\LocalRepository\org\scala-stm\scala-stm_2.10\0.7\scala-stm_2.10-0.7.jar;D:\LocalRepository\com\typesafe\akka\akka-testkit_2.11\2.4.16\akka-testkit_2.11-2.4.16.jar;D:\LocalRepository\com\typesafe\akka\akka-cluster_2.11\2.4.16\akka-cluster_2.11-2.4.16.jar;D:\LocalRepository\com\typesafe\akka\akka-remote_2.11\2.4.16\akka-remote_2.11-2.4.16.jar;D:\LocalRepository\com\typesafe\akka\akka-stream_2.11\2.4.16\akka-stream_2.11-2.4.16.jar;D:\LocalRepository\org\reactivestreams\reactive-streams\1.0.0\reactive-streams-1.0.0.jar;D:\LocalRepository\com\typesafe\ssl-config-core_2.11\0.2.1\ssl-config-core_2.11-0.2.1.jar;D:\LocalRepository\org\scala-lang\modules\scala-parser-combinators_2.11\1.0.4\scala-parser-combinators_2.11-1.0.4.jar;D:\LocalRepository\com\typesafe\akka\akka-protobuf_2.11\2.4.16\akka-protobuf_2.11-2.4.16.jar;D:\LocalRepository\org\uncommons\maths\uncommons-maths\1.2.2a\uncommons-maths-1.2.2a.jar;D:\LocalRepository\io\aeron\aeron-driver\1.0.4\aeron-driver-1.0.4.jar;D:\LocalRepository\io\aeron\aeron-client\1.0.4\aeron-client-1.0.4.jar;D:\LocalRepository\org\agrona\Agrona\0.9.0\Agrona-0.9.0.jar;D:\LocalRepository\org\projectlombok\lombok-maven-plugin\1.12.2.0\lombok-maven-plugin-1.12.2.0.jar;D:\LocalRepository\org\apache\maven\maven-plugin-api\3.0.4\maven-plugin-api-3.0.4.jar;D:\LocalRepository\org\apache\maven\maven-model\3.0.4\maven-model-3.0.4.jar;D:\LocalRepository\org\codehaus\plexus\plexus-utils\2.0.6\plexus-utils-2.0.6.jar;D:\LocalRepository\org\apache\maven\maven-artifact\3.0.4\maven-artifact-3.0.4.jar;D:\LocalRepository\org\sonatype\sisu\sisu-inject-plexus\2.3.0\sisu-inject-plexus-2.3.0.jar;D:\LocalRepository\org\codehaus\plexus\plexus-component-annotations\1.5.5\plexus-component-annotations-1.5.5.jar;D:\LocalRepository\org\codehaus\plexus\plexus-classworlds\2.4\plexus-classworlds-2.4.jar;D:\LocalRepository\org\sonatype\sisu\sisu-inject-bean\2.3.0\sisu-inject-bean-2.3.0.jar;D:\LocalRepository\org\sonatype\sisu\sisu-guice\3.1.0\sisu-guice-3.1.0-no_aop.jar;D:\LocalRepository\org\sonatype\sisu\sisu-guava\0.9.9\sisu-guava-0.9.9.jar;D:\LocalRepository\org\projectlombok\lombok\1.12.2\lombok-1.12.2.jar;D:\Program Files (x86)\ideaIU-2016.2.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain akka.stm.STMMain
companyCount = 100;employeeCount = 20
-----------------------
companyCount = 99;employeeCount = 21
-----------------------
companyCount = 97;employeeCount = 23
-----------------------
companyCount = 94;employeeCount = 26
-----------------------
companyCount = 90;employeeCount = 30
-----------------------
companyCount = 85;employeeCount = 35
-----------------------
companyCount = 79;employeeCount = 41
-----------------------
companyCount = 72;employeeCount = 48
-----------------------
companyCount = 64;employeeCount = 56
-----------------------
companyCount = 55;employeeCount = 65
-----------------------
companyCount = 45;employeeCount = 75
-----------------------
companyCount = 34;employeeCount = 86
-----------------------
companyCount = 22;employeeCount = 98
-----------------------
java.lang.RuntimeException: 余额不足!
    at akka.stm.CompanyActor.lambda$onReceive$0(CompanyActor.java:34)
    at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
	at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:50)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:43)
    at scala.concurrent.stm.ccstm.InTxnImpl.runBlock(InTxnImpl.scala:560)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAttempt(InTxnImpl.scala:516)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAtomicImpl(InTxnImpl.scala:387)
    at scala.concurrent.stm.ccstm.InTxnImpl.atomic(InTxnImpl.scala:248)
    at scala.concurrent.stm.ccstm.CCSTMExecutor.apply(CCSTMExecutor.scala:24)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl.atomic(CommitBarrierImpl.scala:43)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:141)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:157)
    at akka.stm.CompanyActor.onReceive(CompanyActor.java:33)
    at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:165)
	at akka.actor.Actor$class.aroundReceive(Actor.scala:496)
	at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:95)
	at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
	at akka.actor.ActorCell.invoke(ActorCell.scala:495)
	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
	at akka.dispatch.Mailbox.run(Mailbox.scala:224)
	at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
	at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
	at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
	at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
	at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
akka.transactor.CoordinatedTransactionException: Exception in coordinated atomic
	at akka.transactor.Coordinated.atomic(Coordinated.scala:144)
	at akka.transactor.Coordinated.atomic(Coordinated.scala:157)
	at akka.stm.EmployeeActor.onReceive(EmployeeActor.java:25)
	at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:165)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:496)
    at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:95)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
    at akka.actor.ActorCell.invoke(ActorCell.scala:495)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
    at akka.dispatch.Mailbox.run(Mailbox.scala:224)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.RuntimeException: 余额不足!
    at akka.stm.CompanyActor.lambda$onReceive$0(CompanyActor.java:34)
    at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
	at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:50)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:43)
    at scala.concurrent.stm.ccstm.InTxnImpl.runBlock(InTxnImpl.scala:560)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAttempt(InTxnImpl.scala:516)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAtomicImpl(InTxnImpl.scala:387)
    at scala.concurrent.stm.ccstm.InTxnImpl.atomic(InTxnImpl.scala:248)
    at scala.concurrent.stm.ccstm.CCSTMExecutor.apply(CCSTMExecutor.scala:24)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl.atomic(CommitBarrierImpl.scala:43)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:141)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:157)
    at akka.stm.CompanyActor.onReceive(CompanyActor.java:33)
    ... 12 more
companyCount = 9;employeeCount = 111
-----------------------
java.lang.RuntimeException: 余额不足!
companyCount = 9;employeeCount = 111
    at akka.stm.CompanyActor.lambda$onReceive$0(CompanyActor.java:34)
-----------------------
    at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
	at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:50)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:43)
    at scala.concurrent.stm.ccstm.InTxnImpl.runBlock(InTxnImpl.scala:560)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAttempt(InTxnImpl.scala:516)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAtomicImpl(InTxnImpl.scala:387)
    at scala.concurrent.stm.ccstm.InTxnImpl.atomic(InTxnImpl.scala:248)
    at scala.concurrent.stm.ccstm.CCSTMExecutor.apply(CCSTMExecutor.scala:24)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl.atomic(CommitBarrierImpl.scala:43)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:141)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:157)
    at akka.stm.CompanyActor.onReceive(CompanyActor.java:33)
    at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:165)
	at akka.actor.Actor$class.aroundReceive(Actor.scala:496)
	at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:95)
	at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
	at akka.actor.ActorCell.invoke(ActorCell.scala:495)
	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
	at akka.dispatch.Mailbox.run(Mailbox.scala:224)
	at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
	at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
	at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
	at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
	at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
akka.transactor.CoordinatedTransactionException: Exception in coordinated atomic
	at akka.transactor.Coordinated.atomic(Coordinated.scala:144)
	at akka.transactor.Coordinated.atomic(Coordinated.scala:157)
	at akka.stm.EmployeeActor.onReceive(EmployeeActor.java:25)
	at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:165)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:496)
    at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:95)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
    at akka.actor.ActorCell.invoke(ActorCell.scala:495)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
    at akka.dispatch.Mailbox.run(Mailbox.scala:224)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.RuntimeException: 余额不足!
    at akka.stm.CompanyActor.lambda$onReceive$0(CompanyActor.java:34)
    at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
	at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:50)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:43)
    at scala.concurrent.stm.ccstm.InTxnImpl.runBlock(InTxnImpl.scala:560)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAttempt(InTxnImpl.scala:516)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAtomicImpl(InTxnImpl.scala:387)
    at scala.concurrent.stm.ccstm.InTxnImpl.atomic(InTxnImpl.scala:248)
    at scala.concurrent.stm.ccstm.CCSTMExecutor.apply(CCSTMExecutor.scala:24)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl.atomic(CommitBarrierImpl.scala:43)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:141)
    at akka.transactor.Coordinated.atomic(Coordinated.scala:157)
    at akka.stm.CompanyActor.onReceive(CompanyActor.java:33)
    ... 12 more
companyCount = 9;employeeCount = 111
-----------------------
java.lang.RuntimeException: 余额不足!
    at akka.stm.CompanyActor.lambda$onReceive$0(CompanyActor.java:34)
    at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
	at akka.transactor.Coordinated$$anonfun$atomic$1.apply(Coordinated.scala:157)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:50)
    at scala.concurrent.stm.ccstm.CommitBarrierImpl$MemberImpl$$anonfun$atomic$1.apply(CommitBarrierImpl.scala:43)
    at scala.concurrent.stm.ccstm.InTxnImpl.runBlock(InTxnImpl.scala:560)
    at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAttempt(InTxnImpl.scala:516)

注意:需要引入transactor的maven配置:

<dependency>
    <groupId>com.typesafe.akkagroupId>
    <artifactId>akka-actor_2.11artifactId>
    <version>2.4.16version>
dependency>
<dependency>
    <groupId>com.typesafe.akkagroupId>
    <artifactId>akka-agent_2.11artifactId>
    <version>2.4.16version>
dependency>

<dependency>
    <groupId>com.typesafe.akkagroupId>
    <artifactId>akka-transactor_2.10artifactId>
    <version>2.3.16version>
dependency>

参考资料

  • 书籍《java高并发程序设计》
  • AKKA官方文档

你可能感兴趣的:(分布式+高并发,akka学习教程)