- 浏览: 2486264 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Play framework with Akka and WS
1 Set Up Akka in Playframework
In my controller which I believe it is single, I set up the AKKA system.
package controllers
import actors._
import actors.jobs.{JobServiceActor, JobParseActor}
import actors.resumes.{ResumeServiceActor, ResumeParseActor, ResumeAttachActor}
import akka.actor.Props
import com.sillycat.dao.{IpplyJobDAO, DAO}
import com.sillycat.util.IncludeLogger
import com.wordnik.swagger.annotations._
import models.ScanTriggerRequest
import play.api.libs.concurrent.Akka
import play.api.libs.json.{JsError, Json}
import play.api.mvc.{BodyParsers, Action, Controller}
import play.api.Play.current
import akka.contrib.throttle.TimerBasedThrottler
import akka.contrib.throttle.Throttler._
import scala.concurrent.duration._
import com.wordnik.swagger.annotations._
object ScanTriggerController extends Controller with IncludeLogger with IncludeWSConfig{
implicit val scanTriggerWrites = Json.writes[ScanTriggerRequest]
implicit val scanTriggerReads = Json.reads[ScanTriggerRequest]
Akka.system.actorOf(JobParseActor.props, name = "job-parse-actor")
Akka.system.actorOf(JobServiceActor.props, name = "job-service-actor")
Akka.system.actorOf(ResumeAttachActor.props, name = "resume-attach-actor")
Akka.system.actorOf(ResumeParseActor.props, name = "resume-parse-actor")
Akka.system.actorOf(ResumeServiceActor.props, name = "resume-service-actor")
Akka.system.actorOf(TriggerActor.props, name = "trigger-actor")
val contextIOActor = Akka.system.actorOf(ContextIOActor.props, name = "contextio-actor")
val contextIOThrottler = Akka.system.actorOf(Props(classOf[TimerBasedThrottler], contextio_throttle msgsPer 60.second), name = "conetxt-io-throttler")
contextIOThrottler ! SetTarget(Some(contextIOActor))
def trigger = Action(BodyParsers.parse.json) { request =>
val b = request.body.validate[ScanTriggerRequest]
logger.debug("Processing request with param = " + request.body)
b.fold(
errors => {
BadRequest(Json.obj("status" -> "OK", "message" -> JsError.toJson(errors)))
},
trigger => {
//TODO more validation on the params
logger.debug("Param validation success, param = " + trigger)
val triggerActor = Akka.system.actorSelection("/user/trigger-actor")
logger.debug("Prepare the trigger action = " + triggerActor + ", and fire message!")
triggerActor ! trigger
Ok(Json.obj("status" -> "OK"))
}
)
}
}
In any other actors or any other controllers, we can do as follow:
val jobServiceActor = context.actorSelection("/user/job-service-actor")
jobServiceActor ! JobContentPersistMessage(msg.jobId, jobCity, msg.emailContent)
In my case I have one controller to trigger my actions, maybe in the future I need to set up the AKKA system in global class or some where.
2 WS to call other Micro Service
Here is how I call other micro service
import akka.actor.{Actor, Props}
import com.sillycat.dao.{DAO, IpplyJobDAO, ResumeDAO}
import com.sillycat.util.IncludeLogger
import models.messages.jobs.{JobPersistMessage, JobScanMessage, JobContentParseMessage, JobContentScanMessage}
import models.messages.resumes._
import models.{ResumeScanResponse, ResumeScanAttachmentResponse, JobContentResponse, JobScanResponse}
import play.api.libs.json.Json
import play.api.libs.ws.{WS, WSResponse}
import utils.IncludeRegex
import play.api.Play.current
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
…snip...
case msg:JobContentScanMessage => {
logger.debug("receiving the message JobContentScanMessage" + msg)
//context.io fetch job content, actually it is email content, I need the city and location info
val future:Future[WSResponse] = WS.url(contextio_host + "/jobs/" + msg.accountCode +"/" + msg.messageCode ).get()
//scan jobs success
future onSuccess {
case response if response.status == 200 => {
logger.trace("Scanning job content = " + response.json)
response.json.asOpt[JobContentResponse].map { job =>
logger.trace("Scanning result job content = " + job)
val jobContentParseMessage = JobContentParseMessage(msg.jobId, job.content)
logger.debug("fire the message to job-parse-actor to parse email content.")
jobParseActor ! jobContentParseMessage
}
}
case response => {
//handling error
logger.error("Error handle with status = " + response.status + " message = " + response.body)
}
}
//scan jobs fail
future onFailure {
case t => logger.error("An error has occured: " + t.getMessage)
}
}
For post
val request_data = Json.obj(
"provider" -> "CRAIGSLIST",
"accountCode" -> msg.accountCode,
"dateBefore" -> msg.dateBefore.toString(default_date_time_format),
"dateAfter" -> msg.dateAfter.toString(default_date_time_format),
"limit" -> msg.limit,
"offset" -> msg.offset
)
logger.debug("Send json request parameter = " + request_data)
val future:Future[WSResponse] = WS.url(contextio_host + "/jobs/scan").post(request_data)
References:
https://www.playframework.com/documentation/2.4.3/ScalaWS
https://www.playframework.com/documentation/2.4.3/ScalaAkka
akka system
http://sillycat.iteye.com/blog/1767866
http://sillycat.iteye.com/blog/1768625
http://sillycat.iteye.com/blog/1768626
http://sillycat.iteye.com/blog/2099267
http://sillycat.iteye.com/blog/2100232
http://sillycat.iteye.com/blog/2102694
http://sillycat.iteye.com/blog/2175999
1 Set Up Akka in Playframework
In my controller which I believe it is single, I set up the AKKA system.
package controllers
import actors._
import actors.jobs.{JobServiceActor, JobParseActor}
import actors.resumes.{ResumeServiceActor, ResumeParseActor, ResumeAttachActor}
import akka.actor.Props
import com.sillycat.dao.{IpplyJobDAO, DAO}
import com.sillycat.util.IncludeLogger
import com.wordnik.swagger.annotations._
import models.ScanTriggerRequest
import play.api.libs.concurrent.Akka
import play.api.libs.json.{JsError, Json}
import play.api.mvc.{BodyParsers, Action, Controller}
import play.api.Play.current
import akka.contrib.throttle.TimerBasedThrottler
import akka.contrib.throttle.Throttler._
import scala.concurrent.duration._
import com.wordnik.swagger.annotations._
object ScanTriggerController extends Controller with IncludeLogger with IncludeWSConfig{
implicit val scanTriggerWrites = Json.writes[ScanTriggerRequest]
implicit val scanTriggerReads = Json.reads[ScanTriggerRequest]
Akka.system.actorOf(JobParseActor.props, name = "job-parse-actor")
Akka.system.actorOf(JobServiceActor.props, name = "job-service-actor")
Akka.system.actorOf(ResumeAttachActor.props, name = "resume-attach-actor")
Akka.system.actorOf(ResumeParseActor.props, name = "resume-parse-actor")
Akka.system.actorOf(ResumeServiceActor.props, name = "resume-service-actor")
Akka.system.actorOf(TriggerActor.props, name = "trigger-actor")
val contextIOActor = Akka.system.actorOf(ContextIOActor.props, name = "contextio-actor")
val contextIOThrottler = Akka.system.actorOf(Props(classOf[TimerBasedThrottler], contextio_throttle msgsPer 60.second), name = "conetxt-io-throttler")
contextIOThrottler ! SetTarget(Some(contextIOActor))
def trigger = Action(BodyParsers.parse.json) { request =>
val b = request.body.validate[ScanTriggerRequest]
logger.debug("Processing request with param = " + request.body)
b.fold(
errors => {
BadRequest(Json.obj("status" -> "OK", "message" -> JsError.toJson(errors)))
},
trigger => {
//TODO more validation on the params
logger.debug("Param validation success, param = " + trigger)
val triggerActor = Akka.system.actorSelection("/user/trigger-actor")
logger.debug("Prepare the trigger action = " + triggerActor + ", and fire message!")
triggerActor ! trigger
Ok(Json.obj("status" -> "OK"))
}
)
}
}
In any other actors or any other controllers, we can do as follow:
val jobServiceActor = context.actorSelection("/user/job-service-actor")
jobServiceActor ! JobContentPersistMessage(msg.jobId, jobCity, msg.emailContent)
In my case I have one controller to trigger my actions, maybe in the future I need to set up the AKKA system in global class or some where.
2 WS to call other Micro Service
Here is how I call other micro service
import akka.actor.{Actor, Props}
import com.sillycat.dao.{DAO, IpplyJobDAO, ResumeDAO}
import com.sillycat.util.IncludeLogger
import models.messages.jobs.{JobPersistMessage, JobScanMessage, JobContentParseMessage, JobContentScanMessage}
import models.messages.resumes._
import models.{ResumeScanResponse, ResumeScanAttachmentResponse, JobContentResponse, JobScanResponse}
import play.api.libs.json.Json
import play.api.libs.ws.{WS, WSResponse}
import utils.IncludeRegex
import play.api.Play.current
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
…snip...
case msg:JobContentScanMessage => {
logger.debug("receiving the message JobContentScanMessage" + msg)
//context.io fetch job content, actually it is email content, I need the city and location info
val future:Future[WSResponse] = WS.url(contextio_host + "/jobs/" + msg.accountCode +"/" + msg.messageCode ).get()
//scan jobs success
future onSuccess {
case response if response.status == 200 => {
logger.trace("Scanning job content = " + response.json)
response.json.asOpt[JobContentResponse].map { job =>
logger.trace("Scanning result job content = " + job)
val jobContentParseMessage = JobContentParseMessage(msg.jobId, job.content)
logger.debug("fire the message to job-parse-actor to parse email content.")
jobParseActor ! jobContentParseMessage
}
}
case response => {
//handling error
logger.error("Error handle with status = " + response.status + " message = " + response.body)
}
}
//scan jobs fail
future onFailure {
case t => logger.error("An error has occured: " + t.getMessage)
}
}
For post
val request_data = Json.obj(
"provider" -> "CRAIGSLIST",
"accountCode" -> msg.accountCode,
"dateBefore" -> msg.dateBefore.toString(default_date_time_format),
"dateAfter" -> msg.dateAfter.toString(default_date_time_format),
"limit" -> msg.limit,
"offset" -> msg.offset
)
logger.debug("Send json request parameter = " + request_data)
val future:Future[WSResponse] = WS.url(contextio_host + "/jobs/scan").post(request_data)
References:
https://www.playframework.com/documentation/2.4.3/ScalaWS
https://www.playframework.com/documentation/2.4.3/ScalaAkka
akka system
http://sillycat.iteye.com/blog/1767866
http://sillycat.iteye.com/blog/1768625
http://sillycat.iteye.com/blog/1768626
http://sillycat.iteye.com/blog/2099267
http://sillycat.iteye.com/blog/2100232
http://sillycat.iteye.com/blog/2102694
http://sillycat.iteye.com/blog/2175999
发表评论
-
Stop Update Here
2020-04-28 09:00 260I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 429NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 310Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 321Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 291Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 378Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 373Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 325Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 397VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 334Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 415NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 359Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 290Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 207GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 390GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 274GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 263Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 259Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 250Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 233Serverless with NodeJS and Tenc ...
相关推荐
Java and Scala developers can use the Play Framework and the Akka concurrency toolkit to easily implement reactive applications without building everything from scratch. Reactive Web Applications ...
Reactive Programming with Scala and Akka 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
play-zipkin-tracing:使用Zipkin为Play Framework和Akka提供分布式跟踪
Get to grips with the full range of Akka features including the upcoming and cutting edge experimental modules A comprehensive coverage of the principles of FRP with real-world use cases to solve ...
Akka Guice 集成安装将以下内容添加到您的 build.sbt: resolvers += " release repository " at " ... 添加行(插件前面的数字是您播放应用程序内的加载顺序): 10000:akkaGuice.AkkaGuicePlugin在 Global.java 中...
Reactive.programming.with.scala.and.akka.pdf Reactive Programming with Scala and Akka
使用 Actor 将 SignalR到 PlayFramework。 SignalJ(SignalR)是一个服务器到客户端和客户端到服务器的通信框架。 使用回退机制与浏览器通信。 首先尝试 websockets,然后服务器发送事件,最后长轮询。 还有其他几...
This compact book includes in-depth introductions to RxJava, Akka Streams, and Reactor, and integrates the latest related features from Java 9 and 11, as well as reactive streams programming with the...
/home/ambantis/Documents/dev/play/play4jug/hello2akka/project [info] Set current project to hello2akka (in build file:/home/ambantis/Documents/dev/play/play4jug/hello2akka/) > compile 基于线程的并发 ...
Framework、Akka 和 Apache Spark,使用 Scala 语言。 产品积压 Bettercare4.me 产品待办列表 提供基于差距查找患者的搜索功能 在患者记分卡页面上添加差距摘要部分 添加可视化仪表板 患者年龄分布 带有饼图的 HEDIS...
2016年2月出版的《Reactive Programming with Scala and Akka》,介绍了使用scala和akka构建响应式程序的相关技术,pdf格式
Akka is a distributed computing toolkit that enables developers to build correct concurrent and distributed applications using Java and Scala with ease, applications that scale across servers and ...
You’ll start with the big picture of how Akka works, and then quickly build and deploy a fully functional REST service out of actors. You’ll explore test-driven development and deploying and ...
Java and Scala developers can use the Play Framework and the Akka concurrency toolkit to easily implement reactive applications without building everything from scratch. Reactive Web Applications ...
Avoid common mistakes when building distributed, asynchronous, high-performance software with the Akka toolkit and runtime. With this concise guide, author Jamie Allen provides a collection of best ...
Avoid common mistakes when building distributed, asynchronous, high-performance software with the Akka toolkit and runtime. With this concise guide, author Jamie Allen provides a collection of best ...
You'll start with the big picture of how Akka works, and then quickly build and deploy a fully functional REST service out of actors. You'll explore test-driven development and deploying and scaling ...
Akka is a Scala-based toolkit that simplifies developing concurrent distributed applications.... The book concludes with practical advice on how to tune and customize a system built with Akka.
play-akka-cluster-websocket-chat 基于 Play Framework 2 (Java) 和 Akka 集群的多房间可扩展聊天这个集成 play 框架 2 应用程序(java)与 akka 集群的示例。 它提供了为规模系统添加新的播放节点的可能性。 当一...
This book also covers the most useful tools available in the Scala ecosystem, such as Slick, Play, and Akka, and a whole lot more. It will help you unlock the secrets of building your own up-to-date ...