2

I've seen various bits and pieces, but I'm either approaching this wrong or a bit short on my understanding on Guice. I'm trying to modify/extend this cache4guice Infinispan Module so that it can access the JBoss embedded module, and ultimately named caches within the selected cache container.

So, we have my standalone.xml with the following:

<cache-container name="InfinispanCacheModule" default-cache="cache1" jndi-name="java:jboss/infinispan/container/mycachecontainer>
<local-cache name="cache1">
    <eviction strategy="LRU" max-entries="1000"/>
    <expiration max-idle="50000"/>
    <file-store preload="true" passivation="true" purge="false"/>
</local-cache>
<local-cache name="cache2">
    <eviction strategy="LRU" max-entries="500"/>
    <expiration max-idle="20000"/>
    <file-store preload="true" passivation="false" purge="false"/>
</local-cache>

I've modified the @Cached annotation to allow a cachedName argument to be optionally included. The idea being you could use this for a default cache:

@Cached
public someMethod(String someArg) {...}

This for accessing cache2 and beyond...

@Cached(cacheName="cache2")
public someOtherMethod(String someArg) {...}

The only examples that I've found that would seem to allow me to access things is such a fashion would be using the jndi resource, such as in this page - ttp://my.safaribooksonline.com/book/web-development/9781590599976/guice-recipes/integrating_jndi

Which leads me to try something like this:

public class InfinispanCacheModule extends CacheModule {
...
    @Override
    protected void configure() {
        // bind naming context to the default InitialContext
        bind(Context.class).to(InitialContext.class);

        bind(CacheContainer.class).toProvider(JndiIntegration.fromJndi(CacheContainer.class, "java:jboss/infinispan/container/mycachecontainer"));

        bindInterceptor(Matchers.any(), Matchers.annotatedWith(Cached.class), new CacheInterceptor(this));        
    }

Additionally, from the posts here and elsewhere, it seems that I might want to use an @Provides method - along these lines: https://stackoverflow.com/a/8999548/880884 Guice: is it possible to inject modules?

So, now we get into specifics, if we look at the original InfinispanModule, the idea is for me to either pass in a CacheManager on module creation, or create one within the module somehow.

public class MyGuiceFactory {
private static final Injector inj = Guice.createInjector(
new SomeGuiceModule(), 
new InfinispanCacheModule(---- what goes here? -----)
);
public static Injector getInjector() {
    return inj;
}
}

Similar question: Spring, Infinispan and JBoss 7 integration

Community
  • 1
  • 1
shawnjohnson
  • 375
  • 6
  • 19

1 Answers1

1

To get this working I just used normal context and lookup. I placed the following method in my Guice Factory, then passed the CacheContainer to the InfinispanCacheModule via a new constructor.

public static CacheContainer getCacheContainer() {
    org.infinispan.manager.CacheContainer container = null;
    try {
        Context ctx = new InitialContext();
        container = (CacheContainer) ctx
                .lookup("java:jboss/infinispan/container/mycache");
    } catch (NamingException e) {
        e.getCause();
    }
    return container;
}

From there I was able to use a 'cacheName' annotation parameter in the Injector, then reach back to the InfinispanCacheModule's getCache(String cacheName) method. If you want me to share more code repsond to this thread and I will post more details.

shawnjohnson
  • 375
  • 6
  • 19