Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          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>
                  <attribute name="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>
             其中ClusterConfig配置在前面JavaGroups的介紹詳細介紹,其它配置比較簡單,需要進一步了解請參見TreeCache文檔。
             一、Cache分類
             TreeCache按功能分為三類:本地(Local)Cache、復制(Replication)Cache和失效 (Invalidation)Cache。本地Cache只應用于本地環境,后兩個Cache可應用于分布式環境,其中,在分布式環境中,復制Cache 當一個Cache實例的一個節點值發生變化時會將變化復制到其它實例中,而失效Cache是當一個Cache實例的一個節點值發生變化時會將其它實例的相 應節點的值設為空,讓其重新去獲得該值,可通過這種方式緩存大對象以減少在實例中復制對象的代價。分布式Cache(復制和失效Cache)又分為兩種, 同步(REPL_ASYNC)和異步(REPL_SYNC),同步Cache是在一個Cache實例做修改時,等待變化應用到其它實例后才返回,而異步 Cache是在一個Cache實例做修改時,即刻返回。其配置見下:
          !--
            Valid modes are LOCAL
                            REPL_ASYNC
                            REPL_SYNC
                            INVALIDATION_ASYNC
                            INVALIDATION_SYNC
          ->
          attribute name="CacheMode">REPL_SYNC</attribute>

             二、事務和并行(Transaction And Concurrent)
             TreeCache是一種事務Cache,與JDBC一樣,其包括兩方面內容:鎖和隔離級別。鎖分為悲觀鎖和樂觀鎖,當使用悲觀鎖時,分為五個隔離級別, 分別是SERIALIZABLE、REPEATABLE_READ (default)、READ_COMMITTED、READ_UNCOMMITTED和NONE,隔離級別逐步減弱。樂觀鎖也叫版本鎖,其對數據進行操 作時,將其復制到臨時區,操作之后將版本與原有數據比較,如果一致則將遞增版本并寫回,如果不一致則回滾,由于樂觀鎖僅在復制出數據和提交數據時對數據加 鎖,所以并行度更高,但如果寫操作比較頻繁地話則容易出現沖突導致回滾。TreeCache默認使用悲觀鎖。使用TreeCache時,需要使用容器提供 的事務管理器,一般使JBossTransactionManagerLookup和GenericTransactionManagerLookup, 前者應用于JBOSS服務器,后者應用于其他服務器,也可使用DummyTransactionManagerLookup用于測試。如上介紹的配置如 下:
          attribute name="NodeLockingScheme">OPTIMISTIC</attribute>
                <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
               <attribute name="TransactionManagerLookupClass">org.jboss.cache.DummyTransactionManagerLookup</attribute>


             三、逐出策略(Eviction Policy)
             由于內存數量的局限,不可能將所有的Cache數據存放在內存中,但使用內存達到一定極限時,會將部分數據清除出內存,保存到其它持久媒質中,定義的什么 時候清除、如何清除的策略就是逐出策略。自定義一個逐出策略需要實現org.jboss.cache.eviction.EvictionPolicy、 org.jboss.cache.eviction.EvictionAlgorithm、
          g.jboss.cache.eviction.EvictionQueue 和org.jboss.cache.eviction.EvictionConfiguration四個接口,系統提供了LRU(Least recently used,最近最少使用)、LFU(Least Frequently Used最不經常使用)、FIFO(First In First Out先進先出)、MRU(Most Recently Used最近最經常使用)四種實現,詳細參見org.jboss.cache.eviction包的源代碼。配置如下:
          <attribute name="EvictionPolicyConfig">
             <config>
                <attribute name="wakeUpIntervalSeconds">5</attribute>
                <region name="/_default_">
                  <attribute name="maxNodes">5000</attribute>
                  <attribute name="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>
                  <attribute name="timeToLiveSeconds">8</attribute>
                  <attribute name="maxAgeSeconds">10</attribute>
                </region>
             </config>
          </attribute>
               四、Cache加載
            由于逐出策略的存在,那么當我們重新需要獲得一個原來在緩存中但確由內存原因被逐出的數據時,就需要定義一種加載策略,使地可以重新找回數據,同 時,Cache加載也肩負在將數據逐出時將數據保存到持久媒質的責任。
            根據將數據保存媒質的不同,Cache加載包括FileCacheLoader、JDBCCacheLoader等等,可以同時使用多種加載器來靈活定制 加載策略。例見下:
          <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>

          我們將通過定制如上的配置信息以更有效地使用JBossCache。詳細情況可參考JBoss TreeCache參考文檔和范例。

          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();
          posted on 2010-05-18 04:19 seal 閱讀(408) 評論(0)  編輯  收藏 所屬分類: web服務器
          主站蜘蛛池模板: 潞西市| 奉节县| 香港 | 日照市| 巴林左旗| 上饶市| 辽中县| 同心县| 巫山县| 泉州市| 黑水县| 兴仁县| 平乡县| 安岳县| 满城县| 平湖市| 南溪县| 类乌齐县| 安庆市| 临沂市| 新昌县| 仙居县| 株洲县| 府谷县| 久治县| 济宁市| 平阴县| 茶陵县| 宁远县| 大余县| 苏尼特右旗| 宝山区| 深泽县| 清镇市| 冷水江市| 盘锦市| 措美县| 峨眉山市| 海林市| 辽宁省| 蒲城县|