jboss TreeCache 讲解
TreeCache是一种结构化的、基于复制的事务缓存。TreeCache是JBoss应用服务器中集群服务—包括JNDI集群、HTTP和EJB的
Sesssion集群、JMS集群—的基础框架。其可以单独使用,可以集成到JBossAS应用,也可以集成到其他的应用服务器上。TreeCache是
一种树状结构,每个节点拥有一个名字和多个或者没有子节点,除跟节点没有子节点其他节点有且只有一个父母节点,可以通过路径名来访问子节点
(FQN:Full Qualified
Name),在一个TreeCache中可以存在多棵树,,即可以有多个根节点。当应用于分布式环境时,由于TreeCache是基于复制的,每个子节点
的值必须是可序列化的。
在下面中,将通过例子来了解TreeCache的功能及其配置,使用JBossCache1.4和JDK5.0。首先是一个最基本使用TreeCache
的程序例子并配置一个TreeCache的配置骨架(各种常用的配置可参见jboss-cache-dist-1.4.0.CR1版本的etc目录,如下
各种配置参考也可见该目录下的范例配置,以下不再强调),见下:
treecache.xml:
<server> <mbean code=”org.jboss.cache.TreeCache” name=”jboss.cache:service=TreeCache”> <depends>jboss:service=Naming</depends> <depends>jboss:service=TransactionManager</depends> <attributename=”ClusterName”>TreeCache-Cluster</attribute> <attribute name=”ClusterConfig”> <config> <UDP mcast_addr=”228.1.2.3″ mcast_port=”48866″ ip_ttl=”64″ ip_mcast=”true” mcast_send_buf_size=”150000″mcast_recv_buf_size=”80000″ ucast_send_buf_size=”150000″ucast_recv_buf_size=”80000″ loopback=”false”/> <PING timeout=”2000″ num_initial_members=”3″ up_thread=”false” down_thread=”false”/> <MERGE2 min_interval=”10000″max_interval=”20000″/> <FD_SOCK/> <VERIFY_SUSPECT timeout=”1500″ up_thread=”false” down_thread=”false”/> <pbcast.NAKACK gc_lag=”50″retransmit_timeout=”600,1200,2400,4800″ max_xmit_size=”8192″ up_thread=”false”down_thread=”false”/> <UNICAST timeout=”600,1200,2400″window_size=”100″ min_threshold=”10″ down_thread=”false”/> <pbcast.STABLE desired_avg_gossip=”20000″ up_thread=”false” down_thread=”false”/> <FRAG frag_size=”8192″ down_thread=”false” up_thread=”false”/> <pbcast.GMS join_timeout=”5000″join_retry_timeout=”2000″ shun=”true” print_local_addr=”true”/> <pbcast.STATE_TRANSFER up_thread=”true”down_thread=”true”/> </config> </attribute> </mbean></server>
!– Valid modes are LOCAL REPL_ASYNC REPL_SYNC INVALIDATION_ASYNC INVALIDATION_SYNC->attribute name=”CacheMode”>REPL_SYNC</attribute>
attribute name=”NodeLockingScheme”>OPTIMISTIC</attribute> <attributename=”IsolationLevel”>REPEATABLE_READ</attribute> <attributename=”TransactionManagerLookupClass”>org.jboss.cache.DummyTransactionManagerLookup</attribute>
<attribute name=”EvictionPolicyConfig”> <config> <attributename=”wakeUpIntervalSeconds”>5</attribute> <region name=”/_default_”> <attribute name=”maxNodes”>5000</attribute> <attributename=”timeToLiveSeconds”>1000</attribute> </region> <region name=”/org/jboss/data”policyClass=”org.jboss.cache.eviction.FIFOPolicy”> <attribute name=”maxNodes”>5000</attribute> </region> <region name=”/test/”policyClass=”org.jboss.cache.eviction.MRUPolicy”> <attribute name=”maxNodes”>10000</attribute> </region> <region name=”/maxAgeTest/”> <attribute name=”maxNodes”>10000</attribute> <attributename=”timeToLiveSeconds”>8</attribute> <attribute name=”maxAgeSeconds”>10</attribute> </region> </config></attribute>
<attribute name=”CacheLoaderConfiguration”> <config> <passivation>false</passivation> <preload>/</preload> <shared>true</shared> <cacheloader> <class>org.jboss.cache.loader.ClusteredCacheLoader</class> <properties> timeout=1000 </properties> <async>true</async> <fetchPersistentState>false</fetchPersistentState> ignoreModifications>false</ignoreModifications> <purgeOnStartup>false</purgeOnStartup> </cacheloader> <cacheloader> <class>org.jboss.cache.loader.JDBCCacheLoader</class> <properties> cache.jdbc.table.name=jbosscache cache.jdbc.table.create=true cache.jdbc.table.drop=true cache.jdbc.table.primarykey=jbosscache_pk cache.jdbc.fqn.column=fqn cache.jdbc.fqn.type=varchar(255) cache.jdbc.node.column=node cache.jdbc.node.type=longblob cache.jdbc.parent.column=parent cache.jdbc.driver=com.mysql.jdbc.Driver cache.jdbc.url=jdbc:mysql://localhost:3306/jbossdb cache.jdbc.user=root cache.jdbc.password= </properties> <async>true</async> <fetchPersistentState>false</fetchPersistentState> <purgeOnStartup>false</purgeOnStartup> </cacheloader> </config></attribute>
TreeCache tree = new TreeCache();tree.setClusterProperties(“treecache.xml”;tree.createService();tree.startService();tree.put(“/a/b/c”, “name”, “Ben”;tree.put(“/a/b/c/d”, “uid”, new Integer(322649));Integer tmp = (Integer) tree.get(“/a/b/c/d”, “uid”;tree.remove(“/a/b”;tree.stopService();tree.destroyService();