我想让akka使用spring应用程序.这是一款完全符合akka模型的搜索应用程序.关于此集成的大多数在线示例和类型安全示例都讨论了使用akka扩展来注入spring应用程序上下文.但是,它们都使用ActorSystem.actorOf()方法来创建已知昂贵操作的actor.

ActorSystem system = ctx.getBean(ActorSystem.class);

system.actorOf(.....)

我想使用actor来处理每个通过身份验证的Web请求.使用上面的代码,我将最终为每个不理想的请求创建root actor.

任何指针都会非常感激.

解决方法:

下面没有回答原始问题,这个问题是关于减少通常在请求进入Web应用程序时需要创建的新actor的数量.

要使用Akka路由器,它可以像下面的代码一样简单:

getContext().actorOf(SpringExtProvider.get(getContext().system()).props("AnotherActor.").withRouter(new RoundRobinPool(100)), "another");

Akka文档提供了有关配置的更多细节,但值得一看http://doc.akka.io/docs/akka/snapshot/java/routing.html.最好通过Akka配置文件定义它们的行为,而不是硬编码到应用程序中.你可以这样称呼它:

getContext().actorOf(SpringExtProvider.get(getContext().system()).props("AnotherActor.").withRouter(new FromConfig()), "another");

..并在application.conf文件中定义路由器的类型和行为.

如果您还没有考虑过它,那么还有必要查看如何使您的Web重新请求异步.一种方法是使用Spring的DeferredResult并将其实例传递给您的actor,并在完成搜索请求时设置结果.

– 更新20 / 11–

我认为为什么演员选择不适合你是因为你试图使用bean名称,而不是演员名称作为演员选择的pacth.在创建路由器时,您没有指定一个actor名称,所以Akka会在内部给它一个名字,比如“$a”.

为了说明,如果你创建你的演员:

actorSystem.actorOf(this.get(actorSystem).props(applicationContext, "bean_name"), "actorName");

然后你应该能够执行一个演员选择:

actorSystem.actorSelection("actorName");

或者创建上面的路由器actor一次,然后在每个对Spring MVC Web服务的请求中重用它,你可以在Spring @Configuration类中创建它,并将ActorRef作为bean公开,这样你就可以注入你的Spring @Controller类.以下是我创建的内存的快速示例,因此请确保它已经过测试/编译等.

@Configuration

public class Config {

@Autowired

private ActorSystem actorSystem;

@Bean(name = "routerActorRef")

public ActorRef routerActorRef() {

getContext().actorOf(SpringExtProvider.get(actorSystem).props("AnotherActor").withRouter(new RoundRobinPool(100)), "another");

}

}

然后你可以通过写下这样的东西将它注入另一个Spring bean:

@Autowired

@Qualifier("routerActorRef")

private ActorRef routerActorRef;

应该注意的是,这对于顶级演员来说才真正可行,对于较低级别的演员来说这是可能的,但是有效管理会变得非常棘手.

– 原始答案 –

您链接到的Main方法中的示例显示了如何创建初始顶级actor,该actor将由基于系统的“user”actor监督.这是一个非常简单的示例,演示如何创建一个名为CountingActor的Spring托管actor,其中注入了一个名为CountingService的Spring托管bean.

为了进一步采用这个例子,你可以定义另一个类似于CountingActor的actor,例如

@Named("AnotherActor")

@Scope("prototype")

class AnotherActor extends UntypedActor {

// the service that will be automatically injected

final AnotherService anotherService;

@Inject

public AnotherActor(@Named("AnotherService") AnotherService anotherService) {

this.anotherService = anotherService;

}

@Override

public void onReceive(Object message) throws Exception {

if (message == "doSomething") {

anotherService.doSomething();

} else {

unhandled(message);

}

}

}

我假设有另一个名为AnotherService的Spring服务bean,它有一个方法doSomething(),它将在创建AnotherActor时注入.

然后在CountingActor中你可以像这样创建AnotherActor:

@Named("CountingActor")

@Scope("prototype")

class CountingActor extends UntypedActor {

public static class Count {}

public static class Get {}

// the service that will be automatically injected

final CountingService countingService;

@Inject

public CountingActor(@Named("CountingService") CountingService countingService) {

this.countingService = countingService;

}

private int count = 0;

@Override

public void onReceive(Object message) throws Exception {

if (message instanceof Count) {

count = countingService.increment(count);

// Create AnotherActor here as a child of CountingActor by using the CountingActor's context

ActorRef anotherActor = getContext().actorOf(SpringExtProvider.get(system).props("AnotherActor"), "another");

anotherActor.tell("doSomething", getSelf());

} else if (message instanceof Get) {

getSender().tell(count, getSelf());

} else {

unhandled(message);

}

}

}

