Calling Blocking Java Library from Spray Route

28 views
Skip to first unread message

Kevin Meredith

unread,
Dec 8, 2015, 10:34:08 AM12/8/15
to spray.io User List

From my Scala code, I'm calling a Java library that blocks to download a document & parse it. Let's assume it's unreasonable to re-write the library.


The scala method's signature is: `def f: Int` (let's say that it downloads the library & returns an integer for failure or success - not a very good design, I'd say, but let's go with it).


Since the Java library is blocking, is there any value in wrapping this synchronous operation with `Future`'s? In particular, I'm thinking about how I'm going to call this `f` method from my `spray http` web service.


My concern is that, when I call this method in a spray route, will the web service only be able to handle 1 request per time if the java library is blocking?

Age Mooij

unread,
Dec 8, 2015, 11:18:52 AM12/8/15
to spray...@googlegroups.com
If you can't get rid of the blocking call by replacing the library with a better one, you can wrap the blocking stuff in bulkheaded async code, like simple Futures or actors. That's what most people will do. The only thing to watch out for is making sure you don't exhaust the thread pool so I'd recommend running blocking code in its own bulkheaded Akka dispatcher.

Hope this helps
Age


--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/6098beba-52ed-4e5e-8987-a7c81e365894%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Meredith

unread,
Mar 16, 2016, 11:59:06 PM3/16/16
to spray.io User List
hi Age -

could you please recommend any reading on bulkheaded dispatcher? or maybe even some code demonstrating how to address my situation with blocking?

thanks

Reply all
Reply to author
Forward
0 new messages