From 32a4f20b0b5994702f078f7d54a697e3ccd8b773 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Thu, 23 Mar 2017 12:00:47 -0400 Subject: Example for providing cbt's classloader to Akka --- examples/akka-example/Main.scala | 27 +++++++++++++++++++++++++++ examples/akka-example/build/build.scala | 16 ++++++++++++++++ test/test.scala | 1 + 3 files changed, 44 insertions(+) create mode 100644 examples/akka-example/Main.scala create mode 100644 examples/akka-example/build/build.scala diff --git a/examples/akka-example/Main.scala b/examples/akka-example/Main.scala new file mode 100644 index 0000000..1e746f8 --- /dev/null +++ b/examples/akka-example/Main.scala @@ -0,0 +1,27 @@ +package cbt_examples.akka_example + +import akka.http.scaladsl.server._ +import akka.http.scaladsl.model._ +import akka.http.scaladsl.server.Directives._ +import akka.http.scaladsl.settings._ + +object Service extends HttpApp with App { + override protected def route = + path("test") { + get { + complete(HttpResponse()) + } + } + + // CBT isolates it's classloaders. Even when using `direct` mode and a flatClassLoader, + // it will not use the jvm's system classloader stack for the application. + // This means if applications are hard-coded against the system classloader, + // e.g. for loading configuration files from their users or their own jars, + // they will not find them. One way around this is telling CBT to fork the process. + // (via `override def fork = true` in your build) + // This "just" works, but you'll loose all caching speedup benefits. + // Alternatively, applications like akka often allow providing a custom + // classloader instead and then benefit from CBT's classloader caching. Here is how: + val system = akka.actor.ActorSystem( "my-actor-system", classLoader = Some(this.getClass.getClassLoader) ) + startServer("localhost", 8080, ServerSettings(system), system) +} diff --git a/examples/akka-example/build/build.scala b/examples/akka-example/build/build.scala new file mode 100644 index 0000000..7fb2cd1 --- /dev/null +++ b/examples/akka-example/build/build.scala @@ -0,0 +1,16 @@ +package cbt_examples_build.akka_example + +import cbt._ +import java.net.URL + +class Build(val context: Context) extends BaseBuild { + + override def defaultScalaVersion = "2.12.1" + + override def dependencies = + super.dependencies ++ + Resolver(mavenCentral).bind( + ScalaDependency("com.typesafe.akka", "akka-http", "10.0.5") + ) + +} diff --git a/test/test.scala b/test/test.scala index 6004311..9bb3f6b 100644 --- a/test/test.scala +++ b/test/test.scala @@ -226,6 +226,7 @@ object Main{ compile("../examples/scalafmt-example") compile("../examples/scalariform-example") compile("../examples/scalatest-example") + compile("../examples/akka-example") if(slow){ compile("../examples/scalajs-react-example/js") compile("../examples/scalajs-react-example/jvm") -- cgit v1.2.3