scala.concurrent.duration.MILLISECONDS Scala Examples
The following examples show how to use scala.concurrent.duration.MILLISECONDS.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: BatchingLedgerWriterConfigReader.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.participant.state.kvutils.app.batch import com.daml.ledger.participant.state.kvutils.api.BatchingLedgerWriterConfig import com.daml.ledger.participant.state.kvutils.api.BatchingLedgerWriterConfig.reasonableDefault import scopt.Read import scala.concurrent.duration.{Duration, MILLISECONDS} object BatchingLedgerWriterConfigReader { lazy val UsageText: String = { val default = BatchingLedgerWriterConfig.reasonableDefault "Configuration for batching of submissions. The optional comma-separated key-value pairs with their defaults are: [" + List( s"enable=${default.enableBatching}", s"max-queue-size=${default.maxBatchQueueSize}", s"max-batch-size-bytes=${default.maxBatchSizeBytes}", s"max-wait-millis=${default.maxBatchWaitDuration.toMillis}", s"max-concurrent-commits=${default.maxBatchConcurrentCommits}" ).mkString(", ") + "]" } implicit val optionsReader: Read[BatchingLedgerWriterConfig] = Read.mapRead[String, String].map { _.foldLeft[BatchingLedgerWriterConfig](reasonableDefault) { case (config, (key, value)) => key match { case "enable" => config.copy(enableBatching = Read.booleanRead.reads(value)) case "max-queue-size" => config.copy(maxBatchQueueSize = Read.intRead.reads(value)) case "max-batch-size-bytes" => config.copy(maxBatchSizeBytes = Read.longRead.reads(value)) case "max-wait-millis" => config.copy(maxBatchWaitDuration = Duration(Read.longRead.reads(value), MILLISECONDS)) case "max-concurrent-commits" => config.copy(maxBatchConcurrentCommits = Read.intRead.reads(value)) case _ => config } } } }
Example 2
Source File: BatchingQueueFactory.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.participant.state.kvutils.api import scala.concurrent.duration.{Duration, MILLISECONDS} object BatchingQueueFactory { def batchingQueueFrom(batchingLedgerWriterConfig: BatchingLedgerWriterConfig): BatchingQueue = if (batchingLedgerWriterConfig.enableBatching) { DefaultBatchingQueue( maxQueueSize = batchingLedgerWriterConfig.maxBatchQueueSize, maxBatchSizeBytes = batchingLedgerWriterConfig.maxBatchSizeBytes, maxWaitDuration = batchingLedgerWriterConfig.maxBatchWaitDuration, maxConcurrentCommits = batchingLedgerWriterConfig.maxBatchConcurrentCommits ) } else { batchingQueueForSerialValidation(batchingLedgerWriterConfig.maxBatchQueueSize) } private def batchingQueueForSerialValidation(maxBatchQueueSize: Int): DefaultBatchingQueue = DefaultBatchingQueue( maxQueueSize = maxBatchQueueSize, maxBatchSizeBytes = 1, maxWaitDuration = Duration(1, MILLISECONDS), maxConcurrentCommits = 1 ) }
Example 3
Source File: IterateeSpecification.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.libs.iteratee import com.webtrends.harness.libs.iteratee.internal.executeFuture import scala.concurrent.{ Await, ExecutionContext, Future, Promise } import scala.concurrent.duration.{ Duration, SECONDS, MILLISECONDS } import scala.util.Try def delayed(it: => Iteratee[String, String], delay: Duration = Duration(5, MILLISECONDS))(implicit ec: ExecutionContext): Iteratee[String, String] = { Iteratee.flatten(timeout(it, delay)) } val timer = new java.util.Timer(true) def timeout[A](a: => A, d: Duration)(implicit e: ExecutionContext): Future[A] = { val p = Promise[A]() timer.schedule(new java.util.TimerTask { def run() { p.complete(Try(a)) } }, d.toMillis) p.future } }
Example 4
Source File: DemoApp.scala From constructr-consul with Apache License 2.0 | 5 votes |
package com.tecsisa.constructr.coordination package demo import akka.actor.{ ActorRef, ActorSystem, Address } import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives import akka.pattern.ask import akka.stream.ActorMaterializer import akka.util.Timeout import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{ Duration, MILLISECONDS } object DemoApp { val conf = ConfigFactory.load() val hostname = conf.getString("demo.hostname") val httpPort = conf.getInt("demo.port") def main(args: Array[String]): Unit = { // Create an Akka system implicit val system = ActorSystem("ConstructR-Consul") import system.dispatcher implicit val mat = ActorMaterializer() // Create an actor that handles cluster domain events val cluster = system.actorOf(SimpleClusterListener.props, SimpleClusterListener.Name) Http().bindAndHandle(route(cluster), hostname, httpPort) } private def route(cluster: ActorRef) = { import Directives._ implicit val timeout = Timeout( Duration( conf.getDuration("demo.cluster-view-timeout").toMillis, MILLISECONDS ) ) path("member-nodes") { // List cluster nodes get { onSuccess( (cluster ? SimpleClusterListener.GetMemberNodes).mapTo[Set[Address]] )(addresses => complete(addresses.mkString("\n"))) } } } }
Example 5
Source File: syntax.scala From actors-cats-effect-fs2 with Apache License 2.0 | 5 votes |
package app import java.time.Instant import cats.effect.Clock import cats.syntax.flatMap._ import cats.syntax.functor._ import cats.{Functor, MonadError} import scala.concurrent.duration.MILLISECONDS object syntax { final case class NotCollectedException[A](a: A) extends RuntimeException(s"value not collected: $a") implicit final class MonadErrorThrowableSyntax[F[_], A](private val fa: F[A]) extends AnyVal { def collect[B](pf: PartialFunction[A, B])(implicit F: MonadError[F, Throwable]): F[B] = fa.flatMap { pf.andThen(F.pure) .applyOrElse(_, (a: A) => { F.raiseError[B](NotCollectedException(a)) }) } } implicit final class ClockSyntax[F[_]](private val clock: Clock[F]) extends AnyVal { def now(implicit F: Functor[F]): F[Instant] = clock.realTime(MILLISECONDS).map(Instant.ofEpochMilli) } }
Example 6
Source File: JumpCameraControls.scala From threejs-facade with Mozilla Public License 2.0 | 5 votes |
package org.denigma.threejs.extensions.controls import org.denigma.threejs.extensions.animations.{Animation, Scheduler} import org.denigma.threejs.{Camera, Scene, Vector3} import org.scalajs.dom import org.scalajs.dom.raw.{Element, HTMLElement} import org.scalajs.dom.MouseEvent import scala.concurrent.duration import scala.concurrent.duration.Duration import scala.concurrent.duration.MILLISECONDS import scala.language.postfixOps class JumpCameraControls(val camera: Camera, val element: Element, //scalastyle:ignore val scene: Scene, val width: Double, val height: Double, center: Vector3 = new Vector3()) extends HoverControls(camera, element, center) with IntersectionControls { implicit val scheduler = new Scheduler().start() override def onMouseMove(event: MouseEvent)= { this.onCursorMove(event.clientX, event.clientY, width, height) rotateOnMove(event) } def moveTo(position: Vector3): Unit = { val start = center.clone() val dp = new Vector3().subVectors(position, center) dom.console.info(dp) new Animation(Duration(1, duration.SECONDS))(p => { val m = dp.clone().multiplyScalar(p) val cur = start.clone().add(m) // dom.console.info(cur) center.copy(cur) }).go(scheduler) // center.copy(position) } override def onMouseDown(event: MouseEvent): Unit = { this.intersections.headOption match { case Some(obj) => obj.`object`.position match { case p: Vector3 if p.equals(center) => super.onMouseDown(event) case p: Vector3 => moveTo(p) } case None => super.onMouseDown(event) } } }
Example 7
Source File: KafkaSpout.scala From Raphtory with Apache License 2.0 | 5 votes |
package com.raphtory.spouts import java.util import java.util.Properties import com.raphtory.core.components.Spout.SpoutTrait import org.apache.kafka.clients.consumer.KafkaConsumer import scala.collection.JavaConverters._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.duration.MILLISECONDS import scala.concurrent.duration.SECONDS import java.util.concurrent.LinkedBlockingQueue import akka.actor.Props import com.raphtory.core.components.Router.RouterManager import scala.concurrent.duration.Duration import scala.concurrent.duration._ import com.raphtory.core.utils.SchedulerUtil import scala.util.Random class KafkaSpout extends SpoutTrait { println("Starting kafka") var kafkaServer = System.getenv().getOrDefault("KAFKA_ADDRESS", "127.0.0.1").trim var kafkaIP = System.getenv().getOrDefault("KAFKA_PORT", "9092").trim var offset = System.getenv().getOrDefault("KAFKA_OFFSET", "earliest").trim val x = new Random().nextLong() var groupID = System.getenv().getOrDefault("KAFKA_GROUP", "group" + x).trim var topic = System.getenv().getOrDefault("KAFKA_TOPIC", "sample_topic").trim var restart = System.getenv().getOrDefault("RESTART_RATE", "10").trim val queue = new LinkedBlockingQueue[String] val props = new Properties() props.put("bootstrap.servers", s"$kafkaServer:$kafkaIP") props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") props.put("auto.offset.reset", offset) props.put("group.id", groupID) val consumer: KafkaConsumer[String, String] = new KafkaConsumer[String, String](props) consumer.subscribe(util.Arrays.asList(topic)) //val helper = context.system.actorOf(Props(new KafkaSpoutBackPressure(queue)), "Spout_Helper") protected def ProcessSpoutTask(message: Any): Unit = message match { case StartSpout => AllocateSpoutTask(Duration(1, MILLISECONDS), "newLine") case "newLine" => consumeFromKafka() case _ => println("message not recognized!") } def consumeFromKafka() = { //println("Consuming") val record = consumer.poll(java.time.Duration.ofMillis(5000)).asScala for (data <- record.iterator) { sendTuple(data.value()) //helper ! KafkaData(data.value()) } AllocateSpoutTask(Duration(restart.toInt, MILLISECONDS), "newLine") } } case class KafkaData(data:String) class KafkaSpoutBackPressure(queue:LinkedBlockingQueue[String]) extends SpoutTrait { var startingSpeed = System.getenv().getOrDefault("STARTING_SPEED", "1000").trim.toInt var increaseBy = System.getenv().getOrDefault("INCREASE_BY", "1000").trim.toInt override def preStart(): Unit = { super.preStart() SchedulerUtil.scheduleTask(initialDelay = 60 seconds, interval = 60 second, receiver = self, message = "increase") } override protected def ProcessSpoutTask(receivedMessage: Any): Unit = receivedMessage match { case StartSpout => AllocateSpoutTask(Duration(1, MILLISECONDS), "newLine") case KafkaData(data) => queue.put(data) case "newLine" => consumeFromQueue case "increase" => startingSpeed+=increaseBy case _ => println("message not recognized!") } def consumeFromQueue() = { for(i<-0 to startingSpeed/100){ if(!queue.isEmpty) { sendTuple(queue.take()) } } AllocateSpoutTask(Duration(10, MILLISECONDS), "newLine") } }
Example 8
Source File: GabKafkaSpout.scala From Raphtory with Apache License 2.0 | 5 votes |
package com.raphtory.examples.gab.actors import java.util import java.util.Properties import com.raphtory.core.components.Spout.SpoutTrait import org.apache.kafka.clients.consumer.KafkaConsumer import scala.collection.JavaConverters._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.duration.MILLISECONDS import scala.concurrent.duration.SECONDS import scala.util.Random class GabKafkaSpout extends SpoutTrait { val x = new Random().nextLong() val props = new Properties() props.put("bootstrap.servers", "moe.eecs.qmul.ac.uk:9092") props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") props.put("auto.offset.reset", "earliest") props.put("group.id", "group" + x) val consumer: KafkaConsumer[String, String] = new KafkaConsumer[String, String](props) consumer.subscribe(util.Arrays.asList("gabResortedGraph")) protected def ProcessSpoutTask(message: Any): Unit = message match { case StartSpout => AllocateSpoutTask(Duration(1, MILLISECONDS), "newLine") case "newLine" => consumeFromKafka() case _ => println("message not recognized!") } def consumeFromKafka() = { val record = consumer.poll(1000).asScala for (data <- record.iterator) sendTuple(data.value()) AllocateSpoutTask(Duration(1, MILLISECONDS), "newLine") } }
Example 9
Source File: rumourSpout.scala From Raphtory with Apache License 2.0 | 5 votes |
package com.raphtory.examples.twitterRumour import akka.actor.ActorContext import com.raphtory.core.components.Spout.SpoutTrait import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.duration.MILLISECONDS import scala.concurrent.duration.SECONDS import java.io.File class rumourSpout extends SpoutTrait { var directory = System.getenv().getOrDefault("PHEME_DIRECTORY", "/home/tsunade/qmul/datasets/pheme-rnr-dataset").trim var r_case = System.getenv().getOrDefault("RUMOUR_CASE", "/sydneysiege").trim val rpath = directory + r_case + "/rumours" val nrpath = directory + r_case + "/non-rumours" val max_r = 500 //Option(new File(rpath).list.size).getOrElse(0) val max_nr = 500 //Option(new File(nrpath).list.size).getOrElse(0) var rtweetCounter = 0 var nrtweetCounter = 0 //println("at spout....") override protected def ProcessSpoutTask(message: Any): Unit = // println(message) if ((rtweetCounter < max_r) || (nrtweetCounter < max_nr)) //println("inside if stm") message match { case StartSpout => AllocateSpoutTask(Duration(1, SECONDS), "rumourTweets") AllocateSpoutTask(Duration(1, SECONDS), "nonRumourTweets") case "rumourTweets" => if (rtweetCounter < max_r) { // println("sending ingest cmd r...") running(rpath, position = rtweetCounter, "R__") rtweetCounter += 1 context.system.scheduler.scheduleOnce(Duration(1, SECONDS), self, "rumourTweets") } case "nonRumourTweets" => if (nrtweetCounter < max_nr) { // println("sending ingest cmd nr...") running(nrpath, position = nrtweetCounter, "nR__") nrtweetCounter += 1 context.system.scheduler.scheduleOnce(Duration(1, SECONDS), self, "nonRumourTweets") } case _ => println("message not recognized!") } def ingestTweet(path: String, rnr: String): Unit = { // println("inside ingest....") val tweet_l = new File(path) if (tweet_l.exists && tweet_l.isDirectory) tweet_l.listFiles.filter(_.isFile).toList.map(_.toString).foreach { tw_path: String => sendTuple(rnr + tw_path) // println("running sendTuple...") } } def running(rpath: String, position: Int, rnr: String): Unit = { val rumours = new File(rpath) // println("inside runnign...") if (rumours.exists && rumours.isDirectory) { //println("inside runnign if...") val r_tweet = rumours.listFiles.toList.map(_.toString) ingestTweet(r_tweet(position) + "/source-tweet", rnr) ingestTweet(r_tweet(position) + "/reactions", rnr) } } }
Example 10
Source File: EthereumPostgresSpout.scala From Raphtory with Apache License 2.0 | 5 votes |
package com.raphtory.examples.blockchain.spouts import cats.effect.Blocker import cats.effect.IO import com.raphtory.core.components.Spout.SpoutTrait import doobie.implicits._ import doobie.util.ExecutionContexts import doobie.util.transactor.Transactor import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.duration.MILLISECONDS import scala.concurrent.duration.SECONDS class EthereumPostgresSpout extends SpoutTrait { var startBlock = System.getenv().getOrDefault("STARTING_BLOCK", "46147").trim.toInt //first block to have a transaction by default val batchSize = System.getenv().getOrDefault("BLOCK_BATCH_SIZE", "100").trim.toInt //number of blocks to pull each query val maxblock = System.getenv().getOrDefault("MAX_BLOCK", "8828337").trim.toInt //Maximum block in database to stop querying once this is reached val dbURL = System.getenv().getOrDefault("DB_URL", "jdbc:postgresql:ether").trim //db connection string, default is for local with db called ether val dbUSER = System.getenv().getOrDefault("DB_USER", "postgres").trim //db user defaults to postgres val dbPASSWORD = System.getenv().getOrDefault("DB_PASSWORD", "").trim //default no password // querying done with doobie wrapper for JDBC (https://tpolecat.github.io/doobie/) implicit val cs = IO.contextShift(ExecutionContexts.synchronous) val dbconnector = Transactor.fromDriverManager[IO]( "org.postgresql.Driver", dbURL, dbUSER, dbPASSWORD, Blocker.liftExecutionContext(ExecutionContexts.synchronous) ) override def ProcessSpoutTask(message: Any): Unit = message match { case StartSpout => AllocateSpoutTask(Duration(1, MILLISECONDS), "nextBatch") case "nextBatch" => running() case _ => println("message not recognized!") } protected def running(): Unit = { sql"select from_address, to_address, value,block_timestamp from transactions where block_number >= $startBlock AND block_number < ${startBlock + batchSize} " .query[ (String, String, String, String) ] //get the to,from,value and time for transactions within the set block batch .to[List] // ConnectionIO[List[String]] .transact(dbconnector) // IO[List[String]] .unsafeRunSync // List[String] .foreach(x => sendTuple(x.toString())) //send each transaction to the routers startBlock += batchSize //increment batch for the next query if (startBlock > maxblock) stop() //if we have reached the max block we stop querying the database AllocateSpoutTask(Duration(1, MILLISECONDS), "nextBatch") // line up the next batch } }
Example 11
Source File: EthereumGethSpout.scala From Raphtory with Apache License 2.0 | 5 votes |
package com.raphtory.examples.blockchain.spouts import java.net.InetAddress import java.util.NoSuchElementException import com.raphtory.core.components.Spout.SpoutTrait import com.raphtory.core.utils.Utils import com.raphtory.tests.EtherAPITest.baseRequest import com.raphtory.tests.EtherAPITest.currentBlock import com.raphtory.tests.EtherAPITest.request import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.duration.MILLISECONDS import scala.concurrent.duration.NANOSECONDS import scala.concurrent.duration.SECONDS import scala.language.postfixOps import scala.sys.process._ import scalaj.http.Http import scalaj.http.HttpRequest import spray.json._ import akka.http.scaladsl.unmarshalling.Unmarshal import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.stream.ActorMaterializer import spray.json.DefaultJsonProtocol._ import scala.concurrent.duration._ import scala.concurrent.Await case class EthResult(blockHash:Option[String],blockNumber:Option[String],from:Option[String],gas:Option[String],gasPrice:Option[String],hash:Option[String],input:Option[String],nonce:Option[String],r:Option[String],s:Option[String],to:Option[String],transactionIndex:Option[String],v:Option[String],value:Option[String]) case class EthTransaction(id:Option[String],jsonrpc:Option[String],result:EthResult) class EthereumGethSpout extends SpoutTrait { var currentBlock = System.getenv().getOrDefault("SPOUT_ETHEREUM_START_BLOCK_INDEX", "9014194").trim.toInt var highestBlock = System.getenv().getOrDefault("SPOUT_ETHEREUM_MAXIMUM_BLOCK_INDEX", "10026447").trim.toInt val nodeIP = System.getenv().getOrDefault("SPOUT_ETHEREUM_IP_ADDRESS", "127.0.0.1").trim val nodePort = System.getenv().getOrDefault("SPOUT_ETHEREUM_PORT", "8545").trim val baseRequest = requestBuilder() implicit val materializer = ActorMaterializer() implicit val EthFormat = jsonFormat14(EthResult) implicit val EthTransactionFormat = jsonFormat3(EthTransaction) if (nodeIP.matches(Utils.IPRegex)) println(s"Connecting to Ethereum RPC \n Address:$nodeIP \n Port:$nodePort") else println(s"Connecting to Ethereum RPC \n Address:${hostname2Ip(nodeIP)} \n Port:$nodePort") override protected def ProcessSpoutTask(message: Any): Unit = message match { case StartSpout => pullNextBlock() case "nextBlock" => pullNextBlock() } def pullNextBlock(): Unit = { if (currentBlock > highestBlock) return try { log.debug(s"Trying block $currentBlock") val transactionCountHex = executeRequest("eth_getBlockTransactionCountByNumber", "\"0x" + currentBlock.toHexString + "\"") val transactionCount = Integer.parseInt(transactionCountHex.fields("result").toString().drop(3).dropRight(1), 16) if(transactionCount>0){ var transactions = "[" for (i <- 0 until transactionCount) transactions = transactions + batchRequestBuilder("eth_getTransactionByBlockNumberAndIndex",s""""0x${currentBlock.toHexString}","0x${i.toHexString}"""")+"," val trasnactionBlock = executeBatchRequest(transactions.dropRight(1)+"]") val transList = trasnactionBlock.parseJson.convertTo[List[EthTransaction]] transList.foreach(t => { //try needed to ignore contracts //todo include them try{sendTuple(s"${t.result.blockNumber.get},${t.result.from.get},${t.result.to.get},${t.result.value.get}")} catch {case e:NoSuchElementException =>} }) } currentBlock += 1 AllocateSpoutTask(Duration(1, NANOSECONDS), "nextBlock") } catch { case e: NumberFormatException => AllocateSpoutTask(Duration(1, SECONDS), "nextBlock") case e: Exception => e.printStackTrace(); AllocateSpoutTask(Duration(1, SECONDS), "nextBlock") } } def batchRequestBuilder(command:String,params:String):String = s"""{"jsonrpc": "2.0", "id":"100", "method": "$command", "params": [$params]}""" def executeBatchRequest(data: String) = requestBatch(data).execute().body.toString def requestBatch(data: String): HttpRequest = baseRequest.postData(data) def requestBuilder() = if (nodeIP.matches(Utils.IPRegex)) Http("http://" + nodeIP + ":" + nodePort).header("content-type", "application/json") else Http("http://" + hostname2Ip(nodeIP) + ":" + nodePort).header("content-type", "application/json") def request(command: String, params: String = ""): HttpRequest = baseRequest.postData(s"""{"jsonrpc": "2.0", "id":"100", "method": "$command", "params": [$params]}""") def executeRequest(command: String, params: String = "") = request(command, params).execute().body.toString.parseJson.asJsObject def hostname2Ip(hostname: String): String = InetAddress.getByName(hostname).getHostAddress() }
Example 12
Source File: HttpUtil.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.analytics.util import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.RequestEntityAcceptance.Tolerated import akka.http.scaladsl.model.{HttpMethod, HttpRequest, HttpResponse} import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import akka.util.ByteString import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper} import com.typesafe.config.ConfigFactory import scala.concurrent.duration.{MILLISECONDS, _} import scala.concurrent.{Await, ExecutionContextExecutor, Future} object HttpUtil { private val mapper = new ObjectMapper() private val config = ConfigFactory.load private val ReadTimeout = FiniteDuration(config.getDuration("extract-index-from-es.read-timeout").toMillis, MILLISECONDS) // Elasticsearch uses the POST verb in some places where the request is actually idempotent. // Requests that use POST, but are known to be idempotent can use this method. // The presence of any non-idempotent request in-flight causes Akka to not retry, and that will tend result in // entire downloads failing more often. val SAFE_POST = HttpMethod( value = "POST", isSafe = true, isIdempotent = true, requestEntityAcceptance = Tolerated) def resultAsync(request: HttpRequest, action: String) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): Future[ByteString] = Http().singleRequest(request).map { case HttpResponse(status, _, entity, _) if status.isSuccess => entity.dataBytes .fold(ByteString.empty)(_ ++ _) .runWith(Sink.head) case HttpResponse(status, _, entity, _) => val message = Await.result(entity.toStrict(10.seconds).map(_.data), 10.seconds).utf8String throw new RuntimeException(s"HTTP request for $action failed. Status code: $status, message:$message") } .flatMap(identity) def result(request: HttpRequest, action: String, timeout: FiniteDuration = ReadTimeout) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): ByteString = Await.result(resultAsync(request, action), timeout) def jsonResult(request: HttpRequest, action: String, timeout: FiniteDuration = ReadTimeout) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): JsonNode = mapper.readTree(result(request, action, timeout).utf8String) def jsonResultAsync(request: HttpRequest, action: String) (implicit system: ActorSystem, executionContext: ExecutionContextExecutor, actorMaterializer: ActorMaterializer): Future[JsonNode] = resultAsync(request, action).map((bytes: ByteString) => mapper.readTree(bytes.utf8String)) }
Example 13
Source File: Settings.scala From reactive-lib with Apache License 2.0 | 5 votes |
package com.lightbend.rp.servicediscovery.scaladsl import akka.actor._ import com.typesafe.config.Config import java.net.URI import scala.collection.JavaConverters._ import scala.collection.immutable.Seq import scala.concurrent.duration.{ Duration, FiniteDuration, MILLISECONDS } final class Settings(system: ExtendedActorSystem) extends Extension { private val serviceDiscovery = system.settings.config.getConfig("com.lightbend.platform-tooling.service-discovery") val askTimeout: FiniteDuration = duration(serviceDiscovery, "ask-timeout") val externalServiceAddresses: Map[String, Seq[URI]] = { val data = serviceDiscovery.getObject("external-service-addresses") val config = data.toConfig data .keySet() .asScala .map(k => k -> config.getStringList(k).asScala.toVector.map(new URI(_))) .toMap } val externalServiceAddressLimit: Int = serviceDiscovery.getInt("external-service-address-limit") val retryDelays: Seq[FiniteDuration] = serviceDiscovery .getDurationList("retry-delays", MILLISECONDS) .asScala .toVector .map(Duration(_, MILLISECONDS)) private def duration(config: Config, key: String): FiniteDuration = Duration(config.getDuration(key, MILLISECONDS), MILLISECONDS) } object Settings extends ExtensionId[Settings] with ExtensionIdProvider { override def get(system: ActorSystem): Settings = super.get(system) override def lookup: Settings.type = Settings override def createExtension(system: ExtendedActorSystem): Settings = new Settings(system) }
Example 14
Source File: Vamp.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.bootstrap import akka.actor.ActorSystem import akka.util.Timeout import com.typesafe.config.ConfigFactory import io.vamp.common.Namespace import io.vamp.common.akka.Bootstrap import io.vamp.http_api.HttpApiBootstrap import scala.concurrent.{ ExecutionContext, Future } import scala.concurrent.duration.{ FiniteDuration, MILLISECONDS } trait Vamp extends VampApp { implicit val system: ActorSystem = ActorSystem("vamp") implicit val executionContext: ExecutionContext = system.dispatcher implicit val timeout: Timeout = Timeout(FiniteDuration(ConfigFactory.load().getDuration("vamp.bootstrap.timeout", MILLISECONDS), MILLISECONDS)) protected lazy val bootstraps = { implicit val namespace: Namespace = Namespace(ConfigFactory.load().getString("vamp.namespace")) List() :+ new LoggingBootstrap { lazy val logo: String = s""" |██╗ ██╗ █████╗ ███╗ ███╗██████╗ |██║ ██║██╔══██╗████╗ ████║██╔══██╗ |██║ ██║███████║██╔████╔██║██████╔╝ |╚██╗ ██╔╝██╔══██║██║╚██╔╝██║██╔═══╝ | ╚████╔╝ ██║ ██║██║ ╚═╝ ██║██║ | ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ | $version | by magnetic.io |""".stripMargin } :+ new KamonBootstrap :+ new ConfigurationBootstrap :+ new ClassProviderActorBootstrap :+ new ActorBootstrap(new HttpApiBootstrap :: Nil) } addShutdownBootstrapHook() startBootstraps() } trait VampApp extends App { protected implicit def system: ActorSystem protected implicit def executionContext: ExecutionContext protected def bootstraps: List[Bootstrap] def addShutdownBootstrapHook(): Unit = sys.addShutdownHook { val reversed = bootstraps.reverse reversed.tail.foldLeft[Future[Unit]](reversed.head.stop())((f, b) ⇒ f.flatMap(_ ⇒ b.stop())).map { _ ⇒ system.terminate() }.recover { case e: Throwable ⇒ e.printStackTrace() } } def startBootstraps(): Future[Unit] = { bootstraps.tail.foldLeft[Future[Unit]](bootstraps.head.start())((f, b) ⇒ f.flatMap(_ ⇒ b.start())).recover { case e: Throwable ⇒ e.printStackTrace() } } }