These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Resolve Jbosscache using JNDI
PostPosted: Wed Oct 20, 2004 12:16 pm 
Newbie

Joined: Mon Feb 02, 2004 4:18 pm
Posts: 16
I'm using Spring to wire an application together, but intead of linking Hibernate and TreeCache thru JMX, I'd prefer to pass the cache provider to Hibernate using JNDI. The reason is that I'd prefer to wire everything using Spring rather than depending on JMX to provide a similar function.

In my Spring config, I'd like to be able to swap between datasources and cache providers depending on the platform. Is there any else in the same boat as myself?

I was thinking of writing an implementation that would perform the JNDI resolution and then delegates the methods from the interface. Then I could still use the current implementation (setting the cache.provider_class property to xxx.MyCacheProviderDelegate), but the calls would be delegated to the correct implementation.

Any ideas folks?


Top
 Profile  
 
 Post subject: How I solved it
PostPosted: Wed Oct 27, 2004 7:31 am 
Newbie

Joined: Fri Sep 10, 2004 9:46 am
Posts: 10
I have implemented what you require. To do it I copied the two java classes TreeCache, and TreeCacheProvider from the net.sf.hibernate.cache into my own package, and changed the constructor of TreeCache as follows, this handles the lookup of TreeCache from JNDI. Then you register you new Provider class in the Hibernate configuration, and ensure there is a MBean depends clause on the Tree Cache sevice. I have tidied up the code further, but only cosmetic, can post the full code if you want it.

Code:
public TreeCache(String regionName, Properties props)
   throws CacheException {

   this.regionName = '/' + regionName.replace('.', '/');
   try {
            synchronized (TreeCache.class) {
                if (cache==null) {
                    cache = (TreeCache)new InitialContext().lookup( "java:/TreeCache" );
                    if ( cache == null )
                       throw new CacheException("Failed to lookup TreeCache in JNDI");
                }
            }
   }
   catch (Exception e) {
      throw new CacheException(e);
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 9:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Funny. I just added this to the JBoss-Hibernate integration code. It will be available in the next 4.0 release as well as the next 3.2 release (as well as the HEAD branch).

It's a little different. It changes the Hibernate MBean to add a new attribute named "DeployedTreeCacheJndiName". Just set the "CacheProviderClass" attribute to "org.jboss.hibernate.cache.DeployedTreeCacheProvider" and then set the "DeployedTreeCacheJndiName" attribute to the place TreeCache is being bound into JNDI.

Note that this is only available OOTB with TreeCache 1.2 (the automatic binding of the TreeCache, that is).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 11:39 am 
Newbie

Joined: Fri Sep 10, 2004 9:46 am
Posts: 10
Steve Can I make a suggestion.

Change the existing net.sf.cache.TreeCache constructor to the following.

Code:
public TreeCache(String regionName, org.jboss.cache.TreeCache cache)
    throws CacheException {
    this.cache = cache;
    this.regionName = '/' + regionName.replace('.', '/');
}


Then in the CacheProvider implementation perform all the resolution of the jboss TreeCache object, either by JNDI, or by constructing it manually.

This decouples the construction of the jboss TreeCache keeping it in the factory, leaving the net.sf.hibernate.cache.Cache implementation to just invoke the methods in the cache. eg

Code:
public class TreeCacheProvider implements CacheProvider
{
    public Cache buildCache(String regionName, Properties properties) throws CacheException {
        return new JBossTreeCache(regionName, getCache());
    }

    public long nextTimestamp() {
        return System.currentTimeMillis() / 100;
    }

    // reference to the tree cache we discovered
    private static TreeCache cache;

    // What we lookup in JNDI to discobver the Tree Cache
    private static final String TREECACHE_JNDI_REF = "java:/TreeCache";

    public static TreeCache getCache() throws CacheException {
        if ( cache != null ) return cache;
        try {
            synchronized (JBossTreeCache.class) {
                if (cache==null) {
                    cache = (TreeCache)new InitialContext().lookup( TREECACHE_JNDI_REF );
                    if ( cache == null ) throw new CacheException("Failed to lookup TreeCache in JNDI");
                }
            }
        }
        catch (Exception e) {
            throw new CacheException(e);
        }
        return cache;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 12:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I thought about it, but that is not how its done in the other impls. I'd rather leave this as-is for consistency sake.

And no I don't feel like changing all of them :)


Top
 Profile  
 
 Post subject: CacheProvider implementation.
PostPosted: Fri Oct 29, 2004 6:32 pm 
Newbie

Joined: Fri Sep 10, 2004 9:46 am
Posts: 10
Quote:
And no I don't feel like changing all of them :)


Are you looking for a volunteer? I dont have developer status on project, so this would be the only issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 8:02 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
How should my cache -service.xml file look like?
Since now if I follow the docs on JBossTreeCache this is how they bind TreeCache to jndi:
Code:
    <mbean
        code="org.jboss.invocation.jrmp.server.JRMPProxyFactory"
        name="mydomain:service=proxyFactory,type=jrmp,target=factory">
        <attribute name="InvokerName">jboss:service=invoker,type=jrmp</attribute>
        <attribute name="TargetName">jboss.cache:service=TreeCache</attribute>
        <attribute name="JndiName">java:/treeCache</attribute>
        <attribute name="InvokeTargetMethod">true</attribute>
        <attribute name="ExportedInterface">org.jboss.cache.TreeCacheMBean</attribute>
        <attribute name="ClientInterceptors">
            <interceptors>
                <interceptor>org.jboss.proxy.ClientMethodInterceptor</interceptor>
                <interceptor>org.jboss.proxy.SecurityInterceptor</interceptor>
                <interceptor>org.jboss.invocation.InvokerInterceptor</interceptor>
            </interceptors>
        </attribute>
        <depends>jboss:service=invoker,type=jrmp</depends>
        <depends>jboss.cache:service=TreeCache</depends>
    </mbean>           


But this way the proxy bean exposes only TreeCacheMBean interface and not TreeCache object. So I get a ClassCastException in JndiBoundTreeCacheProvider.

Rgds, Ales

_________________
--------------------------------
Ales Justin
JBoss, a division of Red Hat


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 8:15 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Found it:

<property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook"/>
<property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=TreeCache"/>

Rgds, Ales


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.