这里的actor创建和主要的主要区别是使用getContext().actorOf(…)而不是system.actorOf(…).使用getContext()会将新actor创建为CounterActor的子级,而不是顶级“user”actor.

标签:java,spring,akka

来源: https://codeday.me/bug/20190624/1276640.html

akka java_java – Akka和Spring集成相关推荐

  1. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  2. windows api中文文档_Web服务开发:Spring集成Swagger,3步自动生成API文档

    目录: 1,Spring Boot集成Swagger 2,Swagger接口文档页面 3,常见问题和解决方法 在Sping开发REST接口服务时,API文档是不可缺少的一个重要部分.Swagger框架 ...

  3. Spring集成spymemcached

    Spring集成spymemcached Memcached的安装部署我就不介绍了! 首先下载spymemcached,下载地址: jar:https://spymemcached.googlecod ...

  4. Liferay7 BPM门户开发之5: Activiti和Spring集成

    参考文档: https://github.com/jbarrez/spring-boot-with-activiti-example https://github.com/sxyx2008/sprin ...

  5. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  6. spring集成xmemcached

    2019独角兽企业重金招聘Python工程师标准>>> spring集成xmemcached <dependency><groupId>com.googlec ...

  7. Spring集成线程池

    自己在程序中手动New很容易造成线程滥用,创建线程也是比较消耗资源的操作,所以建议如果有此需求,将线程池统一交给Spring框架进行管理. 如下: <!--Spring 集成线程池,不允许自己开 ...

  8. MyBitis(iBitis)系列随笔之六:mybitis与spring集成

    目前Spring官方还没有出整合Mybatis的特性,但是mybitis比较给力,开发了一个mybatis-spring插件,达到与Spring的完美整合目的. 在与Spring集成前,一方面我们需要 ...

  9. JBPM4.4_jBPM4.4应用(与Spring集成自行控制事务等)

    1. jBPM4.4应用 1.1. 与Spring集成(jBPM4.4 Developers Guide, Chapter 17. Spring Integration) 1.1.1. 在jbpm.c ...

最新文章

  1. BT:胞外酶-化腐朽为神奇的催化剂
  2. 《Raspberry Pi用户指南》——导读
  3. Hadoop教程(一):简介、大数据解决方案、介绍快速入门
  4. Ubuntu镜像国内下载地址
  5. 深度学习算法和机器学习算法_啊哈! 4种流行的机器学习算法的片刻
  6. linux 磁盘uuid获取
  7. 面向对象编程起步——神来之笔
  8. Android教程 -07 Activity的任务栈和启动模式
  9. MySQL 中的共享表空间与独立表空间,用哪个好呢?
  10. BZOJ4337 : BJOI2015 树的同构
  11. 使用.net开发手机管理软件 (九) 短信部分——PDU简介及其格式
  12. swfupload 上传报 security error # 2049 (security) 安全错误问题
  13. weblogic安装升级配置
  14. 计算机考研C语言题库
  15. 无延时/无延迟视频直播实例效果案例
  16. python生成excel文件二维码_Python实现读取Excel表内容批量生成二维码
  17. hdu 5887 herb gathering 搜索剪枝
  18. 无状态编程, lambda 表达式中传入的局部变量,为什么需要是不可变的(final)
  19. Python-Django毕业设计影城在线售票及票房数据分析系统(程序+Lw)
  20. 2019 ICPC 南京 F题 Paper Grading

热门文章

  1. php7++linux安装,安装PHP5和PHP7
  2. 【git系列】切换分支相关命令
  3. Java集合系列---List源码解析(ArrayList和LinkedList的区别)
  4. Android2017 这些技术 —— 你都了解过吗
  5. Java项目--俄罗斯方块
  6. 《精通自动化测试框架设计》—第2章 2.6节使用数据库
  7. 【ANDROID游戏开发之六】在SURFACEVIEW中添加系统控件,并且相互交互数据!
  8. structs2 result type介绍
  9. 创建服务factory和service方法的区别
  10. Draw Circle 沿着圆运动~