for people in hurry, here is the code and some steps to run few demo samples .
disclaimer: i am still learning play 2.0, please point to me if something is incorrect.
Tips, Expertise, Articles and Advice from the Pro's for Your Website or Blog to Succeed
for people in hurry, here is the code and some steps to run few demo samples .
disclaimer: i am still learning play 2.0, please point to me if something is incorrect.
Let me guess, you are having your Scala interview scheduled and you are here to know what's in store for you in the interview. Well.....that would be the probable reason you opened this blog. So, without further ado, I'll start the topic.
Scala is a high-level language, statically typed language that supports OOPS concepts as well as functional programming concepts. The language Scala is written in Java(optional).
Many different databases are used at Adform, each tailored for specific requirements, but what is common for these use cases is the necessity for a consistent interchange of data between these data stores. It’s a tedious task to keep the origin of that data and its copies consistent manually, not to mention that with a sufficiently large number of multiplications the origin may not be the source of truth anymore. The need for having its own copy of data is also dictated by the necessity of loose coupling and performance. It wouldn’t be practical to be constantly impacted by every change made in the source system. The answer here is an event-based architecture which allows to keep every change consistent and provides us with the possibility of restoring the sequence of changes related to particular entities. For those reasons, the decision was made to use the publisher/subscriber model. MongoDB’s change streams saved the day, finally letting us say farewell to much more complex oplog tailing.
As of version 3.6 MongoDB offers change data capture implementation named as change streams. It allows us to follow every modification made to an entire database or chosen set of collections. Previous versions already offered some solution to that problem by means of oplog (operation log) mechanism but tailing it directly had serious drawbacks, especially huge traffic caused by iteration overall changes to all collections and lack of reliable API allowing to resume tracking after any interruption. Change streams solve these issues by hiding oplog’s nook and crannies from us behind refined API interoperable with reactive streams implementations.
All the world's a stage,
And all the men and women merely players.
-Shakespeare
This quote from William Shakespeare's pastoral comedy As You Like It, can be very helpful when describing Actor Model. In this post, we will see what an Actor Model is and how you can use it to build concurrent, distributed, and resilient applications.
Reactive Architecture aims to provide software that remains responsive in all situations. Reactive Systems build user confidence by ensuring that the application is available whenever the users need it in all conditions.
The Reactive Manifesto is a document that was authored by Jonas Boner, Dave Farley, Roland Kuhn and Martin Thompson. The Manifesto was created in response to companies trying to cope with changes in the software landscape. Multiple groups or companies independently developed similar patterns for solving similar solutions. So aspects of a Reactive Systems were previously individually recognized by these groups. So the Manifesto attempts to bring all of these common ideas into a single unified set of principles, known as Reactive Principles.
Java comes with a built-in multi-threading model based on shared data and locks. To use this model, you decide what data will be shared by multiple threads and mark as “synchronized” sections of the code that access the shared data.
It also provides a locking mechanism to ensure that only one thread can access the shared data at a time. Lock operations remove possibilities for race conditions but simultaneously add possibilities for deadlocks. In Scala, you can still use Java threads, but the “Actor model” is the preferred approach for concurrency.
“Reactive Streams”, whenever we come across these words, there are two things that come to our mind. The first is asynchronous stream processing and the second is non-blocking backpressure. In this blog, we are going to learn about the latter part.
According to the English dictionary, back pressure means “Resistance or force opposing the desired flow of fluid through pipes”. To define it in our software context, in place of the flow of fluid we can say the flow of data “Resistance or force opposing the desired flow of data through software”
You can find this article in video form on YouTube or embedded below.
val akkaVersion = "2.5.26"
val akkaHttpVersion = "10.1.11"
libraryDependencies ++= Seq(
// akka streams
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
// akka http
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
)
xxxxxxxxxx
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher
application/x-www-form-urlencoded
.
xxxxxxxxxx
val source =
"""
|object SimpleApp {
| val aField = 2
|
| def aMethod(x: Int) = x + 1
|
| def main(args: Array[String]) = {
| println(aMethod(aField))
| }
|}
""".stripMargin
xxxxxxxxxx
val request = HttpRequest(
method = HttpMethods.POST,
uri = "http://markup.su/api/highlighter",
entity = HttpEntity(
ContentTypes.`application/x-www-form-urlencoded`,
s"source=${URLEncoder.encode(source.trim, "UTF-8")}&language=Scala&theme=Sunburst"
)
)
HttpRequest
contains the HTTP method (POST in our case). The URI and a payload in the form of an
HttpEntity
. We specify the content type per the description specified in the API — notice the backticks for the name of the field — and the actual string we want to send, as described by the API. In practice, you can send other strings, like JSON — I'll show you how to auto-convert your data types to JSON auto-magically in another article.
xxxxxxxxxx
def simpleRequest() = {
val responseFuture = Http().singleRequest(request)
responseFuture.flatMap(_.entity.toStrict(2 seconds)).map(_.data.utf8String).foreach(println)
}
singleRequest
method. You obtain a Future containing an HTTP response, which we can then unpack. We use its entity (= its payload) and convert it to a strict entity, meaning that we take its whole content in memory. We then take its data, which is a sequence of bytes, and convert that to a string. And we're done.
xxxxxxxxxx
def highlightCode(myCode: String): Future[String] = {
val responseFuture = Http().singleRequest(
HttpRequest(
method = HttpMethods.POST,
uri = "http://markup.su/api/highlighter",
entity = HttpEntity(
ContentTypes.`application/x-www-form-urlencoded`,
s"source=${URLEncoder.encode(myCode.trim, "UTF-8")}&language=Scala&theme=Sunburst"
)
)
)
responseFuture
.flatMap(_.entity.toStrict(2 seconds))
.map(_.data.utf8String)
}
This article was first published on the Knoldus blog.
Akka cluster provides a fault-tolerant, decentralized, peer-to-peer cluster membership service with no single point of failure or single point of bottleneck. It does this using gossip protocol and an automatic failure detector.
This blog will show you how records can be streamed to Kafka using Akka Streams with Alpakka. Alpakka is an open source project that provides a number of connectors, and in this blog, we will use the Alpakka connector for Kafka.
Before digging into it further, you can read more about Kafka here.
I’m a Java/Scala/Groovy guy, that’s not a mystery. Some people, especially the youngsters, roll their eyes when they learn about it. Old fashioned, ineffective when compared to modern languages, and doomed — that’s basically what I hear all the time. But is that really the case?
Yes, the base constructs and constraints that Java offers are old fashioned and rely on how code was written 20 years ago. Programming languages are not created in a sterilized bubble. Developers’ habits and needs are central to language design, and while inventors try to infuse innovation into it, it’d be dumb to ignore how people will actually use it. The first milestone for Java dates back in 1995, and while some of its foundational blocks looked visionary for at least 10 years, it is pretty obvious that
29
24 years later… not so much.
Akka is a toolkit for designing scalable, resilient systems that span processor cores and networks. Akka allows you to focus on meeting business needs instead of writing low-level code to provide reliable behavior, fault tolerance, and high performance.
Akka actor can have state but it’s lost when the actor is shutdown or crashed. Fortunately, we can persist actor state using Akka Persistence, which is one of Akka extensions.