本文整理汇总了Scala中akka.stream.ActorMaterializer类的典型用法代码示例。如果您正苦于以下问题:Scala ActorMaterializer类的具体用法?Scala ActorMaterializer怎么用?Scala ActorMaterializer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ActorMaterializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Main
//设置package包名称以及导入依赖的类
package onextent.oemap.server
import com.typesafe.scalalogging.LazyLogging
import scala.concurrent.duration._
import akka.actor._
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.util.Timeout
import com.typesafe.config.{Config, ConfigFactory}
object Main extends LazyLogging with App with RestInterface {
val config: Config = ConfigFactory.load().getConfig("main")
val logLevel: String = config.getString("logLevel")
val appName: String = config.getString("appName")
val host: String = config.getString("host")
val port: Int = config.getInt("port")
implicit val system = ActorSystem("map-management-service")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
implicit val timeout = Timeout(10 seconds)
val api = routes
Http().bindAndHandle(handler = api, interface = host, port = port) map {
binding =>
logger.info(s"REST interface bound to ${binding.localAddress}")
} recover {
case ex =>
logger.error(s"REST interface could not bind to $host:$port",
ex.getMessage)
}
}
示例2: StatisticDataFetcher
//设置package包名称以及导入依赖的类
package com.example
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import com.typesafe.config.ConfigFactory
import scala.concurrent.{ExecutionContext, Future}
class StatisticDataFetcher(implicit ec: ExecutionContext, system: ActorSystem, materializer: ActorMaterializer) extends AutoMarshaller {
val statisticsServiceUrl = {
val config = ConfigFactory.load()
config.getString("statisticsServiceUrl")
}
def getStatistics(): Future[List[StatisticData]] = {
implicit val serialization = this.serialization
implicit val formats = this.formats
val responseFuture: Future[HttpResponse] =
Http(system).singleRequest(HttpRequest(uri = statisticsServiceUrl))
responseFuture flatMap { response =>
Unmarshal(response.entity).to[StatisticsResponse] map { statisticsResponse =>
statisticsResponse.query.results.quote
}
}
}
}
示例3: Settings
//设置package包名称以及导入依赖的类
package com.scalaio.kafka.consumer
import akka.actor.ActorSystem
import akka.kafka.ConsumerMessage.CommittableMessage
import akka.kafka.scaladsl.Consumer
import akka.kafka.{ConsumerSettings, ProducerSettings, Subscriptions}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import com.scalaio.kafka.consumer.Settings.consumerSettings
import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.common.serialization.{ByteArrayDeserializer, ByteArraySerializer, StringDeserializer, StringSerializer}
import scala.concurrent.Future
object Settings {
def consumerSettings(implicit system: ActorSystem) =
ConsumerSettings(system, new ByteArrayDeserializer, new StringDeserializer)
.withBootstrapServers("localhost:9092")
.withGroupId("CommittableSourceConsumer")
.withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
def producerSettings(implicit system: ActorSystem) =
ProducerSettings(system, new ByteArraySerializer, new StringSerializer)
.withBootstrapServers("localhost:9092")
}
object CommittableSource extends App {
type KafkaMessage = CommittableMessage[Array[Byte], String]
implicit val system = ActorSystem("CommittableSourceConsumerMain")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
// explicit commit
Consumer
.committableSource(consumerSettings, Subscriptions.topics("topic1"))
.mapAsync(1) { msg =>
BusinessController.handleMessage(msg.record.value)
.flatMap(response => msg.committableOffset.commitScaladsl())
.recoverWith { case e => msg.committableOffset.commitScaladsl() }
}
.runWith(Sink.ignore)
}
object BusinessController {
type Service[A, B] = A => Future[B]
val handleMessage: Service[String, String] =
(message) => Future.successful(message.toUpperCase)
}
示例4:
//设置package包名称以及导入依赖的类
package com.shashank.akkahttp.project
import java.util.concurrent.ConcurrentHashMap
import akka.actor.ActorSystem
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import com.shashank.akkahttp.project.Models.{LoadRequest, ServiceJsonProtoocol}
import spray.json.JsArray
import scala.collection.JavaConverters._
import spray.json.{DefaultJsonProtocol, JsArray, pimpAny}
import spray.json.DefaultJsonProtocol._
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql._
trait RestService {
implicit val system: ActorSystem
implicit val materializer: ActorMaterializer
implicit val sparkSession: SparkSession
val datasetMap = new ConcurrentHashMap[String, Dataset[Row]]()
import ServiceJsonProtoocol._
val route =
pathSingleSlash {
get {
complete {
"welcome to rest service"
}
}
} ~
path("load") {
post {
entity(as[LoadRequest]) {
loadRequest => complete {
val id = "" + System.nanoTime()
val dataset = sparkSession.read.format("csv")
.option("header", "true")
.load(loadRequest.path)
datasetMap.put(id, dataset)
id
}
}
}
} ~
path("view" / """[\w[0-9]-_]+""".r) { id =>
get {
complete {
val dataset = datasetMap.get(id)
dataset.take(10).map(row => row.toString())
}
}
}
}
示例5: UnMarshalling
//设置package包名称以及导入依赖的类
package com.shashank.akkahttp.basic.routing
import akka.actor.ActorSystem
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, HttpResponse, MessageEntity}
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.{ActorMaterializer, Materializer}
import akka.util.ByteString
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import spray.json._
object UnMarshalling {
def main(args: Array[String]) {
implicit val sys = ActorSystem("IntroductionToAkkaHttp")
implicit val mat:Materializer = ActorMaterializer()
//type FromStringUnmarshaller[T] = Unmarshaller[String, T]
val intFuture = Unmarshal("42").to[Int]
val int = Await.result(intFuture, 1.second)
println("int unmarshalling "+int)
//type FromStringUnmarshaller[T] = Unmarshaller[String, T]
val boolFuture = Unmarshal("off").to[Boolean]
val bool = Await.result(boolFuture, 1.second)
println("off unmarshalling "+bool)
//type ToEntityMarshaller[T] = Marshaller[T, MessageEntity]
val string = "Yeah"
val entityFuture = Marshal(string).to[MessageEntity]
val entity = Await.result(entityFuture, 1.second) // don't block in non-test code!
println(entity)
//type ToResponseMarshaller[T] = Marshaller[T, HttpResponse]
val errorMsg = "Not found, pal!"
val responseFuture = Marshal(404 -> errorMsg).to[HttpResponse]
val response = Await.result(responseFuture, 1.second)
println(response)
//type FromEntityUnmarshaller[T] = Unmarshaller[HttpEntity, T]
val jsonByteString = ByteString("""{"name":"Hello"}""")
val httpRequest = HttpRequest(HttpMethods.POST, entity = jsonByteString)
val jsonDataUnmarshalledFuture = Unmarshal(httpRequest).to[String]
val jsonDataUnmarshalled = Await.result(jsonDataUnmarshalledFuture, 1.second)
println(jsonDataUnmarshalled)
sys.terminate()
}
}
示例6: MovieListPipeline
//设置package包名称以及导入依赖的类
package com.stacktrace.yo.scrapeline.imdb.pipelines
import java.nio.file.Paths
import akka.NotUsed
import akka.stream.scaladsl.{FileIO, Flow, Keep, Sink, Source}
import akka.stream.{ActorMaterializer, IOResult}
import akka.util.ByteString
import com.stacktrace.yo.scrapeline.core.ScrapeClient.jsoup
import com.stacktrace.yo.scrapeline.core._
import com.stacktrace.yo.scrapeline.imdb.Domain.MovieNameAndDetailUrl
import net.ruippeixotog.scalascraper.dsl.DSL._
import net.ruippeixotog.scalascraper.model.Document
import net.ruippeixotog.scalascraper.scraper.ContentExtractors.elementList
import scala.concurrent.Future
class MovieListPipeline(implicit val m: ActorMaterializer) {
def getPipelineSource: Source[jsoup.DocumentType, NotUsed] = Source.single(ScrapeClient.scrape("http://www.the-numbers.com/movie/budgets/all"))
def getParseFlow: Flow[Document, MovieNameAndDetailUrl, NotUsed] = {
Flow[Document]
.mapConcat(doc => {
val table = doc >> elementList("table tr")
val movieLinkTuples = table.flatMap(tr => {
val name = tr >> elementList("tr b a")
name.map(
link => {
MovieNameAndDetailUrl(link.text, "http://www.the-numbers.com/" + link.attr("href"))
}
)
})
movieLinkTuples
})
}
def getPipeOut: Sink[MovieNameAndDetailUrl, Future[IOResult]] = Flow[MovieNameAndDetailUrl]
.map(s => ByteString(s.name + "\n"))
.toMat(FileIO.toPath(Paths.get("movie.txt")))(Keep.right)
def buildAndRun: Future[IOResult] = {
getPipelineSource
.via(getParseFlow)
.runWith(getPipeOut)
}
}
示例7: Boot
//设置package包名称以及导入依赖的类
package com.saleass.app.web
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn
object Boot extends App {
implicit val system = ActorSystem("saleass")
implicit val mat = ActorMaterializer()
implicit val ec = system.dispatcher
val route = path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Damn you av!"))
}
}
val httpBindFuture = Http().bindAndHandle(route, "localhost", 8080)
StdIn.readLine()
httpBindFuture.flatMap(_.unbind()).onComplete(_ => system.terminate())
}
示例8: Request
//设置package包名称以及导入依赖的类
package org.freetrm.eventstore.http
import akka.actor.{ActorRef, ActorSystem}
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import akka.util.Timeout
import com.typesafe.config.Config
import org.freetrm.eventstore.utils.Log
import org.freetrm.eventstore.{EventSourceReader, EventSourceWriter}
import scaldi.Module
import scala.concurrent.Future
import scala.concurrent.duration._
case class Request(client: ActorRef, req: HttpRequest)
class WebService extends Module with Log {
implicit lazy val system = inject[ActorSystem]
implicit lazy val mat = ActorMaterializer()
def start(): Future[ServerBinding] = {
val conf = inject[Config]
implicit val timeout = Timeout(5.seconds)
val interface = conf.getString("www-service.interface")
val port = conf.getInt("www-service.port")
log.info(s"Starting http server on $interface:$port")
Http().bindAndHandle(service.flow, interface, port)
}
def stop() {}
def service: EventStoreHttpServer = {
implicit val system = inject[ActorSystem]
val conf = inject[Config]
val cookie = conf.getString("www-service.cookie")
new EventStoreHttpServer(
inject[EventSourceWriter],
inject[EventSourceReader],
cookie)
}
}
示例9: PrintMoreNumbers
//设置package包名称以及导入依赖的类
package sample.stream_actor_simple
import akka.actor.Actor
import akka.stream.{ActorMaterializer, KillSwitches, UniqueKillSwitch}
import akka.stream.scaladsl.{Keep, Sink, Source}
import scala.concurrent.duration._
class PrintMoreNumbers(implicit materializer: ActorMaterializer) extends Actor {
private implicit val executionContext = context.system.dispatcher
private val (killSwitch: UniqueKillSwitch, done) =
Source.tick(0.seconds, 1.second, 1)
.scan(0)(_ + _)
.map(_.toString)
.viaMat(KillSwitches.single)(Keep.right)
.toMat(Sink.foreach(println))(Keep.both)
.run()
done.map(_ => self ! "done")
override def receive: Receive = {
//When the actor is stopped, it will also stop the stream
case "stop" =>
println("Stopping")
killSwitch.shutdown()
case "done" =>
println("Done")
context.stop(self)
context.system.terminate()
}
}
示例10: ExampleMain
//设置package包名称以及导入依赖的类
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.documenteddsl.documentation._
import akka.stream.ActorMaterializer
import scala.concurrent.Future
object ExampleMain extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val routes = {
val apiRoutes = ExampleRoutes.route
val documentation = apiRoutes.selfDescribe(Documentation())
val documentationRoute = pathPrefix("api.json") {
DocumentationRoutes(documentation)
}
apiRoutes ~ documentationRoute
}
for {
port <- Http().bindAndHandle(routes, "localhost", 8080)
_ <- Future {
println(
s"""Example App is available at http://localhost:8080
|
|You can try ExampleResource at
| GET http://localhost:8080/resources
| GET http://localhost:8080/resources/x
| POST http://localhost:8080/resources {"name": "new resource"}
| PUT http://localhost:8080/resources/x {"name": "updated name"}
| DELETE http://localhost:8080/resources/x
|
|Api Documentation (Json) is available at
| OPTIONS http://localhost:8080/api.json // api toc
| GET http://localhost:8080/api.json // full api
| GET http://localhost:8080/api.json/x // specified api route
|
|Press RETURN to stop...
""".stripMargin)
io.StdIn.readLine()
}
_ <- port.unbind()
} { system.terminate() }
}
示例11: ActorSpec
//设置package包名称以及导入依赖的类
package com.github.j5ik2o.reactive.redis
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.testkit.{ ImplicitSender, TestKit }
import akka.util.Timeout
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ BeforeAndAfterAll, DiagrammedAssertions, FunSpecLike }
import scala.concurrent.duration._
abstract class ActorSpec(_system: ActorSystem)
extends TestKit(_system)
with ImplicitSender
with FunSpecLike
with DiagrammedAssertions
with BeforeAndAfterAll
with TimeFactorSupport
with ScalaFutures {
implicit override val patienceConfig: PatienceConfig = PatienceConfig(15 * timeFactor seconds)
implicit val materializer = ActorMaterializer()
implicit val timeout = Timeout(15 seconds)
override protected def afterAll(): Unit = {
shutdown()
super.beforeAll()
}
}
示例12: AService
//设置package包名称以及导入依赖的类
package service.a.app
import scala.concurrent.ExecutionContext
import scala.io.StdIn
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport.defaultNodeSeqMarshaller
import akka.http.scaladsl.marshalling.ToResponseMarshallable.apply
import akka.http.scaladsl.server.Directive.addByNameNullaryApply
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow
import akka.stream.ActorMaterializer
import service.a.core.ACoreBooted
object AService extends App with ACoreBooted with AConfig {
protected implicit val executor: ExecutionContext = system.dispatcher
protected implicit val materializer: ActorMaterializer = ActorMaterializer()
val route =
path("hello") {
get {
complete {
<h1>Say hello to akka-http</h1>
}
}
} ~
path("dude") {
get {
complete {
<h1>Say hello to dude</h1>
}
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", port.toInt)
println(s"Server online at http://localhost:11011/\nPress RETURN to stop...")
StdIn.readLine()
bindingFuture
.flatMap(_.unbind())
.onComplete { _ => system.terminate() }
}
示例13: Main
//设置package包名称以及导入依赖的类
package edu.uw.at.iroberts.wirefugue.sensor
import java.nio.file.Paths
import akka.actor.ActorSystem
import akka.kafka.{ProducerMessage, ProducerSettings}
import akka.kafka.scaladsl.Producer
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{FileIO, Sink}
import com.typesafe.config.ConfigFactory
import edu.uw.at.iroberts.wirefugue.pcap.PcapFileRaw.LinkType
import edu.uw.at.iroberts.wirefugue.pcap._
import edu.uw.at.iroberts.wirefugue.kafka.producer.{KafkaKey, PacketProducer}
import edu.uw.at.iroberts.wirefugue.kafka.serdes.PacketSerializer
import edu.uw.at.iroberts.wirefugue.protocol.overlay.{Ethernet, IPV4Datagram}
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.serialization.{ByteArraySerializer, IntegerSerializer}
import scala.concurrent.Await
import scala.concurrent.duration._
object Main {
def main(args: Array[String]): Unit = {
if (args.length < 1) {
println("Please specify a filename as the first argument")
System.exit(1)
}
val config = ConfigFactory.load("application.conf")
implicit val system = ActorSystem("stream-producer-system", config)
implicit val materializer = ActorMaterializer()
val producerSettings = ProducerSettings[Integer, Packet](system, None, None)
val doneF = PcapSource(Paths.get(args(0)).toUri)
.filter( p => p.network == LinkType.ETHERNET && p.ip.isDefined )
.map( packet => new ProducerRecord[Integer, Packet]("packets", packet.key.##, packet))
.map( pr => new ProducerMessage.Message[Integer, Packet, Unit](pr, ()))
.via(Producer.flow(producerSettings))
.runWith(Sink.foreach(println))
try {
Await.ready(doneF, 10 seconds)
}
finally {
system.terminate()
}
}
}
示例14: UsersProtonModule
//设置package包名称以及导入依赖的类
package proton.users
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{ExceptionHandler, Route, RouteResult}
import akka.stream.ActorMaterializer
import com.typesafe.config.Config
import com.typesafe.scalalogging.LazyLogging
import scaldi.{Injectable, Module, TypesafeConfigInjector}
import spray.json.{CompactPrinter, JsonPrinter}
import scala.concurrent.ExecutionContext
class UsersProtonModule(config: Config) extends Module {
bind[ExecutionContext] to scala.concurrent.ExecutionContext.Implicits.global
bind[ActorSystem] to ActorSystem("ProtonUsers", config) destroyWith (_.terminate())
bind[JsonPrinter] to CompactPrinter
}
object UsersProtonApp extends App with Injectable with LazyLogging with SprayJsonSupport with UsersProtocol {
ProtonConfig.parse("users-dev.conf", args).foreach(c => {
val config = c.config
implicit val injector = TypesafeConfigInjector(config) :: new UsersProtonModule(config)
implicit val executionContext = inject[ExecutionContext]
implicit val system = inject[ActorSystem]
implicit val materializer = ActorMaterializer()
implicit val printer = inject[JsonPrinter]
implicit val exceptionHandler = ExceptionHandler {
case e: Exception =>
logger.error("HTTP unhandled exception.", e)
var message = "HTTP unhandled exception."
if (e != null) {
message = e.getMessage
}
complete(InternalServerError -> Message(message, UsersEvents.Unhandled))
}
def route: Route =
pathSingleSlash {
get {
complete("test")
}
}
Http().bindAndHandle(route, config.getString("proton.ip"), config.getInt("proton.users.http.port"))
})
}
示例15: Boot
//设置package包名称以及导入依赖的类
package com.github.cupenya.hello
import akka.actor.{ ActorSystem, Props }
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
object Boot extends App with Logging
with HelloHttpService
with HealthHttpService
with LivenessProbeHttpService {
implicit val system = ActorSystem()
implicit val ec = system.dispatcher
implicit val materializer = ActorMaterializer()
log.info(s"Starting Hello service")
Http().bindAndHandle(livenessRoute ~ healthRoute ~ helloRoute, Config.http.interface, Config.http.port).transform(
binding => log.info(s"REST interface bound to ${binding.localAddress} "), { t => log.error(s"Couldn't start Hello service", t); sys.exit(1) }
)
}