akka入门-有类型的Actor

Akka 中的有类型 Actor 是 Active Objects 模式的一种实现. Smalltalk诞生之时,就已经缺省地将方法调用从同步操作发为异步派发。

有类型 Actor 由两 “部分”组成, 一个公开的接口和一个实现。对普通actor来说,你拥有一个外部API (公开接口的实例) 来将方法调用异步地委托给其实现的私有实例。

有类型Actor相对于普通Actor的优势在于有类型Actor拥有静态的契约, 你不需要定义你自己的消息, 它的劣势在于对你能做什么和不能做什么进行了一些限制,例如你不能使用 become/unbecome。

有类型Actor是使用 JDK Proxies 实现的,JDK Proxies提供了非常简单的api来拦截方法调用。

1.创建接口

import scala.concurrent.Future;
import akka.japi.Option;

/**
 * @author lcq
 *
 */
public interface Squarer {
  void squareDontCare( int i ); // 不关心的处理逻辑 fire-and-forget like ActorRef.tell

  Future<Integer> square(int i); // non-blocking send-request-reply like ActorRef.ask

  Option<Integer> squareNowPlease( int i );// blocking send-request-reply

  int squareNow( int i ); // blocking send-request-reply

  // Any other type of value -->send-request-reply
}
2.实现类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import scala.concurrent.Future;
import akka.dispatch.Futures;
import akka.japi.Option;

public class SquarerImpl implements Squarer {
  public static final Logger log = LoggerFactory.getLogger(SquarerImpl. class);
  private String name;

  public SquarerImpl() {
    this. name = "default";
  }

  public SquarerImpl(String name) {
    this. name = name;
  }


  public void squareDontCare(int i) {
    try {
      for ( int j = 0; j < 3; j++) {
        Thread. sleep(1000);
        log.info("squareDontCare is call " + j );
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    int sq = i * i; // Nobody cares :(
  }

  public Future<Integer> square( int i ) {
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return Futures. successful(Integer.valueOf (i * i ));
  }

  public Option<Integer> squareNowPlease( int i ) {
    return Option. some(i * i);
  }

  public int squareNow(int i) {
    return i * i;
  }
}

3.测试类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
import akka.actor.ActorSystem;
import akka.actor.TypedActor;
import akka.actor.TypedProps;
import akka.dispatch.OnComplete;
import akka.japi.Option;

/**
 *
 * @author lcq
 *
 */
public class System {
  public static final Logger log = LoggerFactory.getLogger(System.class);

  public static void main(String... args) throws Exception {

    final ActorSystem actorSystem = ActorSystem.create("actor-system");

    Thread.sleep(2000);

    // 阻塞方式调用
    Squarer mySquarer = TypedActor.get(actorSystem).typedActorOf( new TypedProps<SquarerImpl>(Squarer.class , SquarerImpl.class));
    int sqNowValue = mySquarer.squareNow(5);
    log.info( "sqNowValue" + sqNowValue );

    // 非阻塞方式调用
    Future<Integer> fu = mySquarer.square(6);
    // ExecutionContext类似于 java.util.concurrent.Executor
    final ExecutionContext ec = actorSystem.dispatcher();
    // Future的onComplete, onResult, 或 onTimeout 方法可以用来注册一个回调,以便在Future完成时得到通知。从而提供一种避免阻塞的方法。
    fu.onComplete( new OnComplete<Integer>() {
      @Override
      public void onComplete(Throwable failure , Integer result) throws Throwable {
        if (failure != null) {
          // We got a failure, handle it here
        } else {
          log.info("square result:" + result );
        }
      }
    }, ec);
    log.info( "22222222");

    // 阻塞方式调用
    Option<Integer> op = mySquarer.squareNowPlease(7);
    log.info( "squareNowPlease result : " + op .get());

    log.info( "33333333");
    // 不关心的处理,方法会在另一个线程中异步地调用
    mySquarer.squareDontCare(2);
    log.info( "44444444");
    Thread.sleep(2000);

    log.debug( "Actor System Shutdown Starting..." );

    actorSystem.shutdown();
  }
}
4.输出结果

20:53:31.550 [main] INFO  com.center.akka.typedActors.System - sqNowValue25
20:53:31.554 [main] INFO  com.center.akka.typedActors.System - 22222222
20:53:33.561 [actor-system-akka.actor.default-dispatcher-4] INFO  com.center.akka.typedActors.System - square result:36
20:53:33.562 [main] INFO  com.center.akka.typedActors.System - squareNowPlease result : 49
20:53:33.562 [main] INFO  com.center.akka.typedActors.System - 33333333
20:53:33.562 [main] INFO  com.center.akka.typedActors.System - 44444444
20:53:34.562 [actor-system-akka.actor.default-dispatcher-2] INFO  c.c.akka.typedActors.SquarerImpl - squareDontCare is call 0
20:53:35.562 [actor-system-akka.actor.default-dispatcher-2] INFO  c.c.akka.typedActors.SquarerImpl - squareDontCare is call 1
20:53:35.562 [main] DEBUG com.center.akka.typedActors.System - Actor System Shutdown Starting...
20:53:36.562 [actor-system-akka.actor.default-dispatcher-2] INFO  c.c.akka.typedActors.SquarerImpl - squareDontCare is call 2




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值