RuntimeException: There is no HTTP Context available from here

akka
java
sbt
configuration

(Kenny West) #1

Hi All,

I recently migrated our Play 2.2.6 application to Play 2.6.15

Java 1.8
Scala 2.11.12

I am new at Play, but after a few struggles, it builds. When I sbt run, I get this exception. Google and Stack Overflow and github forum searches have only given me errors where the HttpExecutionContext isn’t available for a CompletionStage, but DataPointManagement is a fairly simple java Controller that doesn’t use CompletionStage and doesn’t even have any dependency injection. It never gets into my code as log messages aren’t showing up. I am pulling Guice into my libraryDependencies.

I was hoping one of the Play experts had some ideas.

play.api.UnexpectedException: Unexpected exception[ExceptionInInitializerError: null]
	at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1.reload(DevServerStart.scala:190)
	at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1.get(DevServerStart.scala:124)
	at play.core.server.AkkaHttpServer.play$core$server$AkkaHttpServer$$handleRequest(AkkaHttpServer.scala:222)
	at play.core.server.AkkaHttpServer$$anonfun$4.apply(AkkaHttpServer.scala:137)
	at play.core.server.AkkaHttpServer$$anonfun$4.apply(AkkaHttpServer.scala:137)
	at akka.stream.impl.fusing.MapAsync$$anon$25.onPush(Ops.scala:1194)
	at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:519)
	at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:482)
	at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
	at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:585)
Caused by: java.lang.ExceptionInInitializerError: null
	at controllers.DataPointManagement$$FastClassByGuice$$e7f400b2.newInstance(<generated>)
	at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
	at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:111)
	at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)
	at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:268)
	at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
	at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
	at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:110)
	at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)
	at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:268)
Caused by: java.lang.RuntimeException: There is no HTTP Context available from here.
	at play.mvc.Http$Context.current(Http.java:68)
	at play.mvc.Controller.session(Controller.java:90)
	at controllers.DataPointManagement.<clinit>(DataPointManagement.java:55)
	at controllers.DataPointManagement$$FastClassByGuice$$e7f400b2.newInstance(<generated>)
	at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
	at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:111)
	at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)
	at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:268)
	at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
	at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
[info] p.c.s.AkkaHttpServer - Stopping server...

Here is my build.sbt file:

//-------------------------------------------------------------------
//  Basic Settings
//-------------------------------------------------------------------

name := "ace"

version := "4"

routesGenerator := InjectedRoutesGenerator

//-------------------------------------------------------------------
//  Java Settings
//-------------------------------------------------------------------

javacOptions ++= Seq("-source", "1.8", "-target", "1.8")

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.12"

//-------------------------------------------------------------------
//  Library Dependencies
//-------------------------------------------------------------------

