# JRuby Rack question....is the java request + session available globally?

**URL:** <https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278>\
**Category:** JRuby\
**Created:** [October 6, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278 "2008-10-06T21:18:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 6, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/1 "2008-10-06T21:18:43Z")

</div>

Hi All-

I’ve got a rails app I’m converting to jruby/warbler that uses Memcache  
to  
cache some web service calls. I’d like to remove that dependency to  
Memcache.

To that end, I’m trying to change the cache location from memcache to  
the  
java session in a rails plugin I’ve written. Here is the relevant code:  
def cached( key, &block )  
key="#{key}"  
request = java.servlet\_request  
session = request.getSession()  
$servlet\_context.log(“session: #{session}”)  
cached\_obj = session.getAttribute( key )  
$servlet\_context.log(“cached object: #{cached\_obj}”)  
if(cached\_obj.nil? || cached\_obj[:time].nil? || (cached\_obj[:time] \>  
(Time.now - 5.minutes)))  
cached\_obj = {:time=\>Time.now, :data=\>block.call }  
#@CACHE.add key, cached\_obj, 300  
session[key] = cached\_obj  
end  
cached\_obj[:data]  
end

The line it has issues with is " cached\_obj = session.getAttribute( key  
)".  
I get this error: ArgumentError (wrong number of arguments (1 for 0))

Am I correct to assume that jruby rack makes the java request and  
session  
globally available? If so, what am I doing wrong?

thanks for any suggestions  
Mike Kohout

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 7, 2008, 1:08am UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/2 "2008-10-07T01:08:47Z")

</div>

On Mon, Oct 6, 2008 at 2:18 PM, Michael K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> request = java.servlet_request
> cached_obj[:data]
> 
> ```
> 
> end
> 
> The line it has issues with is " cached\_obj = session.getAttribute( key )".  
> I get this error: ArgumentError (wrong number of arguments (1 for 0))
> 
> Am I correct to assume that jruby rack makes the java request and session  
> globally available? If so, what am I doing wrong?

Request and session actually are _not_ globally available, but the  
servlet context is, as you have above. You should be able to use the  
$servlet\_context.get/setAttribute as a in-memory cache store.

The alternative is to make the servlet request and/or session  
parameters to the #cached method, if you need a per-user session  
cache.

Cheers,  
/Nick

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 7, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/3 "2008-10-07T17:12:57Z")

</div>

The servlet\_request call will fail if you call it inside view files (or  
indirectly, calling your cached method inside views). You can fix this  
making the servlet\_request method available inside views as a helper  
method:  
class ApplicationController \< ActionController::Base

# …

helper\_method :servlet\_request

# …

end

On Mon, Oct 6, 2008 at 4:18 PM, Michael K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> request = java.servlet_request
> cached_obj[:data]
> 
> ```

–  
Fabio K.

> **[Fabio Kung](https://fabiokung.com/)**
>
> software, programming, computers, technology

Caelum - Ensino e InovaÃ§Ã£o

> **[Da Caelum à Alura: conheça a nossa jornada e história](https://www.alura.com.br/artigos/historia-da-caelum)**
>
> Conheça a história da Caelum e da jornada até a Alura e o nosso ecossistema

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 7, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/4 "2008-10-07T18:26:47Z")

</div>

On Tue, Oct 7, 2008 at 10:25 AM, Michael K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Nick, Fabio, thanks for the suggestions.
> 
> My biggest trouble is that the web service I’m calling is called in Models  
> and Controllers(it’s an access control thing), so I’d rather avoid passing  
> the session around averywhere.
> 
> This is more of a ruby question(and shows a bit of greenness), but is there  
> a way to bind the session or request to a dynamically scoped variable, so  
> that it’s bound in the init method of the controller (and visible from a  
> plugin)?

Ruby doesn’t have dynamically-scoped variables, if that’s what you’re  
thinking (a variable created in a parent stack frame visible down the  
call stack). With Fabio’s suggestion, the servlet\_request method will  
be available in controllers and views. You’ll have to pass it as a  
parameter into model methods to use it there.

/Nick

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 7, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/5 "2008-10-07T19:46:35Z")

</div>

Perhaps, it would be better to pass what you want from the session,  
instead  
of passing the session itself.

On Tue, Oct 7, 2008 at 1:19 PM, Nick S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > This is more of a ruby question(and shows a bit of greenness), but is
> 
> /Nick
> 
> * * *
> 
> To unsubscribe from this list, please visit:
> 
> [http://xircles.codehaus.org/manage\_email](http://xircles.codehaus.org/manage_email)

–  
Fabio K.

> **[Fabio Kung](https://fabiokung.com/)**
>
> software, programming, computers, technology

Caelum - Ensino e InovaÃ§Ã£o

> **[Da Caelum à Alura: conheça a nossa jornada e história](https://www.alura.com.br/artigos/historia-da-caelum)**
>
> Conheça a história da Caelum e da jornada até a Alura e o nosso ecossistema

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 7, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/6 "2008-10-07T17:36:02Z")

</div>

Nick, Fabio, thanks for the suggestions.

My biggest trouble is that the web service I’m calling is called in  
Models  
and Controllers(it’s an access control thing), so I’d rather avoid  
passing  
the session around averywhere.

This is more of a ruby question(and shows a bit of greenness), but is  
there  
a way to bind the session or request to a dynamically scoped variable,  
so  
that it’s bound in the init method of the controller (and visible from a  
plugin)?

thanks  
Mike Kohout

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 8, 2008, 12:39am UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/7 "2008-10-08T00:39:36Z")

</div>

Thanks for your help, everybody.

Rather than change tons of code, for now, I changed things to just store  
everything inside a static map…the performance kinda sucks, but it  
keeps  
the code from any external dependencies.

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 7, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/jruby-rack-question-is-the-java-request-session-available-globally/150278/8 "2008-10-07T19:50:42Z")

</div>

On Mon, Oct 6, 2008 at 8:18 PM, Michael K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi All-
> 
> I’ve got a rails app I’m converting to jruby/warbler that uses Memcache to  
> cache some web service calls. I’d like to remove that dependency to  
> Memcache.

I don’t know if this is relevant, but I read about this yesterday:

> **[JGroups Implementation of Memcached Supports Failover and JMX](https://www.infoq.com/news/2008/10/jgroups-memcached/)**
>
> Memcached is a distributed memory object caching system used in dynamic web applications to alleviate database load. Bela Ban at JBoss recently wrote a JGroups-based implementation of memcached which allows Java clients to access it directly. The new...

–  
Rasputnik :: Jack of All Trades - Master of Nuns  
[http://number9.hellooperator.net/](http://number9.hellooperator.net/)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```
