RxScala Observable never runs - scala

With the following build.sbt:
name := "blah"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies ++= Seq("io.reactivex" % "rxscala_2.11" % "0.24.1", "org.scalaj" %% "scalaj-http" % "1.1.4")
and this code:
import rx.lang.scala.Observable
import scala.concurrent.duration._
import scala.language.postfixOps
object Main {
def main(args: Array[String]): Unit = {
println("Ready?")
val o = Observable.interval(200 millis).take(5)
o.subscribe(n => println(s"n = ${n}"))
}
}
When I run it, all that's printed is Ready?; I see no n = ... at all.
I run using sbt run; it's built using Scala 2.6.11 and RxScala 0.24.1, as well as sbt 0.13. Any ideas?

The problem is that your program exits before o fires. Try the following code:
import rx.lang.scala.Observable
import scala.concurrent.duration._
import scala.language.postfixOps
object Main {
def main(args: Array[String]): Unit = {
println("Ready?")
val o = Observable.interval(200 millis).take(5)
o.subscribe(n => println(s"n = ${n}"))
Thread.sleep(5000)
}
}
Alternatively you can replace Thread.sleep with o.toBlocking.last, which cannot return before o terminates.

Related

how to start server/client grpc using scalapb on spark?

i have a problem about running server/client using ScalaPB on spark.
its totally work fine while I running my code using "sbt run". i want running this code using spark coz next ill import my spark model to predict some label. but while I submit my jar to spark, they give me error like this.
Exception in thread "main" io.grpc.ManagedChannelProvider$ProviderNotFoundException:
No functional server found. Try adding a dependency on the grpc-netty artifact
this is my build.sbt
scalaVersion := "2.11.7"
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)
val scalapbVersion =
scalapb.compiler.Version.scalapbVersion
val grpcJavaVersion =
scalapb.compiler.Version.grpcJavaVersion
libraryDependencies ++= Seq(
// protobuf
"com.thesamet.scalapb" %% "scalapb-runtime" % scalapbVersion % "protobuf",
//for grpc
"io.grpc" % "grpc-netty" % grpcJavaVersion ,
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapbVersion
)
assemblyMergeStrategy in assembly := {
case PathList("META-INF", xs # _*) => MergeStrategy.discard
case x => MergeStrategy.first
}
using shade still not work
assemblyShadeRules in assembly := Seq(ShadeRule.rename("com.google.**" -> "shadegoogle.#1").inAll)
and this my main
import java.util.logging.Logger
import io.grpc.{Server, ServerBuilder}
import org.apache.spark.ml.tuning.CrossValidatorModel
import org.apache.spark.sql.SparkSession
import testproto.test.{Email, EmailLabel, RouteGuideGrpc}
import scala.concurrent.{ExecutionContext, Future}
object HelloWorldServer {
private val logger = Logger.getLogger(classOf[HelloWorldServer].getName)
def main(args: Array[String]): Unit = {
val server = new HelloWorldServer(ExecutionContext.global)
server.start()
server.blockUntilShutdown()
}
private val port = 50051
}
class HelloWorldServer(executionContext: ExecutionContext) {
self =>
private[this] var server: Server = null
private def start(): Unit = {
server = ServerBuilder.forPort(HelloWorldServer.port).addService(RouteGuideGrpc.bindService(new RouteGuideImpl, executionContext)).build.start
HelloWorldServer.logger.info("Server started, listening on " + HelloWorldServer.port)
sys.addShutdownHook {
System.err.println("*** shutting down gRPC server since JVM is shutting down")
self.stop()
System.err.println("*** server shut down")
}
}
private def stop(): Unit = {
if (server != null) {
server.shutdown()
}
}
private def blockUntilShutdown(): Unit = {
if (server != null) {
server.awaitTermination()
}
}
private class RouteGuideImpl extends RouteGuideGrpc.RouteGuide {
override def getLabel(request: Email): Future[EmailLabel] = {
val replay = EmailLabel(emailId = request.emailId, label = "aaaaa")
Future.successful(replay)
}
}
}
thanks
It looks like grpc-netty is not found when an uber jar is made. Instead of using ServerBuilder, change your code to use io.grpc.netty.NettyServerBuilder.

Exception while using Play WS to access a REST WebService

I want to use the Play WS Standalone (outside a Play Project) to call a simple Hello World RESTfull Web Service.
I'm Using an SBT Poject in Intelij IDEA whith this configuration :
name := "ScalaSbtProject"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies += "com.typesafe.play" % "play-ws_2.11" % "2.5.10"
Here's my code :
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.ahc.AhcWSClient
import scala.util.{Failure, Success}
object Test {
def main(args: Array[String]): Unit = {
import scala.concurrent.ExecutionContext.Implicits._
implicit val actor = ActorSystem()
implicit val materializer = ActorMaterializer()
val wsClient = AhcWSClient()
val response = wsClient
.url("http://localhost:8080/WebService_war_exploded/service/test")
.get()
response.onComplete {
case Success(re) => { println(re.body)}
case Failure(e) => { e.getStackTrace.foreach(println)}
}
wsClient.close()
}
}
which throws this Exception :
Response Is Failed :
http://localhost:8080
org.asynchttpclient.netty.channel.NettyConnectListener.onFailure(NettyConnectListener.java:160)
org.asynchttpclient.netty.request.NettyChannelConnector$1.onFailure(NettyChannelConnector.java:103)
org.asynchttpclient.netty.SimpleChannelFutureListener.operationComplete(SimpleChannelFutureListener.java:28)
org.asynchttpclient.netty.SimpleChannelFutureListener.operationComplete(SimpleChannelFutureListener.java:20)
io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:514)
io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:507)
io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:486)
io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:427)
io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:129)
io.netty.channel.nio.AbstractNioChannel.doClose(AbstractNioChannel.java:458)
io.netty.channel.socket.nio.NioSocketChannel.doClose(NioSocketChannel.java:235)
io.netty.channel.AbstractChannel$AbstractUnsafe.doClose0(AbstractChannel.java:632)
io.netty.channel.AbstractChannel$AbstractUnsafe.close(AbstractChannel.java:611)
io.netty.channel.AbstractChannel$AbstractUnsafe.close(AbstractChannel.java:554)
io.netty.channel.nio.NioEventLoop.closeAll(NioEventLoop.java:637)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:406)
io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140)
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
java.lang.Thread.run(Thread.java:745)

