淘宝tfs-client java配置

来源:互联网 发布:yy制作头像软件 编辑:程序博客网 时间:2024/06/06 16:36

pom.xml

[html] view plain copy
  1. <dependency>  
  2.             <groupId>com.taobao.tair</groupId>  
  3.             <artifactId>tair-client</artifactId>  
  4.             <version>2.3.1</version>  
  5.         </dependency>  
  6.   
  7.         <dependency>  
  8.             <groupId>com.taobao.common.tair</groupId>  
  9.             <artifactId>common-tair</artifactId>  
  10.             <version>2.3.1</version>  
  11.         </dependency>  
  12.   
  13.         <dependency>  
  14.             <groupId>com.taobao.common.tfs</groupId>  
  15.             <artifactId>tfs-javaclient</artifactId>  
  16.             <version>2.1.6</version>  
  17.         </dependency>  
tfs.properties

[java] view plain copy
  1. tfs.client.maxWaitThread=100  
  2. tfs.client.timeout=2000  
  3. tfs.client.nsip=192.168.229.3:8108  
  4. tfs.client.tfsClusterIndex=1  
  5. tfs.client.maxCacheItemCount=10000  
  6. tfs.client.maxCacheTime=5000  
spring-tfs.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.       
  5.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  6.         <property name="location">  
  7.             <value>classpath:tfs.properties</value>  
  8.         </property>  
  9.     </bean>  
  10.   
  11.   
  12.     <bean id="tfsManager" class="com.taobao.common.tfs.DefaultTfsManager" init-method="init" >  
  13.       <!-- 整个进程中系统最多等待多少个请求,取决于你有多少个线程并发的请求TFS -->  
  14.        <property name="maxWaitThread" value="${tfs.client.maxWaitThread}" />  
  15.   
  16.       <!-- 单个请求最大的等待时间(ms) 超过这个时间放弃这次请求-->  
  17.       <property name="timeout" value="${tfs.client.timeout}" />  
  18.   
  19.       <!-- Tfs master nameserver ip address -->  
  20.       <property name="nsip" value="${tfs.client.nsip}" />  
  21.   
  22.       <!-- TFS 集群的编号,这个编号只是一种参考,系统初始化的时候会从ns上取,取不到才用本地设置的.!-->  
  23.       <property name="tfsClusterIndex" value="${tfs.client.tfsClusterIndex}" />  
  24.   
  25.       <!-- TFS在读取文件的时候会缓存block所在的数据服务器ip,这个参数配置了最多缓存的记录个数!-->  
  26.       <property name="maxCacheItemCount" value="${tfs.client.maxCacheItemCount}" />  
  27.   
  28.       <!-- 上一项缓存最大有效的时间(ms)!-->  
  29.       <property name="maxCacheTime" value="${tfs.client.maxCacheTime}" />  
  30.       
  31.        <!-- tair排重数据库的serverlist, 至少指定一个ip address -->  
  32.        <!--   
  33.       <property name="uniqueServerList">  
  34.         <list>  
  35.           <value>10.232.12.141:5198</value>  
  36.         </list>  
  37.       </property>  
  38.        -->  
  39.        <!-- tair排重数据库的groupName -->  
  40.        <!--   
  41.       <property name="groupName">  
  42.         <value>group_1</value>  
  43.       </property>  
  44.       <property name="namespace">  
  45.         <value>100</value>  
  46.       </property>  
  47.        -->  
  48.     </bean>  
  49.     <bean id="tfsClientService" class="xxx.xxx.xxx.TfsClientService">  
  50.       <property name="tfsManager" ref="tfsManager" />  
  51.     </bean>  
  52. </beans>  
TfsClientService.java

