2013-02-19 5 views
49

Ich rufe eine Webservice wie diese finden:Play-Framework 2.1 - Kann nicht eine implizite ExecutionContext

WS 
    .url(url) 
    .get 
    .map { response => // error occurs on this line 
    response.status match { 
     case 200 => Right(response.json) 
     case status => Left(s"Problem accessing api, status '$status'") 
    } 
} 

Der komplette Fehler: Error: Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global

Antwort

115

Nach this issue, es in der Dokumentation festgelegt ist. Ich brauchte die folgenden Import hinzuzufügen:

import play.api.libs.concurrent.Execution.Implicits._ 
+7

Über den Ausführungskontext. Manche Leute denken, es ist das gleiche wie der globale Kontext von Scala. Es ist jedoch nicht. [Ausführung.scala] (https://github.com/playframework/playframework/blob/2.2.x/framework/src/play/src/main/scala/play/api/libs/concurrent/Execution.scala) verweist auf [Invoker.scala] (https://github.com/playframework/playframework/blob/2.2.x/framework/src/play/src/main/scala/play/core/system/Invoker.scala?source = cc) So ist es eigentlich der Ausführungskontext des Play Actor-Systems. Dadurch können Sie das Verhalten mit der application.conf ändern. – EECOLOR

+0

Der Link "Dieses Problem" ist jetzt defekt. Es gibt jedoch ein Archiv im web.archive: http://web.archive.org/web/20140222113140/http://play.lighthouseapp.com/projects/82401/tickets/899-error-cannot-find-an -implicit-executioncontext-entweder-require-one-yourself-or-import-executioncontextimplicitsglobal – EdgeCaseBerg

+0

@EdgeCaseBerg Vielen Dank, ich habe den Link – EECOLOR

-1

Eine alternative Option:

import scala.concurrent.ExecutionContext.Implicits.global 
+0

Bitte beachten Sie meinen Kommentar zur upvoted Frage zu diesem Ausführungskontext. – EECOLOR

1

Seit 2.4 Spielen Sie Standard execution context über Guice Abhängigkeit injizieren kann: Injektion.

class Foo @Inject()()(implicit ec:ExecutionContext) { 

def bar() = { 
    WS.url(url) 
    .get 
    .map { response => // error occurs on this line 
     response.status match { 
     case 200 => Right(response.json) 
     case status => Left(s"Problem accessing api, status '$status'") 
    } 
    } 
} 
+0

Ihr Code fügt den Ausführungskontext nicht ein. Es verwendet weiterhin implizite Auflösung, um den Ausführungskontext zu erhalten. – EECOLOR

+0

Laut Play-Dokumentation (mindestens 2.6), ist @ mgosks Antwort der Standard; siehe: https://www.playframework.com/documentation/2.6.x/ThreadPools#Using-the-default-thread-pool – juanmirocks

+0

Es gibt jedoch mehr dazu. Sie können auch 'CustomExecutionContext' erweitern/definieren. Siehe: https://www.playframework.com/documentation/2.6.x/Migration26#play.api.libs.concurrent.Execution-is-deprecated und https://www.playframework.com/documentation/2.6.x/ Highlights26 # CustomExecutionContext-and-Thread-Pool-Sizing – juanmirocks

Verwandte Themen