Get content from Akka ResponseEntity in Scala

I do a GET HTTP call to a rest service which returns a json. I would like to parse the json to a scala object but here I got stuck. I am using the Akka api and I can't manage to retrieve the content from the Akka's ResponseEntity
Here is my sbt file:
name := "ScalaHttp"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++={
val akkaV = "2.4.5"
Seq(
"com.typesafe.akka" %% "akka-http-core" % akkaV,
"com.typesafe.play" %% "play-json" % "2.4.0-M3"
)
}
And here is the app
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.stream.ActorMaterializer
import scala.concurrent.ExecutionContext.Implicits.global
object Sender {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
Http().singleRequest(HttpRequest(uri = "http://declinators.com/declinator?noun=komunikacja")) foreach {
y => println(y.entity)
println(y.entity.getContentType())
println(y.entity.contentType)
}
}
}
This prints:
HttpEntity.Strict(application/json,{"nominative":"komunikacja","genitive":"komunikacji","dative":"komunikacji","accusative":"komunikacjÄ™","instrumental":"komunikacjÄ…","locative":"komunikacji","vocative":"komunikacjo"})
application/json
application/json
Here come the questions:
1. Why ResponseEntity supplies getContentType() and contentType()? They return the same thing.
2. Getting the contentyType is easy, you have two ways for doing it, but how can I get the content itself, so I can play (i.e. parse it using play) with the json!
You can use entity.data.toString for Binary content type, or the following code piece
data.decodeString(nb.charset.value)
Please follow the HttpEntity.Strict.toString implementation here for detail:
https://github.com/akka/akka/blob/master/akka-http-core/src/main/scala/akka/http/scaladsl/model/HttpEntity.scala#L316