[java] view plain copy
  1. public class TfsClientService {  
  2.   
  3.     /** 
  4.      * tfs 管理器 
  5.      */  
  6.       
  7.     private DefaultTfsManager tfsManager ;  
  8.       
  9.     /** 
  10.      * 保存一个文件到 tfs中,文件小于2M 
  11.      * 
  12.      * @param localFileName  本地文件名 
  13.      * @param tfsSuffix  文件后缀 
  14.      * @return  保存成功,返回成功后 的tfs文件名,失败返回null 
  15.      */  
  16.    public  String saveFile(String localFileName,  String tfsSuffix){  
  17.        if(null == localFileName || null == tfsSuffix){  
  18.            throw new IllegalArgumentException("保存文件的入参不能为空!localFileName=" + localFileName  + ",tfsSuffix=" + tfsSuffix);  
  19.        }  
  20.        return  tfsManager.saveFile(localFileName, null, tfsSuffix);  
  21.           
  22.     }  
  23.       
  24.    /** 
  25.     * 保存一个文件字节流到 tfs中,文件小于2M 
  26.     * 
  27.     * @param data  文件字节流 
  28.     * @param tfsSuffix  文件后缀 
  29.     * @return  保存成功,返回成功后 的tfs文件名,失败返回null 
  30.     */  
  31.    public   String saveFile(byte[] data,  String tfsSuffix){  
  32.        if(null == data || null == tfsSuffix){  
  33.            throw new IllegalArgumentException("保存文件字节流入参不能为空!data=" + data  + ",tfsSuffix=" + tfsSuffix);  
  34.        }  
  35.        return tfsManager.saveFile(data, null, tfsSuffix);  
  36.    }  
  37.      
  38.        
  39.       
  40.    /** 
  41.     * 保存一个大文件到tfs(文件大于2M),成功返回tfs文件名(L开头),失败返回null 
  42.     * tfs文件写大文件,为了断点续传,必须传入一个key参数,来标识此次大文件的写。一次写失败后,再次传入相同的key写,tfsclient会根据key找到前一次已经写完成的部分重用 
  43.     * 默认使用localFileName作为key 
  44.     * 
  45.     * @param localFileName  本地文件名 
  46.     * @param tfsFileName  保存到tfs中的文件名 (目前都为null,不支持自定义文件名) 
  47.     * @param tfsSuffix  文件后缀 
  48.     * @return  保存成功,返回成功后 的tfs文件名,失败返回null 
  49.     */  
  50.     public  String saveLargeFile(String localFileName,  String tfsSuffix){  
  51.          if(null == localFileName || null == tfsSuffix){  
  52.              throw new IllegalArgumentException("保存大文件入参不能为空! localFileName=" + localFileName  + ",tfsSuffix=" + tfsSuffix);  
  53.        }  
  54.          return tfsManager.saveLargeFile(localFileName, null, tfsSuffix);  
  55.     }  
  56.       
  57.     /** 
  58.      * 保存一个字节流data到tfs(文件大于2M),成功返回tfs文件名,失败返回null, 
  59.      * tfs文件写大文件,为了断点续传,必须传入一个key参数,来标识此次大文件的写。一次写失败后,再次传入相同的key写,tfsclient会根据key找到前一次已经写完成的部分重用 
  60.      *  
  61.      * @param data 
  62.      * @param tfsSuffix 
  63.      * @param key key name 
  64.      * @return 
  65.      */  
  66.     public  String saveLargeFile(byte[] data, String tfsSuffix, String key){  
  67.        if(null == data || null == tfsSuffix){  
  68.            throw new IllegalArgumentException("保存大文件字节流入参不能为空! data=" + data  + ",tfsSuffix=" + tfsSuffix);  
  69.        }  
  70.       return tfsManager.saveLargeFile(data, null, tfsSuffix, key);  
  71.     }  
  72.   
  73.       
  74.       
  75.     /** 
  76.      * 删除一个文件 
  77.      * 
  78.      * @param tfsFileName 文件名 
  79.      * @param tfsSuffix  文件后缀 
  80.      * @return  true if delete successully, or false if fail 
  81.      */  
  82.     public  boolean deleteFile(String tfsFileName, String tfsSuffix){  
  83.        if(null == tfsFileName || null == tfsSuffix){  
  84.              
  85.            throw new IllegalArgumentException("删除文件名或文件后缀不能为空! tfsFileName=" + tfsFileName  + ",tfsSuffix=" + tfsSuffix);  
  86.        }  
  87.          return tfsManager.unlinkFile(tfsFileName, tfsSuffix);  
  88.     }  
  89.       
  90.       
  91.     /** 
  92.      * 从tfs 获取文件到本地 
  93.      * 
  94.      * @param tfsFileName   需要读取的tfs文件名 
  95.      * @param tfsSuffix  需要读取的文件名后缀,需要和存入时后缀相同。 
  96.      * @param localFileName 本地文件名 
  97.      * @return  读操作成功返回true,读操作失败返回false 
  98.      */  
  99.     public  boolean  fetchFile(String tfsFileName, String tfsSuffix, String localFileName){  
  100.        if(null == tfsFileName || null == tfsSuffix || null == localFileName ){  
  101.            throw new IllegalArgumentException("获取文件入参不能为空! tfsFileName=" + tfsFileName  + ",tfsSuffix=" + tfsSuffix + ",localFileName=" + localFileName);  
  102.        }  
  103.          
  104.         return tfsManager.fetchFile(tfsFileName, tfsSuffix, localFileName);  
  105.     }  
  106.       
  107.       
  108.     /** 
  109.      * 从tfs 获取文件到本地,数据存到输出流 
  110.      *  
  111.      * @param tfsFileName 
  112.      * @param tfsSuffix 
  113.      * @param output   数据流 
  114.      * @return  读操作成功返回true,读操作失败返回false 
  115.      */  
  116.     public  boolean fetchFile(String tfsFileName, String tfsSuffix, OutputStream output){  
  117.        if(null == tfsFileName || null == tfsSuffix || null == output ){  
  118.            throw new IllegalArgumentException("获取文件入参不能为空! tfsFileName=" + tfsFileName  + ",tfsSuffix=" + tfsSuffix + ",output=" + output);  
  119.        }  
  120.              
  121.         return tfsManager.fetchFile(tfsFileName, tfsSuffix, output);  
  122.     }  
  123.       
  124.       
  125.     /** 
  126.      * stat一个tfs文件 
  127.      *文件的状态有0(正常), 1(删除), 4(隐藏) 
  128.      * @param tfsFileName 需要读取的tfs文件名 
  129.      * @param tfsSuffix 需要读取的文件名后缀,需要和存入时后缀相同 
  130.      * @return  操作成功返回FileInfo,操作失败返回null 
  131.      */  
  132.     public   FileInfo statFile(String tfsFileName, String tfsSuffix){  
  133.          if(null == tfsFileName || null == tfsSuffix ){  
  134.                throw new IllegalArgumentException("stat文件入参不能为空! tfsFileName=" + tfsFileName  + ",tfsSuffix=" + tfsSuffix );  
  135.          }  
  136.            
  137.          return tfsManager.statFile(tfsFileName, tfsSuffix);  
  138.     }   
  139.       
  140.       
  141.       
  142.     public void setTfsManager(DefaultTfsManager tfsManager){  
  143.         this.tfsManager = tfsManager;  
  144.     }  
  145.       
  146.     public DefaultTfsManager getTfsManager(){  
  147.         return this.tfsManager ;  
  148.     }  
原创粉丝点击