score:13
edit for play 2.5:
import akka.actor.actorsystem
import akka.stream.actormaterializer
import play.api.libs.ws._
import play.api.libs.ws.ahc.ahcwsclient
import scala.concurrent.future
object main {
import scala.concurrent.executioncontext.implicits._
def main(args: array[string]): unit = {
implicit val system = actorsystem()
implicit val materializer = actormaterializer()
val wsclient = ahcwsclient()
call(wsclient)
.andthen { case _ => wsclient.close() }
.andthen { case _ => system.terminate() }
}
def call(wsclient: wsclient): future[unit] = {
wsclient.url("http://www.google.com").get().map { response =>
val statustext: string = response.statustext
println(s"got a response $statustext")
}
}
}
please see:
- https://www.playframework.com/documentation/2.5.x/scalaws#using-wsclient
- https://www.playframework.com/documentation/2.5.x/scalatestingwebserviceclients
for more detailed examples of standalone wsclient usage. if you are migrating from earlier versions, see https://www.playframework.com/documentation/2.5.x/migration25#play-ws-upgrades-to-asynchttpclient-2
for play 2.4:
do not use raw asynchttpclientconfig.builder for https -- it does not configure a secure sslcontext with hostname validation.
you can create a new wsclient instance using the following code:
import play.api.libs.ws.ning._
import play.api.libs.ws._
val config = new ningasynchttpclientconfigbuilder(defaultwsclientconfig()).build()
val builder = new asynchttpclientconfig.builder(config)
val wsclient:wsclient = new ningwsclient(builder.build())
please note that this will start up threads which will not be closed until you close the client:
wsclient.underlying[ningwsclient].close()
and you may run into memory leaks if you don't close it.
score:4
a started playapplication contains a client instance, which ws.client simply points to it. since you won't start a play application, you have to create your own client, like this:
val client = {
val builder = new com.ning.http.client.asynchttpclientconfig.builder()
new play.api.libs.ws.ning.ningwsclient(builder.build())
}
client.url("http://example.com/").get()
have a look on my project for a similar usecase, i am using play-ws and play-json, without play itself.
score:8
play 2.4 makes it very easy to utilize ws in a standalone app.
the following gist provides a nice working example and the following blog post provides a nice explanation.
here are the highlights.
configure build.sbt
librarydependencies ++= seq(
"com.typesafe.play" %% "play-ws" % "2.4.0-m2"
)
initialize the ws client
val config = new ningasynchttpclientconfigbuilder(defaultwsclientconfig()).build
val builder = new asynchttpclientconfig.builder(config)
val client = new ningwsclient(builder.build)
use ws
client.url("http://www.example.com").get
release ws resources
client.close()
Source: stackoverflow.com
Related Query
- How to have a Scala standalone application that uses the playframework libraries
- How do I scale my Scala REST application that uses Akka?
- In the scala spray framework, how can I create multiple http clients that have different configurations (e.g., timeouts, retries)
- How to set up a scala project in IntelliJ IDEA that uses git libraries
- How can I view the code that Scala uses to automatically generate the apply function for case classes?
- Scala - how to make the SortedSet with custom ordering hold multiple different objects that have the same value by which we sort?
- How to make the 'public' schema default in a Scala Play project that uses PostgreSQL?
- How do I write a query for mongodb using the casbah driver for scala that uses a substring and checks if the field is in a list of supplied values?
- How to build a scala application that was created in eclipse scala plugin FROM THE CL
- In a scala macro, how to get the full name that a class will have at runtime?
- How do I change the version of a dependency in a Scala application that I haven't set?
- How to see the list of JARs that are loaded with my Scala Application
- Created scala docker image using sbt native plugin. I have to add LDAP CA cert. How to add that in the image
- How to parse a csv that uses ^A (i.e. \001) as the delimiter with spark-csv?
- How do I write a JSON Format for an object in the Java library that doesn't have an apply method?
- What is the syntax for creating a Map in Scala that uses an enum as a key?
- scala spec2 I am Unable to create a test that uses must be_== and failure at the same time
- How do I create a generic Scala function that returns an object based on the generic type?
- How does the Scala type checker know to prevent calling flatten on a List that is already flat
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How to convert/wrap a sequence in scala to an Option[Seq] so that if the list is empty, the Option is None
- Scala - How to split the probability column (column of vectors) that we obtain when we fit the GMM model to the data in to two separate columns?
- How to port Scala 2.7.7 code that uses scala.collection.jcl to Scala 2.8?
- How do i change the Scala version that sbt works with?
- How does the Scala type system know that cons + Nil is exhaustive?
- How to make compiler check that 2 method arguments have the same type?
- How can I ensure that the dynamic type of my custom Scala collection is preserved during a map()?
- How do I iterate over the routes in a playframework 2 scala application?
- According to the Scala Language Specification, packages are AnyRef values and have types. How does this make sense?
- How do I run a comand line scala script that uses Play Framework app database?
More Query from same tag
- Where does one place a scala file in the Play Framework 2.0 so that it is compiled as part of the default package?
- Use scala.Either from Java
- Scalacheck prop type mismatch
- How to read grib file by netcdfAll
- Where is List("String") described in API?
- ScalaSpark: unable to create dataframe with scala-client dependency
- How can I convert Rep[Option[Instant]] to Rep[Option[String]] in scala
- Inconsistent JSON schema guess with Spark dataframes
- apache spark stand alone connecting to mongodb with scala using casbah
- Getting all Keys associated with the max value in Spark
- Filter Seq[Map[k,v]] given a Seq[String]
- Scala: Is there a way to improve the timing performance of this convolution code?
- Match parameterized type using Scala's pattern matching
- Sending a response back when a chain of actors are involved in Spray
- Dynamically compiling scala class files at runtime in Scala 2.11
- Counting longest sequence of specific elements in a list contained within a spark.sql database column
- How to change sbt version for project
- How to create com.google.common.collect.HashBiMap
- Closing over the loop variable in Scala
- Getting MissingRequirementError after moving classes to another package
- Change the format of file path which is partitioned by java.sql.Timestamp
- Random Forest Analysis
- Keeping a sum based on future operations
- spark streaming JSON value in dataframe column scala
- Simplest form of multithreading in scala
- Using this keyword to inherit?
- Adding custom handler to jetty throws ClassNotFoundException
- Why I should use context.become() to store internal state in Actor?
- akka streams with spark streaming: messages are not delivered to actor; getting dead letters
- Failed to find Spark assembly JAR on windows 8.1