Akka 配置Dispatcher(一)

Akka 配置Dispatcher(一)

akka默认加载classpath下application.conf配置文件,在application.conf配置文件中配置Dispatcher

如下定义一个application.conf配置文件,

akka {
  loglevel = INFO
}

my-dispatcher {
  # Dispatcher is the name of the event-based dispatcher
  type = Dispatcher
  # What kind of ExecutionService to use
  executor = "fork-join-executor"
  # Configuration for the fork join pool
  fork-join-executor {
    # Min number of threads to cap factor-based parallelism number to
    parallelism-min = 2
    # Parallelism (threads) . . . ceil(available processors * factor)
    parallelism-factor = 2.0
    # Max number of threads to cap factor-based parallelism number to
    parallelism-max = 10
  }
  # Throughput defines the maximum number of messages to be
  # processed per actor before the thread jumps to the next actor.
  # Set to 1 for as fair as possible.
  throughput = 100
}

如何使用这个Dispatcher的配置?

首先定义一个Actor,如下,

package com.usoft6;

import akka.actor.UntypedActor;

/**
 * Created by liyanxin on 2015/1/12.
 */
public class MyActor extends UntypedActor {
    private int x;
    private int y;

    public MyActor(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void onReceive(Object message) throws Exception {
        System.out.println("接收到的消息=" + message);
        int result = x + y;
        this.getSender().tell(result, this.getSelf());
        this.getContext().stop(this.getSelf());
    }
}

DispatcherDemo1.java

package com.usoft6;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

/**
 * Created by liyanxin on 2015/1/13.
 */
public class DispatcherDemo1 {

    public static void main(String args[]) throws Exception {
        ActorSystem system = ActorSystem.create("myActorSystem");
        ActorRef myActor = system.actorOf(Props.create(MyActor.class, 54, 65).
                withDispatcher("my-dispatcher"), "myactor");

        Timeout timeout = new Timeout(Duration.create(5, "seconds"));
        Future<Object> future = Patterns.ask(myActor, "are you ready?", timeout);

        // This will cause the current thread to block and wait for the UntypedActor to ‘complete’
        // the Future with it’s reply.
        // 在这里会阻塞到 Await.result 方法上,但这会导致性能的损失。
        Integer result = (Integer) Await.result(future, timeout.duration());
        System.out.println(result);
    }
}

这块代码指定Dispatcher的配置,

 ActorRef myActor = system.actorOf(Props.create(MyActor.class, 54, 65).
                withDispatcher("my-dispatcher"), "myactor");

=============END=============

转载于:https://my.oschina.net/xinxingegeya/blog/367343

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值