Drop in clustering for Guice Singletons

59 views
Skip to first unread message

tgau...@gmail.com

unread,
Mar 12, 2008, 4:42:38 AM3/12/08
to google-guice
Hello. I just started playing with Guice - it seemed like a very
interesting technology to learn.

Being from Terracotta, my first thought is always "how do I cluster
this thing". Well since we have a way to make singleton Spring beans
cluster wide singletons, I thought maybe I could do the same in Guice?

After about 2 hours, I have a working demonstration! The demonstration
shows that by setting the Injector as a Terracotta root, singleton
beans that are instantiated in one VM are available to other VMs!

So, my demo app looks like this:

JVM1:
$ mvn tc:run
[INFO] [node] Called go 0 times.

JVM 2:
$ mvn tc:run
[INFO] [node] Called go 1 times.

Here's all the code. Basically Main bootstraps an Injector. I have
two classes, Foo and Bar. Foo depends on Bar. Bar is a simple
counter, showing that it is indeed clustered in the preceding demo.

Here's all the code in the demo:

--------------------------
public class Main
{
private static Injector injector = Guice.createInjector(new
MyModule());

private static class MyModule extends AbstractModule
{
protected void configure()
{
bind(Foo.class).in(Scopes.SINGLETON);
bind(Bar.class).in(Scopes.SINGLETON);
}
}

public static void main(String[] args)
{
Foo foo = injector.getInstance(Foo.class);
foo.go();
}
}


----------------------------------------------
Foo is declared like this:

import com.google.inject.Inject;

public class Foo
{
private Bar bar;

@Inject
public Foo(Bar bar)
{
this.bar = bar;
}

public void go()
{
bar.go();
}
}

----------------------------------------------
And Bar is like this:

public class Bar
{
int count = 0;

public synchronized void go()
{
System.out.println("Called go " + count + " times.");
count++;
}
}

tgau...@gmail.com

unread,
Mar 12, 2008, 2:29:31 PM3/12/08
to google-guice
I checked everything in to our Terracotta Forge labs. It's a self
contained demo, all you need is Maven.

I just checked in tim-guice.

$ svn co http://svn.terracotta.org/svn/forge/projects/labs/tim-guice
$ cd tim-guice
$ mvn clean install
$ cd sample
$ mvn tc:start
$ mvn tc:run
[INFO] [node] Called go 0 times.
$ mvn tc:run
[INFO] [node] Called go 1 times.

eu

unread,
Mar 12, 2008, 11:37:18 PM3/12/08
to google-guice

To keep spirit of Guice it would make sense to use annotations to
mark clustered injector instance.

It would be also really neat to resolve classes that need to be
instrumented using annotations, or better discover them automatically
when bindings are created (somewhat similar how it is done for
Terracotta integration for Spring).

regards,
Eugene Kuleshov


On Mar 12, 2:29 pm, "tgaut...@gmail.com" <tgaut...@gmail.com> wrote:
> I checked everything in to our Terracotta Forge labs. It's a self
> contained demo, all you need is Maven.
>
> I just checked in tim-guice.
>
> $ svn cohttp://svn.terracotta.org/svn/forge/projects/labs/tim-guice

tgau...@gmail.com

unread,
Mar 13, 2008, 3:34:58 AM3/13/08
to google-guice
Good point re annotations. The core TIM I built shouldn't have that
in it - as the Injector will live in the users main code somewhere,
and so anything having to do with the injector would lie in the main
tc-config, not the TIM (sorry everyone, talking about the guts of
Terracotta Integration Modules here)
j
What you ask for is already entirely possible using the tim-
annotations project. In the sample app, just add the tim-annotations
to the modules in the tc-config, then annotate the injector with @Root
- done!

Classes could be done the same way using the tim-annotations and
@Instrumented. I'll make a copy of the sample app with just
annotations (and no config, or very little)

Taylor

eu

unread,
Mar 13, 2008, 1:22:00 PM3/13/08
to google-guice
On Mar 13, 3:34 am, "tgaut...@gmail.com" <tgaut...@gmail.com> wrote:

> What you ask for is already entirely possible using the tim-
> annotations project. In the sample app, just add the tim-annotations
> to the modules in the tc-config, then annotate the injector with @Root
> - done!

Yes, I know.BTW, you have some weird dependencies in your guice tim.

> Classes could be done the same way using the tim-annotations and
> @Instrumented.

That would be a brute force approach that will require to annotate
every class that could get shared (not necessarily a bad thing). But
what I was suggesting is to have some kind of class hierarchy walker
that would make those annotations unnecessary and it should be doable
with Guice's strongly typed API. Sort of similar to how Terracotta
works, when you attach any instance to the distributed root it gets
shared, and here when you bind something to distributed injector it
gets instrumented.

regards,
Eugene

tgau...@gmail.com

unread,
Mar 13, 2008, 3:03:30 PM3/13/08
to google-guice
Eugene,

I am not against those things - I think they are all good ideas. But
I would prefer to wait until people start picking up the
implementation and request ways to make it easier, at that point we
should/could consider tighter integration.
Reply all
Reply to author
Forward
0 new messages