jruby / jruby-rack

Rack for JRuby and Java appservers
MIT License
396 stars 137 forks source link

Asynchronous processing in JRuby-Rack? #119

Open kidlab opened 11 years ago

kidlab commented 11 years ago

Does JRuby-Rack support asynchronous processing as in Java Servlet 3.0 API ?

As I know, we can create an async. servlet (http://developerlife.com/tutorials/?p=1437). Is there anyway we can do that with JRuby-Rack?

Thanks.

This issue seems to be lacking activity, you can change that by posting a bounty.

kares commented 11 years ago

Hi Man, I just realized it's really not possible with JRuby-Rack to do right now due the way how we handle responses.

It's doable but the "main" problem (at least for me) is how to do it - see there's no Rack standard way of returning an async response, I've seen catch(:async) / throw(:async) or returning negative status responses. I'll try to think of something but can not promise you when I'll get into it. Also it would be perfect to align this somehow with Rails 4.0 ActionController::Live since it would be a perfect fit, unfortunately Thread logic seemed hard-coded last time I've checked for streamed responses thus it might be a long run ...

kidlab commented 11 years ago

I have took a look at https://github.com/matadon/mizuno/blob/master/lib/mizuno/rack_servlet.rb. It's also using a "hacking-way" (catch :async) but it seems to integrate better with servlet container.

kares commented 11 years ago

You're right but it's a good fit, it's only unfortunate that it might break a Rack middleware or two. I thing I "hack" something quite similar into JRuby-Rack. Currently it requires you to write a custom servlet (or filter) to use it with JRuby-Rack (note that Mizuno does not use JRuby-Rack and even uses different env names for the servlet objects). Although it should be easy to write (in Ruby) and configure such servlet for Trinidad (which uses JRuby-Rack).

kidlab commented 11 years ago

Can I extend https://github.com/jruby/jruby-rack/blob/master/src/main/java/org/jruby/rack/RackServlet.java with asyncSupported then configure to use it in web.xml? It sounds crazy?

kares commented 11 years ago

I'm sorry for mis-guiding you that won't be enough for JRuby-Rack since you need to dig deeper the source to find the response handling code (as opposed to mizuno's response handling in the servlet). Here's an old attempt I did but gave up due some middleware issuers it has been rebased to match the current master thus do not expet it to work but it shall show you where to get started (hope it helps): https://github.com/kares/jruby-rack/commit/2f3a8b0cd0928813ddbd0d93a0453e0b04e185bb (async-support branch of my fork)

jheintz commented 11 years ago

Me too! I found this issue while search for jruby/rails async servlet integration. I'm not sure that I can do anything to help, but getting async support for long file upload/downloads in our app will be very good for our thread count and memory use.

grinser commented 11 years ago

I'm building an API with the wonderful Grape framework that pushes requests to a message queue and sends back responses from the queue using the AMQP gem. The AMQP gem does requests asynchronously, hence the only working solution I found using JRuby was Mizuno. Therefore it would be awesome if you could include async support in jruby-rack soon somehow.