libraryDependencies ++= Seq(
    guice,
    filters,                                                                           // Java      - Filtering
    javaJdbc,                                                                          // Java      - JDBC
    javaJpa,                                                                           // Java      - JPA
    "javax.servlet" % "javax.servlet-api" % "3.1.0",                                   // Servlets
    "org.hibernate" % "hibernate-entitymanager" % "4.2.1.Final",                       // Hibernate - Entity Manager
    "org.hibernate" % "hibernate-envers" % "4.2.1.Final",                              // Hibernate - Audit Annotation
    "org.hibernate" % "hibernate-ehcache" % "4.2.1.Final",                             // Hibernate - 2nd Level Cache
    ws,
    ehcache,                                                                           // Java      - Cache
    javaCore,                                                                          // Java      - Core API
    "mysql" % "mysql-connector-java" % "5.1.42",                                       // MySQL     - Connector
    jdbc,                                                                              // Java      - JDBC
    "org.webjars" %% "webjars-play" % "2.6.3",                                         // WebJars   - Play Version
    "org.webjars" % "bootstrap" % "2.3.1",                                             // WebJars   - Bootstrap
    "com.typesafe.play" %% "play-mailer" % "6.0.1",                                    // Java      - Mailer
    "com.typesafe.play" %% "play-mailer-guice" % "6.0.1",                              // Java      - Mailer Guice
    "com.typesafe.play" %% "play-json" % "2.6.0",
    "com.typesafe.play" %% "play-iteratees" % "2.6.1",
    "com.typesafe.play" %% "play-iteratees-reactive-streams" % "2.6.1",
    "org.hibernate" % "hibernate-search-orm" % "4.4.4.Final",                          // Hibernate - Search
    "org.hibernate" % "hibernate-search-analyzers" % "4.4.4.Final",                    // Hibernate - Search Analyzers
    "org.apache.jclouds.labs" % "rackspace-cloudfiles-us" % "1.8.0",                   // Rackspace - Unknown
    "org.apache.commons" % "commons-math" % "2.0",                                     // Apache    - Math
    "redis.clients" % "jedis" % "2.6.0",                                               // Java      - Redis
    "org.apache.httpcomponents" % "httpclient" % "4.3.3",                              // Apache    - Basic HTTP Protocols
    "org.apache.httpcomponents" % "httpmime" % "4.3.3",                                // Apache    - MIME Coded Entities
    "com.google.code.gson" % "gson" % "2.3.1",                                         // JSON      - Java Objects to JSON
    "com.google.guava" % "guava" % "22.0",                                             // Java      - Google Guava Implementation
    "com.diffplug.guava" % "guava-io" % "19.0.0",                                      // Java      - Google Guava I/O
    "org.docx4j" % "docx4j" % "3.2.0",                                                 // Java      - Microsoft Open XML Objects
    "org.springframework" % "spring-context" % "4.3.18.RELEASE",                       // SpringFW  - Context
    "org.springframework" % "spring-web" % "4.3.18.RELEASE",                           // SpringFW  - Web
    "org.jsonschema2pojo" % "jsonschema2pojo-core" % "0.4.7",                          // JSON      - JSON to Java Objects
    "com.github.fge" % "json-schema-validator" % "2.2.6",                              // JSON      - JSON Validator
    "org.jasypt" % "jasypt" % "1.9.2",                                                 // Java      - Basic Encryption
    "org.jasypt" % "jasypt-hibernate4" % "1.9.2",                                      // Hibernate - Basic Encryption
    "org.apache.activemq" % "activemq-all" % "5.10.1",                                 // ActiveMQ  - Core
    "joda-time" % "joda-time" % "2.7",                                                 // Java      - Time/Date Replacement
    "commons-io" % "commons-io" % "2.6",                                               // Apache    - Common IO Library
    "net.jcip" % "jcip-annotations" % "1.0",                                           // Java      - JCIP Annotations
    "net.minidev" % "json-smart" % "1.3.1",                                            // JSON      - JSON Parser
    "org.bouncycastle" % "bcprov-jdk15on" % "1.52",                                    // Java      - Cryptography Package
    "com.nimbusds" % "nimbus-jose-jwt" % "4.2",                                        // JSON      - WebTokens
    "org.eclipse.birt.runtime.3_7_1" % "org.apache.batik.pdf" % "1.6.0" % "runtime",   // BIRT      - Reports
    "com.typesafe.akka" %% "akka-actor" % "2.5.14",
    "com.typesafe.akka" %% "akka-testkit" % "2.5.14" % Test
)

(Igmar Palsenberg) #2

Without some actual code, it’s hard to give advice.


(Marcos Pereira) #3

Hi @Kenye,

Agree with @igmar that is hard to know what is happening based only on the information you sent. Maybe you can share the code for controllers.DataPointManagement?

Anyway, it looks like you are trying to access the session inside the controller constructor, is that correct? If so, it won’t work because the constructor is invoked when the application is initializing, before any request is made. But to get session data, you first need a request (to know which session are you getting). I would go a little further and say that, for Guice, anything besides assignments happening inside a constructor is code smell.

Best.


(Kenny West) #4

We weren’t getting the session in the constructor, but were initializing a static variable with a session() TimeZone. Thanks!


(Patrick Li) #5

This is one of the changes introduced in Play 2.5 I think, where new Java 8 API replaced the older Play equivalents such as Promise. You now have to explicitly pass in the HttpExecutionContext when you run async code that needs anything HTTP related such as session, and since it is an optional parameter, you only run into errors during runtime and not compile time.


(Marcos Pereira) #6

Hey @chrono_b, you are right about using HttpExecutionContext. For future reference, this is better explained here:

https://www.playframework.com/documentation/2.6.x/JavaAsync#Using-HttpExecutionContext

But, using HttpExecutionContext wouldn’t help in this case if a static variable is being initialized at application start, without having a request at all.

Best.