scala: delimited continuation issues

Here is a code I have problem to compile.
My question is very simple.
How to fix compilation error?
I did try to run this from sbt and from command prompt.
SBT configuration below. I did add references to continuation plugin!
But looks like this is not helping!
See error below.
I've got this from scaladocs: http://www.scala-lang.org/files/archive/api/2.11.8/scala-continuations-library/#scala.util.continuations.package
object MainApp
{
def main(args: Array[String]): Unit =
{
import scala.util.continuations._
val uuidGen : String = "UniqueValue"
def ask(prompt: String): Int #cps[Unit] =
shift {
k: (Int => Unit) => {
val id = uuidGen
printf("%s\nrespond with: submit(0x%x, ...)\n", prompt, id)
}
}
def go =
reset {
println("Welcome!")
val first = ask("Please give me a number")
val second = ask("Please enter another number")
printf("The sum of your numbers is: %d\n", first + second)
}
go
}
}
Sbt configuration for the refrences
name := """scala-testing"""
version := "0.1.0"
scalaVersion := "2.11.2"
autoCompilerPlugins := true
addCompilerPlugin(
"org.scala-lang.plugins" % "scala-continuations-plugin_2.11.6" % "1.0.2")
libraryDependencies +=
"org.scala-lang.plugins" %% "scala-continuations-library" % "1.0.2"
fork in run := true

Set a limit to scala.rx timer

Here is an example using Timer from scala.rx:
package tutorial.webapp
import akka.actor.ActorSystem
import rx.core.{Rx, Var}
import rx._
import rx.ops._
import scala.concurrent.Promise
import scala.concurrent.duration._
import scala.scalajs.js.JSApp
import scala.scalajs.js.annotation.JSExport
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Created by IDEA on 29/10/15.
*/
object RxAddtionalOps extends JSApp {
#JSExport
override def main(): Unit = {
timer1
}
def timer1: Unit = {
implicit val scheduler = new DomScheduler
val t = Timer(100 millis)
var count = 0
val o = Obs(t){
count = count + 1
println(count)
}
}
}
When you run runMain tutorial.webapp.RxAddtionalOps from sbt, the console will be indefinitely blocked. Can I set a limit to the timer? For example, to make it stop emitting events in 2 minutes.
First of all, Scala is a language for express common programming patterns in a concise, elegant, and type-safe way. So keep your work tidy!
Therefore
import akka.actor.ActorSystem
import rx.core.{Rx, Var}
import rx._
import scala.concurrent.Promise
is a lot of unnecessary noise. And if the target is a JavaScript platform, the Actor system is not available yet, maybe over couple of years.
Why should you fire runMain tutorial.webapp.RxAddtionalOps in sbt, while a simple run command will do?
I used the Timer.kill() method to terminate the execution after a limited time:
package tutorial.webapp
import rx.Obs
import rx.ops.{DomScheduler, Timer}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.scalajs.js.JSApp
object RxAddtionalOps extends JSApp {
val executionStart = scala.compat.Platform.currentTime
def main(): Unit = {
timer1()
}
def timer1() = {
implicit val scheduler = new DomScheduler
val t = Timer(100 millis)
var count = 0
Obs(t) {
count += 1
println(count)
if (count >= 19) {
t.kill()
println(s"Successfully completed without errors. [within ${
scala.compat.Platform.currentTime - executionStart
} ms]")
}
}
}
}
Since it is actually a headless phantom or rhino environment (depending on your build.sbt config) there can be no user initiated interrupt processed.
For completeness here the build.sbt file:
name := "RxAddtionalOps"
version := "1.0"
scalaVersion := "2.11.7"
enablePlugins(ScalaJSPlugin)
scalacOptions ++= Seq("-unchecked", "-deprecation","-feature")
libraryDependencies ++= Seq(
"com.lihaoyi" %%% "scalarx" % "0.2.8",
"org.scala-js" %% "scalajs-library" % "0.6.5"
)