SpringBoot下Akka的简单使用
SpringBoot下Akka的简单使用
Akka框架实现一个异步消息传输,通过定义演员来处理业务逻辑。
首先引入依赖
<!-- akka -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.13</artifactId>
<version>2.6.0</version>
</dependency>
然后编写一个不带构造函数的演员——Actor类,如下:
import akka.actor.AbstractActor;
public class ActorNormal extends AbstractActor {
//处理消息
@Override
public Receive createReceive() {
//处理一个具体类型的消息,比如是字符串类型的消息
Receive build = receiveBuilder().match(String.class,(msg)-> {
System.out.println(msg);
sender().tell("response", self());
}).match(Integer.class,(msg)-> {
System.out.println(msg+"1");
}).build();
return build;
}
}
ActorNormal重写createReceive函数,然后使用receiveBuilder构造一个接收器Receive,然后使用Receive的Match函数,对不同类型的请求进行分别处理;在处理内部可以使用 sender().tell发送返回值给请求者。
然后编写一个带构造函数的演员——ActorStruct。
import akka.actor.AbstractActor;
import com.example.dynamicdb.dbmodel.User;
/**
* 保护构造函数的演员
*/
public class ActorStruct extends AbstractActor {
private final User user;
public ActorStruct(User userModel){
this.user = userModel;
}
//处理消息
@Override
public Receive createReceive() {
//处理一个具体类型的消息,比如是字符串类型的消息
Receive build = receiveBuilder().match(String.class,(msg)-> {
System.out.println(msg);
sender().tell("我是ActorStruct返回结果", self());
}).match(Integer.class,(msg)-> {
System.out.println(msg+"1");
}).build();
return build;
}
}
然后编写一个Controller——AkkaController,测试AKKA,如下:
package com.example.dynamicdb.controller;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;
import com.example.dynamicdb.akka.ActorNormal;
import com.example.dynamicdb.akka.ActorStruct;
import com.example.dynamicdb.dbmodel.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit;
@RestController
public class AkkaController {
@GetMapping(value = "/Akka/AkkaSendString")
@ResponseBody
@ApiOperation(value = "Akka使用tell发送字符串", notes = "发送数值", httpMethod = "Get")
public void AkkaSendString() {
//创建所有管理actor的系统管理对象
ActorSystem actorSystem = ActorSystem.create();
//通过这个系统管理对象创建actor,并返回当前actor的地址,可以理解成现实生活中用户的一个邮箱地址
//使用actorSystem.actorOf定义一个名为actorNormal的ActorRef
ActorRef actor = actorSystem.actorOf(Props.create(ActorNormal.class), "actorNormal");
//发送消息Object msg(发送消息的内容,任何类型的数据), final ActorRef sender(表示没有发送者(其实是一个叫做deadLetters的Actor))
actor.tell("kiba", ActorRef.noSender());
}
@GetMapping(value = "/Akka/AkkaSendInt")
@ResponseBody
@ApiOperation(value = "Akka使用tell发送数值", notes = "发送数值", httpMethod = "Get")
public void AkkaSendInt() {
ActorSystem actorSystem = ActorSystem.create();
ActorRef actor = actorSystem.actorOf(Props.create(ActorNormal.class), "actorNormal");
actor.tell(518, ActorRef.noSender());//发送数值
}
@GetMapping(value = "/Akka/AkkaAsk")
@ResponseBody
@ApiOperation(value = "Akka使用Ask请求", notes = "Ask请求可以接受演员的返回值", httpMethod = "Get")
public void AkkaAsk() {
ActorSystem actorSystem = ActorSystem.create();
ActorRef actor = actorSystem.actorOf(Props.create(ActorNormal.class), "actorNormal");
Timeout timeout = new Timeout(Duration.create(2, TimeUnit.SECONDS));
Future<Object> future = Patterns.ask(actor, "我是接收游戏返回值", timeout);
try {
Object obj = Await.result(future, timeout.duration());
String reply = obj.toString();
System.out.println("回复的消息: " + reply);//返回值获取不到
} catch (Exception e) {
e.printStackTrace();
}
}
@GetMapping(value = "/Akka/AkkaAskStruct")
@ResponseBody
@ApiOperation(value = "Akka使用Ask请求带构造函数的演员", notes = "带构造函数的演员", httpMethod = "Get")
public void AkkaAskStruct() {
ActorSystem actorSystem = ActorSystem.create();
ActorRef actor = actorSystem.actorOf(Props.create(ActorStruct.class,new User(1,"kiba")), "actorNormal");
Timeout timeout = new Timeout(Duration.create(2, TimeUnit.SECONDS));
Future<Object> future = Patterns.ask(actor, "我是带构造函数的演员接收游戏返回值", timeout);
try {
Object obj = Await.result(future, timeout.duration());
String reply = obj.toString();
System.out.println("回复的消息: " + reply);//返回值获取不到
} catch (Exception e) {
e.printStackTrace();
}
}
}
----------------------------------------------------------------------------------------------------
注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!
https://www.cnblogs.com/kiba/p/17494029.html

