Akkahttp and servlet文档格式.docx

上传人:b****5 文档编号:15769045 上传时间:2022-11-16 格式:DOCX 页数:6 大小:15.85KB
下载 相关 举报
Akkahttp and servlet文档格式.docx_第1页
第1页 / 共6页
Akkahttp and servlet文档格式.docx_第2页
第2页 / 共6页
Akkahttp and servlet文档格式.docx_第3页
第3页 / 共6页
Akkahttp and servlet文档格式.docx_第4页
第4页 / 共6页
Akkahttp and servlet文档格式.docx_第5页
第5页 / 共6页
点击查看更多>>
下载资源
资源描述

Akkahttp and servlet文档格式.docx

《Akkahttp and servlet文档格式.docx》由会员分享,可在线阅读,更多相关《Akkahttp and servlet文档格式.docx(6页珍藏版)》请在冰豆网上搜索。

Akkahttp and servlet文档格式.docx

importjava.util.logging.Level;

importjava.util.logging.Logger;

importjavax.servlet.AsyncContext;

importjavax.servlet.ServletException;

importjavax.servlet.annotation.WebServlet;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="

/test/async"

asyncSupported=true)

publicclassTestAsyncServletextendsHttpServlet{

privateExecutorexecutor;

@Override

publicvoidinit()throwsServletException{

executor=Executors.newFixedThreadPool(30);

}

protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)

throwsServletException,IOException{

finalAsyncContextasyncContext=req.startAsync();

executor.execute(newRunnable(){

publicvoidrun(){

HttpServletResponseresp=(HttpServletResponse)asyncContext.getResponse();

resp.setContentType("

text/plain"

);

try(PrintWriterwriter=resp.getWriter()){

writer.print("

Ok"

}catch(IOExceptionex){

Logger.getLogger(TestAsyncServlet.class.getName()).log(Level.SEVERE,null,ex);

asyncCplete();

});

}

 

Fixedthreadpoolisusedtoprocessrequestsinbackgroundandresponseiswrittendirectlyfromthethreadofthisthreadpoolwhenitisready.

ThismodelgivesyouanabilitytointegratewithsuchframeworksasAkka.Seeexamplebelow.

importakka.actor.ActorRef;

importakka.actor.ActorSystem;

importakka.actor.Props;

importakka.actor.ReceiveTimeout;

importakka.actor.Terminated;

importakka.actor.UntypedActor;

importscala.concurrent.duration.Duration;

/test/akka"

publicclassTestAkkaServletextendsHttpServlet{

finalActorSystemsystem=(ActorSystem)req.getServletContext()

.getAttribute("

ActorSystem"

system.actorOf(Props.create(AskActor.class,asyncContext));

staticclassTestActorextendsUntypedActor{

publicvoidonReceive(Objectmsg)throwsException{

if(msg=="

Test!

!

"

){

getSender().tell("

getSelf());

}else{

unhandled(msg);

staticclassAskActorextendsUntypedActor{

finalprivateAsyncContextasyncContext;

publicAskActor(AsyncContextasyncContext){

this.asyncContext=asyncContext;

ActorReftestActor=getContext()

.actorOf(Props.create(TestActor.class),"

TestActor"

getContext().watch(testActor);

getContext().setReceiveTimeout(Duration.create("

5seconds"

));

testActor.tell("

publicvoidonReceive(Objectmsg)throwsIOException{

if(msginstanceofReceiveTimeout){

getContext().stop(getSelf());

resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,

"

Timeout"

}elseif(msginstanceofTerminated){

UnexpectedlyStopped"

}elseif(msginstanceofString){

Theworkflowisthefollowing.

•doGet()usesActorSystemtocreateanactoroftype 

AskActorwhichwillbetherootofsupervisionsubtreededicatedtoprocessthisrequest.See 

WhatSupervisionMeans.

•AskActorcreatesaninstanceofTestActorandsendsamessagetoit.MeanwhileitiswatchingtheTestActorinstanceandincaseitterminatesforsomereason 

AskActorwillreceive 

Terminatedmessage.WealsosettimeoutthatmeansifthereisnoresponsefromTestActorinspecifiedamountoftimeAskActorwillgetReceiveTimeoutmessage.

•TestActorreceivesmessagefromAskActor,processesitandsendsaresponse.

•AskAc

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 演讲主持

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1