2011-08-26 5 views
2

J'ai suivi l'exemple here. Ça fonctionne bien. Mais maintenant, je dois effectuer une logique métier dans case get:Get =>Akka REST service + expédition

La logique métier requise inclut l'utilisation de la bibliothèque dispatch. En un mot, je fais une requête sur une page, j'obtiens les données, je les enveloppe dans une classe, et je la renvoie.

Mon cas de réception ressemble à ceci:

case get:Get => { 
    get.response.setContentType(MediaType.APPLICATION_JSON) 
    val response = Sender.doLogin 
    val battery = (response.batteryRemaining/response.batteryCapacity) * 100 
    val soc = (response.pluginState + ", " + response.chargingStatus).replaceAll("_", " ") 
    val jsonResult = pretty(render(("battery" -> battery) ~ ("soc" -> soc))) 
    get OK jsonResult 
    // get OK "works" 
} 

Cela ne fonctionne pas:

ERROR [akka:event-driven:dispatcher:event:handler-5] akka.event.slf4j.Slf4jEventHandler - 
    [akka.http.Servlet30ContextMethodFactory$$anon$2] 
    [org.eclipse.jetty.io.EofException] 
    [org.eclipse.jetty.io.UncheckedIOException: org.eclipse.jetty.io.EofException 
    at org.eclipse.jetty.io.UncheckedPrintWriter.setError(UncheckedPrintWriter.java:107) 
    at org.eclipse.jetty.io.UncheckedPrintWriter.write(UncheckedPrintWriter.java:280) 
    at org.eclipse.jetty.io.UncheckedPrintWriter.write(UncheckedPrintWriter.java:295) 
    at akka.http.RequestMethod$$anonfun$complete$1.apply(Mist.scala:346) 
    at akka.http.RequestMethod$$anonfun$complete$1.apply(Mist.scala:343) 
    at akka.http.RequestMethod$class.rawComplete(Mist.scala:357) 
    at akka.http.Get.rawComplete(Mist.scala:407) 
    at akka.http.RequestMethod$class.complete(Mist.scala:343) 
    at akka.http.Get.complete(Mist.scala:407) 
    at akka.http.RequestMethod$class.complete(Mist.scala:340) 
    at akka.http.Get.complete(Mist.scala:407) 
    at akka.http.RequestMethod$class.OK(Mist.scala:388) 
    at akka.http.Get.OK(Mist.scala:407) 
    at com.thenewmotion.caronline.services.rest.BatteryStatusService$$anonfun$receive$1.apply(BatteryStatusService.scala:36) 
    at com.thenewmotion.caronline.services.rest.BatteryStatusService$$anonfun$receive$1.apply(BatteryStatusService.scala:28) 
    at akka.actor.Actor$class.apply(Actor.scala:563) 
    at com.thenewmotion.caronline.services.rest.BatteryStatusService.apply(BatteryStatusService.scala:27) 
    at akka.actor.LocalActorRef.invoke(ActorRef.scala:890) 
    at akka.dispatch.MessageInvocation.invoke(MessageHandling.scala:25) 
    at akka.dispatch.ExecutableMailbox$class.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:214) 
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$4.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:120) 
    at akka.dispatch.ExecutableMailbox$class.run(ExecutorBasedEventDrivenDispatcher.scala:186) 
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$4.run(ExecutorBasedEventDrivenDispatcher.scala:120) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:662) 
    at akka.dispatch.MonitorableThread.run(ThreadPoolBuilder.scala:181) 
Caused by: org.eclipse.jetty.io.EofException 
    at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:150) 
    at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:97) 
    at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:109) 
    at org.eclipse.jetty.server.HttpWriter.write(HttpWriter.java:283) 
    at org.eclipse.jetty.server.HttpWriter.write(HttpWriter.java:107) 
    at org.eclipse.jetty.io.UncheckedPrintWriter.write(UncheckedPrintWriter.java:271) 
    ... 25 more 
] 

Il semble aussi qu'il commence à faire la demande (avec la bibliothèque d'expédition) après il répond avec HTTP 200 OK.

Quel pourrait être le problème ici?

+1

Pour ce qu'il vaut, votre cas d'utilisation semble être exactement le genre de choses que [Blue Eyes] (https://github.com/jdegoes/blueeyes) a été fait pour. –

+0

Merci, je vais essayer. – folone

Répondre

3
+0

N'a pas aidé. J'ai également vérifié que, effectivement, toute demande d'acheminement émanant du corps de réception aboutit à cette exception. – folone

+0

C'est intéressant. Quand je le laisse sans demandes pendant environ 10 minutes, cela fonctionne une fois, puis cesse de fonctionner à nouveau. – folone

+0

Que se passe-t-il si vous définissez le get.response.setContentType (MediaType.APPLICATION_JSON) juste avant l'obtention OK? –