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