By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

What is causing the OutOfMemmoryError. The exception is thrown from the router actor. ForkJoinPool creates a new thread for every blocked one automatically. See, Blocking Needs Careful Management.

Here you may find why Await. Or you creating a lot of ActorSystem s, same page of akka documentation states:. An ActorSystem is a heavyweight structure that will allocate 1…N Threads, so create one per logical application.

Learn more. Asked 4 years, 9 months ago. Active 9 months ago. Viewed 3k times. While checking the throughput of spray api. OutOfMemoryError: unable to create new native thread at java. Rahul Dev Rahul Dev 1 1 gold badge 3 3 silver badges 10 10 bronze badges. Active Oldest Votes. Or you creating a lot of ActorSystem s, same page of akka documentation states: An ActorSystem is a heavyweight structure that will allocate 1…N Threads, so create one per logical application.

I was making Await. Now i removed that and made it to Future. But other actors have blocking calls such as REST calls. Will that be the issue. Had 2 actor systems in the application. Now made it to one and seems to be working. Can you please explain the change in behavior.The central idea of a spray-caching cache is to not store the actual values of type T themselves in the cache but rather corresponding Akka Futures, i. This approach has the advantage of nicely taking care of the thundering herds problem where many requests to a particular cache key e.

As soon as the first request completes all other ones complete as well. This minimizes processing time and server load for all requests. Apart from the Scala library see Current Versions chapter spray-caching depends on.

The Maven Repository chapter contains all the info about how to pull spray-caching into your classpath. Afterwards just import spray. All spray-caching cache implementations implement the Cache trait, which allows you to interact with the cache through nine methods:.

Note that the apply overloads require an implicit ExecutionContext to be in scope. They difference between the two only consists of whether they support time-based entry expiration or not. This cache implementation has a defined maximum number of entries it can store. After the maximum capacity is reached new entries cause old ones to be evicted in a last-recently-used manner, i.

This implementation has the same limited capacity behavior as the SimpleLruCache but in addition supports time-to-live as well as time-to-idle expiration.

The former provides an upper limit to the time period an entry is allowed to remain in the cache while the latter limits the maximum time an entry is kept without having been accessed. If both values are finite the time-to-live has to be strictly greater than the time-to-idle. Expired entries are only evicted upon next access or by being thrown out by the capacity constraintso they might prevent garbage collection of their values for longer than expected. Please check out the migration guide for help with the upgrade.

Commercial support is available from Lightbend. Dependencies Apart from the Scala library see Current Versions chapter spray-caching depends on spray-util concurrentlinkedhashmap akka-actor 2.

Installation The Maven Repository chapter contains all the info about how to pull spray-caching into your classpath. Note, that the generation expression is never run inside a Future with this overload. Instead, either, the cache already contains an entry for the key in which case the existing result is returned, or the generating expression is synchronously run to produce the value.

Returns None if the key has no corresponding cache entry. Returns the removed item if it was found and removed. Example import scala. Future import akka. ActorSystem import spray. Cache Implementations spray-caching comes with two implementations of the Cache interface, SimpleLruCache and ExpiringLruCacheboth featuring last-recently-used cache eviction semantics and both internally wrapping a concurrentlinkedhashmap. SimpleLruCache This cache implementation has a defined maximum number of entries it can store.

ExpiringLruCache This implementation has the same limited capacity behavior as the SimpleLruCache but in addition supports time-to-live as well as time-to-idle expiration.However, migration from one major version of a library to another is not an easy task.

Very often it requires you to spend some time reading the source code in order to figure out how to use certain features, as well as how to migrate existing logic. In order to have the latest Akka HTTP packages, all previous Spray dependencies now need to be replaced by the following:. Use Http class and pass your routes to the bindAndHandle method. For example:. One solution might be using next directive before working with form fields:. Users may also notice a lack of documentation for some cases.

Having said that, it would be a good idea to keep the above issues in mind during this process. Happy migration! In order to give you a better service Zalando uses cookies.

By continuing to browse the site you are agreeing to our use of cookies. Packages In order to have the latest Akka HTTP packages, all previous Spray dependencies now need to be replaced by the following: "com. Bind service, "0. See below: Before: Marshaller. Before: MediaType. Share the post Facebook Twitter LinkedIn.

Similar blog posts. Check out the blog.Akka HTTP is the successor for spray. The popular high-level routing DSL former spray-routing has mostly been kept but was made more consistent and simplified where possible. While underlyings have changed a lot, many of the high-level features and syntax of the routing DSL have only changed superficially so that code can hopefully be converted with little effort.

This has important consequences. Streaming support is needed to handle request and response entities or bodies in a streaming fashion, i. The same is valid for sending out request or response data entities. In spray, you could configure spray-can to send out HttpRequestPart and HttpResponsePart messages to receive a request or a response in a streaming fashion. The default case was for spray to collect the full entity in memory and send it out as a ByteString ByteString as part of the request or response entity object.

When you receive a HttpRequest HttpRequest on the server-side or an HttpResponse HttpResponsein the default case it will contain a streamed entity as the entity field which you are required to consume.

