Glassfish gem - shared singleton


#1

Hi Everyone,
I’m currently using the glassfish gem (0.9.1) with jruby (1.1.6) to host
a
rails application. I’m wondering if there is a recommended/supported
way of
having a singleton shared among multiple jruby runtimes. It appears
that
each of the runtimes has its own child class loader (which makes sense)

  • so
    just naively requiring the jar file causes each runtime to have it’s own
    instance. Is there an easy way that I can get my singleton class loaded
    into the parent class loader before the runtimes get created?

Thanks,
Scott


#2

Not really… However, there are a few hack-ish things that you could
try.

Given that you are using gem, you might be able to modify the load
script to load your jar, where it should show up in the parent
classloader. I’m not sure if that would work at all, though.

You may also be able to do something Jave EE-ish and have a singleton
bean that you could access from all of the runtimes, but that would most
likely be painful and difficult.

Another possibility would be moving to threadsafe mode in Rails 2.2,
where there would always only be a single runtime, and you could have
your singleton easily (as long as you weren’t doing non-threadsafe
things with it, of course).

Scott Miller wrote:

Thanks,
Scott


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

Thanks for the response. That was going to be my follow up question:
how
confident are people with the new thread-safe mode? If it’s solid (in
rails
2.2.2), then I’d prefer to take that approach anyway (and save some
memory
as well).


#4

I should also mention additional benefits of running a single JRuby
instance:

  • we cut our memory footprint in half on each app server
  • reduced startup time

Ikai


#5

Good to hear - this is definitely my preferred option.
Thanks much,
Scott


#6

It’s solid. We’re running it on our production JRuby applications at
LinkedIn.

If you use MemCache, either enable threadsafe mode in memcache-client or
use
JRuby-MemCached wrapper
(http://github.com/ikai/jruby-memcache-client/tree/master).

Ikai