SpringBoot下Akka的简单使用的更多相关文章
- Java结合SpringBoot拦截器实现简单的登录认证模块
Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...
- SpringSecurity在Springboot下使用的初步体验
SpringSecurity曾经在十年前非常火热,只要是做权限系统,当时几乎非用它不可,记得是在XML文件里一堆的配置.曾几何时,Shiro冒了出来,以其简洁和轻量的风格慢慢地捕获了众多码农的心,从此 ...
- (04) springboot 下的springMVC和jsp和mybatis
1. springboot 和springmvc下的注解完全一样(新增了一些有用的) 常用的注解如下: @Controller @RestController= @Controller + @Resp ...
- SpringBoot 下 mybatis 的缓存
背景: 说起 mybatis,作为 Java 程序员应该是无人不知,它是常用的数据库访问框架.与 Spring 和 Struts 组成了 Java Web 开发的三剑客--- SSM.当然随着 Spr ...
- springBoot整合MyBatise及简单应用
springBoot整合MyBatise及简单应用 我采用的是 工具IDEA 框架是springBoot+maven+Mybatise 第一步: pom.xml 引入相关jar包 <?xml v ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
- 使用 SpringBoot+Dubbo 搭建一个简单分布式服务
实战之前,先来看几个重要的概念 开始实战之前,我们先来简单的了解一下这样几个概念:Dubbo.RPC.分布式.由于本文的目的是带大家使用SpringBoot+Dubbo 搭建一个简单的分布式服务,所以 ...
- 手撸一个SpringBoot的Starter,简单易上手
前言:今天介绍一SpringBoot的Starter,并手写一个自己的Starter,在SpringBoot项目中,有各种的Starter提供给开发者使用,Starter则提供各种API,这样使开发S ...
- vue springboot利用easypoi实现简单导出
vue springboot利用easypoi实现简单导出 前言 一.easypoi是什么? 二.使用步骤 1.传送门 2.前端vue 3.后端springboot 3.1编写实体类(我这里是dto, ...
- Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看
一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...
随机推荐
- [转帖]自动化配置SSH免密登录和取消SSH免密配置脚本
1. 前文 搭建了一套有多台主机的局域网环境,不完全考虑安全性的情况下,为方便管理局域网内主机,配置SSH免密登录,因主机较多,前阵子针对配置ssh免密和取消ssh免密功能单独写了脚本来自动化批量部署 ...
- [转帖]cx_Oracle.DatabaseError: ORA-28040
背景: python第三方库cx-Oracle连接Oracle数据库报错 ORA-28040 cx_Oracle.DatabaseError: ORA-28040: No matching authe ...
- 使用rpm打包nacos然后部署为systemd服务开机自动启动的方法
背景 Nacos是阿里开源的服务注册组件,能够简单的实现微服务的注册与发现机制. 但是官方并没有提供 sytemd的服务脚本, 也没有提供rpm包的方式. 公司里面使用 nacos的场景越来越多, 部 ...
- rust入坑指南之ownership
作者:京东零售 王梦津 I. 前言 Rust,不少程序员的白月光,这里我们简单罗列一些大牛的评价. Linus Torvalds:Linux内核的创始人,对Rust的评价是:"Rust的主要 ...
- 每日一库:使用Viper处理Go应用程序的配置
在开发Go应用程序时,处理配置是一个常见的需求.配置可能来自于配置文件.环境变量.命令行参数等等.Viper是一个强大的库,可以帮助我们处理这些配置. 什么是Viper? Viper是一个应用程序配置 ...
- Map结构映射,避免每一个字段赋值
var query1 = (from fore in forecastShippingDate join ship in shipOutOfStock on fore.Id equals ship.F ...
- 在K8S中,deploy升级过程包括什么?
在Kubernetes (K8S) 中,Deployment的升级过程主要包括以下几个步骤: 更新Deployment配置: 当需要对应用程序进行升级时,通常会更新Deployment的YAML配置文 ...
- 新来的一个同事,把SpringBoot参数校验玩的那叫一个优雅
介绍 在开发现代应用程序时,数据验证是确保用户输入的正确性和应用程序数据完整性的关键方面.Spring Boot 提供了强大的数据验证机制,使开发者能够轻松地执行验证操作.本文将深入介绍 Spring ...
- 英伟达系列显卡大解析B100、H200、L40S、A100、A800、H100、H800、V100如何选择,含架构技术和性能对比带你解决疑惑
英伟达系列显卡大解析B100.H200.L40S.A100.A800.H100.H800.V100如何选择,含架构技术和性能对比带你解决疑惑 近期,AIGC领域呈现出一片繁荣景象,其背后离不开强大算力 ...
- 3.1 C++ STL 双向队列容器
双向队列容器(Deque)是C++ STL中的一种数据结构,是一种双端队列,允许在容器的两端进行快速插入和删除操作,可以看作是一种动态数组的扩展,支持随机访问,同时提供了高效的在队列头尾插入和删除元素 ...