Otherwise, a connection might be stuck at least until timeouts kick in. In the implementation, Akka HTTP makes heavy use of streams as well with the occasional fallback to actors. The number of modules has been reduced. Instead, we support a light-weight replacement that models heterogeneous lists with tuples.

spray akka

The implicit magic in the background that powers directives will - in user code - work as before. All APIs are also available for Java. See everything under the akka. This has been done intentionally, because in Spray it was easy to forget to complete requests but the code would still compile.

The following article mentions a few ways for us to complete the request based on processing outside the controller: CodeMonkey blog - Actor per Request with Akka HTTP. The Spray-client pipeline was removed. One solution might be using next directive before working with form fields:.

From the creators of Akkaget technology enhancements, monitoring, and expert support with Lightbend Platform.

See Akka In Lightbend Platform. New module structure The number of modules has been reduced. Changes in Marshalling Marshaller. Was: Marshaller. Was: MediaType.

"pkh.breadedvilgefortz.pw v.s. Play Framework" by Yevgeny(Jim) Brikman at ScalaMatsuri 2014

Bind service, "0. More information here: Registering Custom Media Types changed from Spray in order not to rely on global state.

spray akka

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.Integration with spray-json Jackson is provided out of the box through the akka-http-spray-json akka-http-jackson module.

Integration with other JSON libraries are supported by the community. To make use of the support module for un marshalling from and to JSON with Jacksonadd a library dependency onto:. Use akka. Refer to this file in the sources for the complete example. RootJsonWriter respectively is available for.

How to build a REST api with Spray and Akka

To enable automatic support for un marshalling from and to JSON with spray-jsonadd a library dependency onto:. Next, provide a RootJsonFormat[T] for your type and bring it into scope. Check out the spray-json documentation for more info on how to do this. Once you have done this un marshalling between JSON and your type T should work nicely and transparently. In the above example the marshalling is handled by the implicitly provided JsonEntityStreamingSupportwhich is also used when building server-side streaming APIs.

You can also achieve the same more explicitly, by manually connecting the entity byte stream through a framing and then deserialization stage:. In the above example the JsonEntityStreamingSupport class is used to obtain the proper framing, though you could also pick the framing manually by using akka.

Framing or akka. This technique is simpler to use and often good enough rather than writing a fully streaming JSON parser which also is possible. By default, spray-json marshals your types to compact printed JSON by implicit conversion using CompactPrinteras defined in:.

Alternatively to marshal your types to pretty printed JSON, bring a PrettyPrinter in scope to perform implicit conversion. To learn more about how spray-json works please refer to its documentation. From the creators of Akkaget technology enhancements, monitoring, and expert support with Lightbend Platform.

See Akka In Lightbend Platform.

spray akka

Jackson; import akka. StatusCodes; import java. Map; import java. CompletableFuture; import static akka. OK, pet, Jackson. OK, thePet, Jackson. Directives import akka.

SprayJsonSupport import spray. You can also achieve the same more explicitly, by manually connecting the entity byte stream through a framing and then deserialization stage: Scala import MyJsonProtocol. EntityStreamingSupport import akka.Comment 3. I begin by constructing the core of our system. If I now wanted, I could write a simple application that starts our actors by mixing in the traits. I must be mindful of the initialization order, though. Having to worry about the order in which I mix in the traits is wholly unsatisfactory.

Rephrasing, I want a member whose value will be computed and remembered on first access, andsubsequent accesses will return the remembered value.

Migrating from Spray to Akka HTTP

The motivation was testing. The implementation of the actors is not particularly interesting. I will explore the common patterns of naming and structuring that I believe are useful; the implementation of the actors I leave to the readers.

Its purpose is to register the users in our system. In code, the structure is.

Subscribe to RSS

This structure allows me to tidy up the messages and perhaps some utility functions in the future from the actor itself. To the code, then. This is the entire body of our test. If you run the test, it will fail. It will complain about failing timeouts, namely assertion failed: timeout 3 seconds during expectMsgClass waiting for class core. Going back to our test, I write registration!

Register mkUser "". If you explore the! This feels like quite a handful, but you will see that the code is rather succinct and, of course, type safe.GitHub is home to over 40 million developers working together. Join them to grow your own development teams, manage permissions, and collaborate on projects.

An SBT plugin for dangerously fast development turnaround in Scala. A suite of scala libraries for building and consuming RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, testable. SBT template project for quickly getting started with spray-server.

A low-overhead, high-performance, fully async HTTP 1. Skip to content. Sign up. Type: All Select type. All Sources Forks Archived Mirrors. Select language. All Scala. Repositories spray-json A lightweight, clean and simple JSON implementation in Scala json scala json-serialization json-parser.

Scala Apache Scala 3, 11 0 0 Updated Dec 6, Scala 2, 71 14 Updated Feb 21, Scala 3 1 Updated Mar 24, Scala 3 8 0 0 Updated Oct 25, Scala 4 12 0 0 Updated Apr 12, Top languages Loading…. Most used topics Loading…. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window.