??xml version="1.0" encoding="utf-8" standalone="yes"?>gogo全球高清大胆亚洲,国产V亚洲V天堂A无码,91精品国产亚洲爽啪在线影院http://m.tkk7.com/dongbule/category/46466.html建造高性能门户|?/description>zh-cnTue, 04 Mar 2014 08:54:29 GMTTue, 04 Mar 2014 08:54:29 GMT60朋友要招几个javaQ让帮忙出点面试题目http://m.tkk7.com/dongbule/archive/2013/07/05/401249.html陈于?/dc:creator>陈于?/author>Fri, 05 Jul 2013 09:18:00 GMThttp://m.tkk7.com/dongbule/archive/2013/07/05/401249.htmlhttp://m.tkk7.com/dongbule/comments/401249.htmlhttp://m.tkk7.com/dongbule/archive/2013/07/05/401249.html#Feedback6http://m.tkk7.com/dongbule/comments/commentRss/401249.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/401249.html上周朋友说要招几个高U点的javaQ网上那些java面试宝典已经被h背得熟透了Q让帮忙出出几个面试的问题,主要看看Ҏ使用得语a有较深入得了解,不停留在使用什么开源框Ӟ和对自己一些项目得见解。当然还要装装BQ最后问一些别Z懂得问题Q好压压价钱。。。罪q,公司是他自己?/div>
随便整理了一下,以后面试也可以用刎ͼ主要针对qx日常得应用,只要有稍微阅读过jdk源码Q对数据l构有一点点了解回答应该没什么问题?/div>
随便写了几个

1.     当需要往一?/span>hashMap里面put很多对象Q你会怎么?/span>new q个hashmap?

注:主要审查是否懂得hash的数据结构,是否会用

HashMap(int initialCapacity, float loadFactor)

q个构造函数去创徏一个大容积?/span>Hashl构Q懂得原理即可。如果回{正,可深入问hashmap的扩宏V?/span>

 

2.     实现一个先q先出的队列Q分别有arraylist?/span>linkedlistQ选择哪个Qؓ什么?

注:主要审查是否懂得U性表和链表?/span>

 

3. 当我?/span>new arrayList(20)一个这L对象后,不断往里面dadd(object),加到W?/span>21个后Q内存会有什么调整?/span>

注:主要审查是否懂得可变array的本质,当内存进行扩容的本质?/span>

Arrays.copyOf(elementData, newCapacity)

      

4.     一个类有几百个实现ҎQ突然有个需求:要纪录每个方法被调用的参敎ͼ旉为日志,q个怎么d玎ͼ

注:主要审查是否使用aop{面向切面编E?/span>如果回答正确Ql追问,只对Ҏ名开头ؓ’insert’Q?/span>’update’{方法进行日志纪录,该怎么配对。注Q主要审查对正则表达式的掌握?/span>

 

     5Q在vi的编辑下Q对光标所在行q行整行删除命o是什么,光标移到当前行   W一列的命o是什?/span>

         注:主要审查?/span>linux下最基础?/span>vi~辑命oQ不?/span>vi是否可认为无?/span>linux真实工作Q?/span>

 

6.     机器出现OutOfMemoryErrorQ?/span> PermGen space 的内存溢出错误,解决思\是什?/span>

注:主要审查是否明白Ҏ区或叫永久区的定义,该区是否内存回收Q什么实例会存放在该区,不求具体实现ҎQ主要有大概思\卛_?/span>

 

7.     你的pȝ在明天出现ȝ高ƈ发,该怎么准备

注:Ҏ多种Q主要检查思\QO谈来观察?/span>

 

8.     一?/span>linux服务器每天定时生成一ҎӞ需要将q批文g同步到其他多台服务器Q该怎么实现?/span>

注:Ҏ多种Q如rsyncQ?/span>nfsQ?/span>scp{等Q主要检?/span>linux下文件处理的能力Q主要检查思\?/span>

 

9.     Z么说量减小synchronized Ҏ

注:主要审核对同步实现方法的认识Q如果能回答?#8220;?#8221;的概念,l箋q问Q如果对实例Ҏ?/span>synchronizedQ?/span> 锁的对象是什么,寚w态方法加synchronized Q锁的对象是什?/span>

  

10Q说说单例的使用场景是什么,?/span>spring场景中就有许多单例模?/span>

       注:{案很多Q可以O谈来观察Q能回答出,让多个线E相互通信辑ֈ资源׃n卛_

 

11. 随便?/span>jdk?/span>spring的英?/span>api中找一D让其翻?/span>

注:能大概说出接口的用处卛_



----------------------------------------

by 陈于?
QQ:34174409
Mail: dongbule@163.com



]]>单分析cglib引v的PermSize Space内存溢出http://m.tkk7.com/dongbule/archive/2011/08/21/356987.html陈于?/dc:creator>陈于?/author>Sun, 21 Aug 2011 07:55:00 GMThttp://m.tkk7.com/dongbule/archive/2011/08/21/356987.htmlhttp://m.tkk7.com/dongbule/comments/356987.htmlhttp://m.tkk7.com/dongbule/archive/2011/08/21/356987.html#Feedback0http://m.tkk7.com/dongbule/comments/commentRss/356987.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/356987.html
上段旉有台机器发生?java.lang.OutOfMemoryError: PermGen space 内存溢出的异常,当时大概判断了原因后把 MaxPermSize 配置调高后,把问题解决了,不过IZ旉后还是需要l把review代码?br />
一般来说PermSize Space OOM的话Q第一U可能就是方法区溢出Q第二种是q行时常量池溢出Q第二种查看后基本排除掉Q问题就应该出现在方法区的溢出,Ҏ区用于存放class的相关信息,如类名,讉K修饰W,帔R池,字段描述Q方法描q等{,对于q个区域的溢出,基本上都是运行时产生大量的类填满了整个方法区Q直到溢出?br />
spring aop中都是用到了cglibq类字节码的技术,动态代理的c越多,需要越多的Ҏ区来保证动态生成的class可以加蝲入到内存中去Q?/span>
不过spring框架D的不会因U原因?strong>撑爆perm的应该是各种methodaccessorX和constructoracccessorX{等。本来这些accessor也有~存Q但它们使用内存大小敏感的reference引用着的,且用的是堆内存。当你堆内存吃紧的时候,q些~存摧毁了Q就必然会不断生新的methodAccessor字节码,是这个撑爆了perm?strong>所以除增大permsizeq应该看看^时运行时堆内存是不是l常用光?/span>

下面的例使用cglib直接q行动态代理生大量的动态类Q然后用jconsoleq行观察?br />
首先本机的jvm配置?-XX:PermSize=64M -XX:MaxPermSize=64M Q给到PermSize最大ؓ64M的内?/div>
public class PermgenOOM {

    
public static void main(String[] args) throws InterruptedException {
        
int i=0;
        
while(true){
            Enhancer enhancer 
= new Enhancer();
            enhancer.setSuperclass(Product.
class);
            enhancer.setUseCache(
false);// 关闭CGLib~存Q否则L生成同一个类
            enhancer.setCallback(new MethodInterceptor() {                
                @Override
                
public Object intercept(Object obj, Method method, Object[] args,
                        MethodProxy methodproxy) 
throws Throwable {
                    
// TODO Auto-generated method stub
                    return methodproxy.invokeSuper(obj,args);
                }
            });
            enhancer.create();
            Thread.sleep(
100);
        }
    }
}

很快Q系l就抛出?java.lang.OutOfMemoryError: PermGen space
内存池peimgen的情?/div>

加蝲cȝ情况



q且在方法区中,一个类如果要被垃圾攉器回收掉Q判断的条g是非常苛ȝQ很多h都把Ҏ区称?#8220;怹?#8221;(Permanent Generation),但据?者在本质上是不一致的Q另外还有称gؓ“非堆?#8221;Q不U结q个了?br />
再看看enhancer.setUseCache(false)Q如果选择为true的话Q那么就使用和更Ccd有相同属性生成的cȝ静态缓存,而不会在同一个类文gql被动态加载ƈ视ؓ不同的类Q这个其实跟cȝequals()和hashCode()有关Q它们是与cglib内部的class cache的key相关的?br />
上面的E序 enhancer.setUseCache(false) 改ؓ enhancer.setUseCache(ture)

public class PermgenOOM {

    
public static void main(String[] args) throws InterruptedException {
        
int i=0;
        
while(true){
            Enhancer enhancer 
= new Enhancer();
            enhancer.setSuperclass(Product.
class);
            enhancer.setUseCache(
true);// 或者不写,默认值就是true
            enhancer.setCallback(new MethodInterceptor() {                
                @Override
                
public Object intercept(Object obj, Method method, Object[] args,
                        MethodProxy methodproxy) 
throws Throwable {
                    
// TODO Auto-generated method stub
                    return methodproxy.invokeSuper(obj,args);
                }
            });
            enhancer.create();
            Thread.sleep(
100);
        }
    }
}

内存池peimgen的情?/div>

加蝲cȝ情况



可以发现内存池peimgen和加载类的情况ƈ没有呈现直线上涨Q已l他们一直都使用者动态类生成cȝ静态缓存,但是q种动态创建类使用静态缓存在一些情况下q不适合需求?br />



]]>java~程?Z性能'一些尽量做到的地方http://m.tkk7.com/dongbule/archive/2011/08/15/356577.html陈于?/dc:creator>陈于?/author>Mon, 15 Aug 2011 08:51:00 GMThttp://m.tkk7.com/dongbule/archive/2011/08/15/356577.htmlhttp://m.tkk7.com/dongbule/comments/356577.htmlhttp://m.tkk7.com/dongbule/archive/2011/08/15/356577.html#Feedback12http://m.tkk7.com/dongbule/comments/commentRss/356577.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/356577.html


最q的机器内存又爆满了Q出了新增机器内存外Q还应该好好review一下我们的代码Q有很多代码~写q于随意化,q些不好的习惯或对程序语a的不了解是应该好好打压打压了?/div>
下面是参考网l资源和ȝ一些在java~程中尽可能做到的一些地?br />-
1.量在合适的场合使用单例
使用单例可以减轻加蝲的负担,~短加蝲的时_提高加蝲的效率,但ƈ不是所有地斚w适用于单例,单来_单例主要适用于以下三个方?/div>
W一Q控制资源的使用Q通过U程同步来控制资源的q发讉K
W二Q控制实例的产生Q以辑ֈ节约资源的目?/div>
W三Q控制数据共享,在不建立直接兌的条件下Q让多个不相关的q程或线E之间实现通信
-
2.量避免随意使用静态变?/strong>
要知道,当某个对象被定义为stataic变量所引用Q那么gc通常是不会回收这个对象所占有的内存,?/div>
public class A{
static B b = new B();
}
此时静态变量b的生命周期与Acd步,如果AcM会卸载,那么b对象会常d存,直到E序l止?br />-
3.量避免q多q常的创建java对象
量避免在经常调用的ҎQ@环中new对象Q由于系l不仅要p旉来创建对象,而且q要花时间对q些对象q行垃圾回收和处理,在我们可以控制的范围内,最
大限度的重用对象Q最好能用基本的数据cd或数l来替代对象?br />
-
4.量使用final修饰W?/strong>
带有final修饰W的cL不可z的。在Java核心API中,有许多应用final的例子,例如java.lang.String。ؓStringcL定final防止了用者覆盖length()Ҏ。另外,如果一个类是final的,则该cL有方法都是final的。java~译器会LZ内联(inline)所有的finalҎ(q和具体的编译器实现有关)。此举能够性能q_提高50%?br />
-
5.量使用局部变?/strong>
调用Ҏ时传递的参数以及在调用中创徏的时变量都保存在栈(Stack)中,速度较快。其他变量,如静态变量,实例变量{,都在?Heap)中创建,速度较慢?br />-
6.量处理好包装类型和基本cd两者的使用场所
虽然包装cd和基本类型在使用q程中是可以怺转换Q但它们两者所产生的内存区域是完全不同的,基本cd数据产生和处理都在栈中处理,包装cd是对象,是在堆中产生实例?/div>
在集合类对象Q有对象斚w需要的处理适用包装cdQ其他的处理提倡用基本类型?br />-
7.慎用synchronizedQ尽量减synchronize的方?/strong>
都知道,实现同步是要很大的系l开销作ؓ代h的,甚至可能造成死锁Q所以尽量避免无谓的同步控制。synchronizeҎ被调用时Q直接会把当前对象锁了,在方法执行完之前其他U程无法调用当前对象的其他方法。所以synchronize的方法尽量小Qƈ且应量使用Ҏ同步代替代码块同步?br />-
8.量使用StringBuilder和StringBufferq行字符串连?/strong>
q个׃多讲?br />-
9.量不要使用finalizeҎ
实际上,资源清理放在finalizeҎ中完成是非常不好的选择Q由于GC的工作量很大Q尤其是回收Young代内存时Q大都会引v应用E序暂停Q所以再选择使用finalizeҎq行资源清理Q会DGC负担更大Q程序运行效率更差?br />
-
10.量使用基本数据cd代替对象
String str = "hello";
上面q种方式会创Z?#8220;hello”字符Ԍ而且JVM的字W缓存池q会~存q个字符Ԍ
String str = new String("hello");
此时E序除创建字W串外,str所引用的String对象底层q包含一个char[]数组Q这个char[]数组依次存放了h,e,l,l,o
-
11.单线E应量使用HashMap, ArrayList
HashTable,Vector{用了同步机制Q降低了性能?br />
-
12.量合理的创建HashMap
当你要创Z个比较大的hashMapӞ充分利用另一个构造函?/div>
public HashMap(int initialCapacity, float loadFactor)
避免HashMap多次q行了hash重构,扩容是一件很耗费性能的事Q在默认中initialCapacity只有16Q而loadFactor?.75Q需要多大的定wQ你最好能准确的估计你所需要的最佛_,同样的HashtableQVectors也是一L道理?br />
-
13.量减少对变量的重复计算
?/div>
for(int i=0;i<list.size();i++)
应该改ؓ
for(int i=0,len=list.size();i<len;i++)
q且在@环中应该避免使用复杂的表辑ּQ在循环中,循环条g会被反复计算Q如果不使用复杂表达式,而循环条gg变的话,E序会q行的更快?nbsp;
-
14.量避免不必要的创徏
?/div>
A a = new A();
if(i==1){list.add(a);}
应该改ؓ
if(i==1){
A a = new A();
list.add(a);}
-
15.量在finally块中释放资源
E序中用到的资源应当被释放Q以避免资源泄漏。这最好在finally块中d。不程序执行的l果如何Qfinally块L会执行的Q以保资源的正关闭?nbsp;
-
16.量使用UM来代?a/b'的操?/strong>
"/"是一个代价很高的操作Q用移位的操作会更快和更有效
?/div>
int num = a / 4;
int num = a / 8;
应该改ؓ
int num = a >> 2;
int num = a >> 3;
但注意的是用移位应d注释Q因为移位操作不直观Q比较难理解
-
17.量使用UM来代?a*b'的操?/strong>
同样的,对于'*'操作Q用移位的操作会更快和更有效
?/div>
int num = a * 4;
int num = a * 8;
应该改ؓ
int num = a << 2;
int num = a << 3;
-
18.量定StringBuffer的容?/strong>
StringBuffer的构造器会创Z个默认大?通常?6)的字W数l。在使用中,如果出q个大小Q就会重新分配内存,创徏一个更大的数组Qƈ原先的数组复制q来Q再丢弃旧的数组。在大多数情况下Q你可以在创?StringBuffer的时候指定大,q样避免了在容量不够的时候自动增长,以提高性能?nbsp;
如:StringBuffer buffer = new StringBuffer(1000);  
-
19.量早释放无用对象的引用
大部分时Q方法局部引用变量所引用的对?会随着Ҏl束而变成垃圾,因此Q大部分时候程序无需局部,引用变量昑ּ设ؓnull?/div>
例如Q?/div>
Public void test(){
Object obj = new Object();
……
Obj=null;
}
上面q个没必要了,随着Ҏtest()的执行完成,E序中obj引用变量的作用域q束了。但是如果是Ҏ下面Q?/div>
Public void test(){
Object obj = new Object();
……
Obj=null;
//执行耗时Q耗内存操作;或调用耗时Q耗内存的Ҏ
……
}
q时候就有必要将obj赋gؓnullQ可以尽早的释放对Object对象的引用?br />
-
20.量避免使用二维数组
二维数据占用的内存空间比一l数l多得多Q大?0倍以上?br />
-
21.量避免使用split
除非是必ȝQ否则应该避免用splitQsplit׃支持正则表达式,所以效率比较低Q如果是频繁的几十,几百万的调用会耗费大量资源Q如果确实需要频J的调用splitQ可以考虑使用apache的StringUtils.split(string,char)Q频Jsplit的可以缓存结果?br />
-
22.ArrayList & LinkedList
一个是U性表Q一个是链表Q一句话Q随机查询尽量用ArrayListQArrayList优于LinkedListQLinkedListq要Ud指针Q添加删除的操作LinkedList优于ArrayListQArrayListq要Ud数据Q不q这是理论性分析,事实未必如此Q重要的是理解好2者得数据l构Q对症下药?br />
-
23.量使用System.arraycopy ()代替通过来@环复制数l?/strong>
System.arraycopy() 要比通过循环来复制数l快的多 
-
24.量~存l常使用的对?/strong>
可能将l常使用的对象进行缓存,可以使用数组Q或HashMap的容器来q行~存Q但q种方式可能Dpȝ占用q多的缓存,性能下降Q推荐可以用一些第三方的开源工P如EhCacheQOscacheq行~存Q他们基本都实现了FIFO/FLU{缓存算法?br />
-
25.量避免非常大的内存分配
有时候问题不是由当时的堆状态造成的,而是因ؓ分配p|造成的。分配的内存块都必须是连l的Q而随着堆越来越满,扑ֈ较大的连l块来困难?br />
-
26.慎用异常
当创Z个异常时Q需要收集一个栈跟踪(stack track)Q这个栈跟踪用于描述异常是在何处创徏的。构些栈跟踪旉要ؓq行时栈做一份快照,正是q一部分开销很大。当需要创Z?Exception ӞJVM 不得不说Q先别动Q我惛_您现在的样子存一份快照,所以暂时停止入栈和出栈操作。栈跟踪不只包含q行时栈中的一两个元素Q而是包含q个栈中的每一个元素?/div>
如果您创Z?Exception Q就得付Z仗好在捕获异常开销不大Q因此可以?try-catch 核心内容包h。从技术上Ԍ您甚臛_以随意地抛出异常Q而不用花费很大的代h。招致性能损失的ƈ不是 throw 操作——管在没有预先创建异常的情况下就抛出异常是有点不d。真正要׃L是创建异常。幸q的是,好的~程习惯已教会我们,不应该不三七二十一抛出异常。异常是为异常的情况而设计的Q用时也应该牢记这一原则?br />
文章主要是ؓ了抛砖引玉,希望有更多牛人的指点

谢谢?nbsp;
xuanyuan 的徏议:
===================================================
7.慎用synchronizedQ尽量减synchronize的方?br />reQ同意,不过文中有个地方说错了,使用synchronized关键字ƈ不一定都是锁定当前对象的Q要看具体的锁是什么。如果是在方法上加的synchronizedQ则是以对象本n为锁的,如果是静态方法则锁的_度是类?br />---------------
9.量不要使用finalizeҎ
reQ同意,其实不推荐用finalizeҎ的根本原因在于,JVM的规范ƈ不保证何时执行该ҎQ所以用q个Ҏ来释放资源很不合适,有可能造成长时间资源得不到释放?br />---------------
16.量使用UM来代?a/b'的操作;17.量使用UM来代?a*b'的操?br />reQ个Z太同意这两条。这样做实有更好的性能Q但是却牺牲了可L。这两个操作W对很多E序员来说ƈ不直观。我认ؓ在如今硬件hg那么昂贵的情况下Q略微牺牲一些性能Q换来更好的可读性和可维护性是好的选择?br />
===================================================
19.量早释放无用对象的引用
大部分时Q方法局部引用变量所引用的对?会随着Ҏl束而变成垃圾,因此Q大部分时候程序无需局部,引用变量昑ּ设ؓnull?/span>
例如Q?/span>
Public void test(){
Object obj = new Object();
……
Obj=null;
}
上面q个没必要了,随着Ҏtest()的执行完成,E序中obj引用变量的作用域q束了。但是如果是Ҏ下面Q?/span>
Public void test(){
Object obj = new Object();
……
Obj=null;
//执行耗时Q耗内存操作;或调用耗时Q耗内存的Ҏ
……
}
如果Object obj = new Object(); 如果q对象ƈ不是大对象,q有必要吗?Obj=null;只是告诉jvmq个对象已经成ؓ垃圾Q至于什么时候回Ӟq不能确定! q可L也不好Q?/span>
===================================================


]]>说说MongoDB的ObjectIdhttp://m.tkk7.com/dongbule/archive/2011/06/12/352138.html陈于?/dc:creator>陈于?/author>Sun, 12 Jun 2011 10:30:00 GMThttp://m.tkk7.com/dongbule/archive/2011/06/12/352138.htmlhttp://m.tkk7.com/dongbule/comments/352138.htmlhttp://m.tkk7.com/dongbule/archive/2011/06/12/352138.html#Feedback8http://m.tkk7.com/dongbule/comments/commentRss/352138.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/352138.html前段旉有个朋友问我Q分布式主键生成{略在我们这Ҏ怎么实现的,当时我给的答案是sequenceQ当然这在不高ƈ发的情况下是没有M问题Q实际上Q我们的主键生成是可控的Q但如果是在分布式高q发的情况下Q那肯定是有问题的?br />
H然惌vmongodb的objectidQ记得以前看q文档,objectid是一U轻量型的,不同的机器都能用全局唯一的同U方法轻量的生成它,而不是采用传l的自增的主键策略,因ؓ在多台服务器上同步自动增加主键既费力又费Ӟ不得不佩服,mongodb从开始设计就被定义ؓ分布式数据库?br />下面深入一Ҏȝq个Objectid的底l,在mongodb集合中的每个document中都必须有一?_id"建,q个键的值可以是Mcd的,在默认的情况下是个Objectid对象?br />当我们让一个collection中插入一条不带_id的记录,pȝ会自动地生成一个_id的key

> db.t_test.insert({"name":"cyz"})
> db.t_test.findOne({"name":"cyz"})
{ "_id" : ObjectId("4df2dcec2cdcd20936a8b817"), "name" : "cyz" }

可以发现q里多出一个Objectidcd的_idQ当然了Q这个_id是系l默认生成的Q你也可以ؓ其指定一个|不过在同一collections中该值必L唯一?br />
?ObjectId("4df2dcec2cdcd20936a8b817")q串值拿出来q对照官|的解析来深入分析?br />
"4df2dcec2cdcd20936a8b817" 以这D字W串Z来进行解析,q是一?4位的字符Ԍ看v来很长,很难理解Q实际上它是由ObjectId(string)所创徏的一l十六进制的字符Q每个字节两位的十六q制数字Qd使用?2字节的存储空_可能有些朋友会感到很奇怪,居然是用?2个字节,而mysql的INTcd也只?个字节,不过按照现在的存储设备,多出来的q点字节也应该不会成Z么瓶颈,实际上,mongodb在设计上无处不在的体现着用空间换旉的思想Q接下看?br />
下面是官|指定Bson中ObjectId的详l规?br />


TimeStamp
?位是一个unix的时间戳Q是一个intcdQ我们将上面的例子中的objectid的前4位进行提?#8220;4df2dcec”Q然后再他们安装十六进制专为十q制Q?#8220;1307761900”Q这个数字就是一个时间戳Qؓ了让效果更佳明显Q我们将q个旉戌{换成我们习惯的时间格?/div>
$ date -d '1970-01-01 UTC 1307761900  sec'  -u
2011q?nbsp;06?nbsp;11?nbsp;星期?nbsp;03:11:40 UTC

?个字节其实隐藏了文档创徏的时_q且旉戛_在于字符的最前面Q这意味着ObjectId大致会按照插入进行排序,q对于某些方面v到很大作用,如作为烦引提高搜索效率等{。用时间戳q有一个好处是Q某些客L驱动可以通过ObjectId解析记录是何时插入的Q这也解{了我们qx快速连l创建多个ObjectidӞ会发现前几位数字很少发现变化的现实,因ؓ使用的是当前旉Q很多用h心要Ҏ务器q行旉同步Q其实这个时间戳的真实值ƈ不重要,只要其M停增加就好?/div>
Machine
接下来的三个字节Q就?2cdcd2 ,q三个字节是所在主机的唯一标识W,一般是机器L名的散列|q样q保了不同L生成不同的机器hash|保在分布式中不造成冲突Q这也就是在同一台机器生成的objectid中间的字W串都是一模一L原因?br />
pid
上面的Machine是ؓ了确保在不同机器产生的objectid不冲H,而pid是Z在同一台机器不同的mongodbq程产生了objectid不冲H,接下来的0936两位是产生objectid的进E标识符?br />
increment
前面的九个字节是保证了一U内不同机器不同q程生成objectid不冲H,q后面的三个字节a8b817Q是一个自动增加的计数器,用来保在同一U内产生的objectid也不会发现冲H,允许256?ơ方{于16777216条记录的唯一性?/div>
客户端生?/strong>
mongodb产生objectidq有一个更大的优势Q就是mongodb可以通过自n的服务来产生objectidQ也可以通过客户端的驱动E序来生,如果你仔l看文档你会感叹Qmongodb的设计无处不在的?br />
用空间换旉的思想Q比较objectid是轻量Q但服务端生也必须开销旉Q所以能从服务器转移到客L驱动E序完成的就量的{U,必须事务扔l客L来完成,减低服务端的开销Q另q有一点原因就是扩展应用层比扩展数据库层要变量得多?br />
好吧Q既然我们了解到我们的程序生objectid是在客户端完成,那再l箋Q进一步了解,打开mongodb java driver源码Q无源码可以到mongodb官网q行下蝲Q下面摘录部分代?br />
public class ObjectId implements Comparable<ObjectId> , java.io.Serializable {

    
final int _time;
    
final int _machine;
    
final int _inc;

    
public ObjectId( byte[] b ){
        
if ( b.length != 12 )
            
throw new IllegalArgumentException( "need 12 bytes" );
        ByteBuffer bb 
= ByteBuffer.wrap( b );
        _time 
= bb.getInt();
        _machine 
= bb.getInt();
        _inc 
= bb.getInt();
        _new 
= false;
    }
    
    
public ObjectId( int time , int machine , int inc ){
        _time 
= time;
        _machine 
= machine;
        _inc 
= inc;
        _new 
= false;
    }
    
    
public ObjectId(){
        _time 
= (int) (System.currentTimeMillis() / 1000);
        _machine 
= _genmachine;
        _inc 
= _nextInc.getAndIncrement();
        _new 
= true;
    }


Q完整代码请查看源码Q?/span>

q里可以发现ObjectId的构建可以有多种方式Q可以由自己制定字节Q也可以指定旉Q机器码和自增|q里重点看看驱动E序默认的构建,也就是public ObjectId()
可以看到objectid主要由_time _machine _inc 所l成Q其?_time直接?System.currentTimeMillis() / 1000)计算出所谓的旉戻Iq里很简单,接下来是重点Q主要看?strong>机器码和q程?/strong>的构?/div>

 private static final int _genmachine;
    
static {

        
try {
            final int machinePiece;//机器码块
            {
                StringBuilder sb 
= new StringBuilder();
                Enumeration
<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();//NetworkInterface此类表示一个由名称和分配给此接口的 IP 地址列表l成的网l接口,它用于标识将多播l加入的本地接口Q这里通过NetworkInterface此机器上所有的接口
                while ( e.hasMoreElements() ){
                    NetworkInterface ni 
= e.nextElement();
                    sb.append( ni.toString() );
                }
                machinePiece 
= sb.toString().hashCode() << 16//得到所有接口的字符串进行取散列?/span>
                LOGGER.fine( "machine piece post: " + Integer.toHexString( machinePiece ) );
            }

            final int processPiece;//q程?/span>
            {
                
int processId = new java.util.Random().nextInt();
                
try {
                    processId 
= java.lang.management.ManagementFactory.getRuntimeMXBean().getName().hashCode();//RuntimeMXBean是Java虚拟机的q行时系l的理接口Q这里是q回表示正在q行?nbsp;Java 虚拟机的名称Qƈq行取散列倹{?/span>
                }
                
catch ( Throwable t ){
                }

                ClassLoader loader 
= ObjectId.class.getClassLoader();
                
int loaderId = loader != null ? System.identityHashCode(loader) : 0;

                StringBuilder sb 
= new StringBuilder();
                sb.append(Integer.toHexString(processId));
                sb.append(Integer.toHexString(loaderId));
                processPiece 
= sb.toString().hashCode() & 0xFFFF;
                LOGGER.fine( 
"process piece: " + Integer.toHexString( processPiece ) );
            }
            _genmachine 
= machinePiece | processPiece; //最后将机器码块的散列gq程块的散列D行位或运,得到 _genmachine 
            LOGGER.fine( "machine : " + Integer.toHexString( _genmachine ) );
        }
        
catch ( java.io.IOException ioe ){
            
throw new RuntimeException( ioe );
        }
    }

 Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
               while ( e.hasMoreElements() ){
                    NetworkInterface ni = e.nextElement();
                    sb.append( ni.toString() );
                }
 machinePiece = sb.toString().hashCode() << 16;

q里的NetworkInterface.getNetworkInterfaces();取得的接口通常是按名称Q如 "le0"Q区分的Q大U是下面的类?/div>
name:lo (Software Loopback Interface 1) index: 1 addresses:
/0:0:0:0:0:0:0:1;
/127.0.0.1;
name:net0 (WAN Miniport (SSTP)) index: 
2 addresses:
name:net1 (WAN Miniport (IKEv2)) index: 
3 addresses:
name:net2 (WAN Miniport (L2TP)) index: 
4 addresses:
name:net3 (WAN Miniport (PPTP)) index: 
5 addresses:
name:ppp0 (WAN Miniport (PPPOE)) index: 
6 addresses:

q里Z么要采取q样斚wq行取散列|感觉有些不太理解Q应该网l接口本w相对而言是ƈ不稳定的

int processId = new java.util.Random().nextInt();
 try {
        processId = java.lang.management.ManagementFactory.getRuntimeMXBean().getName().hashCode();
 }
 catch ( Throwable t ){
}

RuntimeMXBean是Java虚拟机的q行时系l的理接口Q这里是q回表示正在q行?Java 虚拟机的名称Qƈq行取散列|如果在这q程中出现异常,processId 以随机数的方式l箋计算

_genmachine = machinePiece | processPiece;
最后将机器码块的散列gq程块的散列D行位或运,当然q里是十q制Q你把这里的十进制专为十六进Ӟ׃发现q块的值就是生产objectid中间部分的|q里的构服务端的构徏是有些不一LQ不q最基本的构建元素还是一致的Q就?strong>TimeStampQMachine QpidQincrement?br />
mongodb的ObejctId生思想在很多方面挺值得我们借鉴的,特别是在大型分布式的开发,如何构徏轻量U的生Q如何将生的负载进行{U,如何以空间换取时间提高生产的最大优化等{?/div>
----------------------------------------

by 陈于?
QQ:34174409
Mail: dongbule@163.com


]]>解答一下几道笔试题http://m.tkk7.com/dongbule/archive/2011/04/21/348723.html陈于?/dc:creator>陈于?/author>Thu, 21 Apr 2011 09:23:00 GMThttp://m.tkk7.com/dongbule/archive/2011/04/21/348723.htmlhttp://m.tkk7.com/dongbule/comments/348723.htmlhttp://m.tkk7.com/dongbule/archive/2011/04/21/348723.html#Feedback9http://m.tkk7.com/dongbule/comments/commentRss/348723.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/348723.html
    1.cookie和session的作用以及他们的应用场合
    2.怎样让jvm加蝲一个Class的同时执行一D代?br />     3.post和get区别
    4.事务的属性有哪些Q写出spring或jdbc理事务的例?br />     5.实现一个高q发、高性能的hashmap。写Z代码
    6.解析一DxmlQ拼接成一个url?br />     7.怎么解决q发Q怎么解决集群环境下的q发?
    8.java GC的原?
    9.mysql支持事务吗?mysql存储引擎有哪些?
    10.数据库数据查找比较慢的时候,如何解决Q?br />     11.学生 评 选课的一个sql语句书写
    12.查找日志文g中某一个信息出现的ơ数
    13.32位linux操作pȝ中,最多支持多个U程Q?br />     14.关于TreeMap操作?br />     15.重写cA的equals和hascodeҎ。类A有个属性private B bQ?br />     16.实现一个mysql的sequence?br />
下面是我的一点解{,也不知道对不对,现在的笔试要你白UR字写些东西真是难啊!

1)cookie和session的作用以及他们的应用场合
两者都是记录用戯问网站信息,但他们的保持状态不同,cookie是采用客L保持Q而session是采用服务端保持Q一般来说cookie不是很安全,因ؓ别h可以分析本地的cookieq行cookieƺ骗Q考虑到安全的问题应该使用sessionQsession是保持在服务端,但当讉K增多Ӟ会增加服务器的压力?br /> 在应用场合中Q徏议徏用户dQ交易重要信息存放sessionQ其他信息可以存攑֜cookie中,Ҏ实际需求两者可以交替搭配用?br />
2)怎样让jvm加蝲一个Class的同时执行一D代?/strong>
使用Class.forName()动态加载类Q题目中?同时"Q是否启用另一U程Q)

3)post和get区别
从http动词协议来看Qget是从服务器获取数据,post是向服务器上传数?br /> get方式会把数据出现在url中,post则不?br /> get提交的数据有大小限制Qpost则无

4)事务的属性有哪些Q写出spring或jdbc理事务的例?/strong>
有原子性,一致性,隔离性,持久?br /> spring提供了丰富的事务模型Q主要是针对jdbc事务Qjta事务Q还有一些自定义事务Q如果有从一张白U写出spring transaction的例子,那真是佩服之极)
q是写一个简单jdbc事务
try{
class.forName("***");
...
conn=DriverManager.getConnection(url,user,password);
conn.createStartement().executeUpdate("****");//更新操作
conn.commit();//事务提交
}catch(Exception e){
..
conn.rollback();//事务回滚
..
}

5)实现一个高q发、高性能的hashmap。写Z代码
什么叫做高性能Q高q发的hashmap? 是要考hashmap的数据结??

6)解析一DxmlQ拼接成一个url
xml,url???q两者有什么联pd?

7)怎么解决q发Q怎么解决集群环境下的q发?
Q有很多点可以ƈ发,webh可以q发Q数据库h可以q发Q怎么那么I泛Q?br /> 高ƈ发最重要的还是考虑到锁的问题,如数据库该用表锁用表锁,该用排他锁用排它锁,E序中要注意非线E安全的问题
高ƈ发还有带来性能问题Q缓存,负蝲均衡Q这些看情况而定

8)java GC的原?
又来Q不回答?br />
9)mysql支持事务吗?mysql存储引擎有哪些?
支持Q常用的是inndb和myisam

10)数据库数据查找比较慢的时候,如何解决Q?/strong>
假如是mysql
1.漏了索引,用explain看看查询语句是不是没使用索引或者烦引用有问题
2.使用show processlist看看是不是有sql卡住?br /> 3.q有一U情况就是sql写得q于复杂Q如goroupbyQ多表关联等{,使用desc来进行分?br /> 4.q有是配置的问题,如key_buffer{参敎ͼq个看文档吧
5.最后一U就是机器负药高,调整l构来解军_Q比如用mysql-proxy代理{等

11)学生 评 选课的一个sql语句书写
??

12)查找日志文g中某一个信息出现的ơ数
cat 日志 | grep 信息 | wc -l

13) 32位linux操作pȝ中,最多支持多个U程Q?/strong>
上网查了Q写得很复杂Q附上文?br /> http://www.517sou.net/Article/Linux-maximum-number-of-threads-and-the-maximum-number-of-processes.aspx

14)关于TreeMap操作?/strong>
TreeMap是一颗^衡排序二叉树Q操作的。。后面是什??

15)重写cA的equals和hascodeҎ。类A有个属性private B bQ?/strong>
??

16)实现一个mysql的sequence
mysql的last_insert_id()大致可以满Q如果不行就写触发器?br />


]]>
制定hessian服务端的讉K列表http://m.tkk7.com/dongbule/archive/2010/12/21/341246.html陈于?/dc:creator>陈于?/author>Tue, 21 Dec 2010 08:05:00 GMThttp://m.tkk7.com/dongbule/archive/2010/12/21/341246.htmlhttp://m.tkk7.com/dongbule/comments/341246.htmlhttp://m.tkk7.com/dongbule/archive/2010/12/21/341246.html#Feedback0http://m.tkk7.com/dongbule/comments/commentRss/341246.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/341246.html
对org.springframework.remoting.caucho.HessianServiceExporterq行重写

/**
 * 重写HessianServiceExporter.handleRequest(),拦截获取q端调用信息
 * 
@author chenyz
 *
 
*/
public class HouseHessianServiceExporter extends HessianServiceExporter {
    
    
private static String[] entryIP = {"192.168.0.1","192.168.0.3","192.168.0.3"};
    
    
private static Log log = LogFactory.getLog("Myremote"); 
    
    @Override
    
public void handleRequest(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {        
        log.info(
"try ==>remote 's IP:"+IpUtil.getIpAddr(request)+"remote 's host: "+request.getRemoteHost());
        
int call = 0;
        
for(String ip:entryIP){
            
if(ip.equals(IpUtil.getIpAddr(request)))
                call
++;
        }
        
if(call>0){
            log.info(
"call ==>remote 's IP:"+IpUtil.getIpAddr(request)+"remote 's host: "+request.getRemoteHost());
            
super.handleRequest(request, response);
        }
    }
}

   <bean id="shineLibWSImpl" class="com.***.shine.remote.ShineLibWSImpl"/>
    
<bean name="/remote/shineinfo" class="com.***.shine.hessian.service.HouseHessianServiceExporter">
        
<property name="service">
            
<ref bean="shineLibWSImpl"/>
        
</property>
        
<property name="serviceInterface">
            
<value>com.***.shine.remote.ShineLibWebService</value>
        
</property>
    
</bean>

重写HessianServiceExporter.handleRequest(),拦截获取q端调用信息,提取用端的ip信息与服务端制定的ip列表q行Ҏ,q将所有调用信息记录日?br />
如果直接使用hessian的api的HessianServlet,直接对HessianServlet的service()重写,拦截q提取远端调用信?br />
/**
 * 重写HouseHessianServlet.service(),拦截获取q端调用信息
 * 
@author chenyz
 *
 
*/
public class HouseHessianServlet extends HessianServlet{
    
    
private static String[] entryIP = {"192.168.0.1","192.168.0.3","192.168.0.3"};
    
    
private static Log log = LogFactory.getLog("Myremote"); 
    
    @Override
    
public void service(ServletRequest request, ServletResponse response)
            
throws IOException, ServletException {
        HttpServletRequest req 
= (HttpServletRequest) request;
        log.info(
"try ==>remote 's IP:"+IpUtil.getIpAddr(req)+"remote 's host: "+request.getRemoteHost());
        
int call = 0;
        
for(String ip:entryIP){
            
if(ip.equals(IpUtil.getIpAddr(request)))
                call
++;
        }
        
if(call>0){
            log.info(
"call ==>remote 's IP:"+IpUtil.getIpAddr(req)+"remote 's host: "+request.getRemoteHost());
            
super.service(request, response);
        }
    }
}

web.xml

   <servlet>   
    
<servlet-name>HessianServlet</servlet-name>   
    
<servlet-class>   
        com.***.product.remote.Htest
    
</servlet-class>   
  
</servlet>  

  
<servlet-mapping>   
    
<servlet-name>HessianServlet</servlet-name>   
    
<url-pattern>/service/hession</url-pattern>   
  
</servlet-mapping>  

com.***.product.remote.Htest

public class Htest extends HouseHessianServlet implements IHtest{
    
    
public hello(){
        System.out.println(
"hello");
    }
}




]]>
解决hessianq程调用q接时的问?/title><link>http://m.tkk7.com/dongbule/archive/2010/12/16/340894.html</link><dc:creator>陈于?/dc:creator><author>陈于?/author><pubDate>Thu, 16 Dec 2010 06:46:00 GMT</pubDate><guid>http://m.tkk7.com/dongbule/archive/2010/12/16/340894.html</guid><wfw:comment>http://m.tkk7.com/dongbule/comments/340894.html</wfw:comment><comments>http://m.tkk7.com/dongbule/archive/2010/12/16/340894.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://m.tkk7.com/dongbule/comments/commentRss/340894.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/dongbule/services/trackbacks/340894.html</trackback:ping><description><![CDATA[目前几套pȝ中主要用的hessianq行q程调用webservice服务的有hessian的HessianProxyFactory(com.caucho.hessian.client.HessianProxyFactory)和spring的HessianProxyFactoryBean(org.springframework.remoting.caucho.HessianProxyFactoryBean).<br /> <br /> <strong>1.HessianProxyFactory</strong><br /> 查看HessianProxyFactory源码后发?hessian在创建httphq接webservice服务q没有对q接时q行相关的参数设|?所以当|络出现问题׃造成整个hessian处理的阻?q而阻塞整个线E后l的处理<br /> 以下是HessianProxyFactory对连接处理的源码<br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> URLConnection openConnection(URL url)<br />     </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> IOException<br />   {<br />     URLConnection conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> url.openConnection();<br /> <br />     conn.setDoOutput(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br /> <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (_readTimeout </span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) {<br />       </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />     conn.setReadTimeout((</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) _readTimeout);<br />       } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Throwable e) {<br />       }<br />     }<br /> <br />     conn.setRequestProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Content-Type</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">x-application/hessian</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (_basicAuth </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br />       conn.setRequestProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Authorization</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, _basicAuth);<br />     </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (_user </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&&</span><span style="color: rgb(0, 0, 0);"> _password </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />       _basicAuth </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Basic </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> base64(_user </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> _password);<br />       conn.setRequestProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Authorization</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, _basicAuth);<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> conn;<br />   }</span></div> <br /> 所以我们针Ҏ逻辑l承q写该openConnectionҎ,在创建httpq接的时候通过讄q接时旉来解军_|络问题dE序l箋的问?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyHessianProxyFactory </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> HessianProxyFactory {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> connectTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10000</span><span style="color: rgb(0, 0, 0);">;<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> readTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10000</span><span style="color: rgb(0, 0, 0);">;<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getConnectTimeOut() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> connectTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setConnectTimeOut(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> connectTimeOut) {<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.connectTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> connectTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getReadTimeOut() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> readTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setReadTimeOut(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> readTimeOut) {<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.readTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> readTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> URLConnection openConnection(URL url) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> IOException {<br />         URLConnection conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> url.openConnection();<br />         conn.setDoOutput(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.connectTimeOut </span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) {<br />             conn.setConnectTimeout(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.connectTimeOut);<br />         }<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.readTimeOut </span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) {<br />             conn.setReadTimeout(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.readTimeOut);<br />         }<br />         conn.setRequestProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Content-Type</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">x-application/hessian</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (_basicAuth </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br />                   conn.setRequestProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Authorization</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, _basicAuth);<br />             </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (_user </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&&</span><span style="color: rgb(0, 0, 0);"> _password </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />                   _basicAuth </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Basic </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> base64(_user </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> _password);<br />                   conn.setRequestProperty(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Authorization</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, _basicAuth);<br />             }<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> conn;<br />     }<br /> }</span></div> <br /> <strong>2.HessianProxyFactoryBean</strong><br /> 查看spring的HessianProxyFactoryBean源码发现,它在装hessian是直接创Z个HessianProxyFactory实例,然后利用该实例完成创E服?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> HessianProxyFactoryBean </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> HessianClientInterceptor </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> FactoryBean {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Object serviceProxy;<br /> <br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> afterPropertiesSet() {<br />         </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.afterPropertiesSet();<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.serviceProxy </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ProxyFactory.getProxy(getServiceInterface(), </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">);<br />     }<br />     <br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Object getObject() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.serviceProxy;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Class getObjectType() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> getServiceInterface();<br />     }<br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> isSingleton() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />     }<br /> <br /> }</span></div> <br /> 所以对此的解决Ҏ与上面差不多,l承HessianProxyFactoryBean然后加入相应的连接超时和d时的变?重写afterPropertiesSetҎ,q且同时完成上面W一步对HessianProxyFactory的改?q样p保证q接q程webserver服务器时不会因ؓ|络原因dE序的执?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyHessianProxyFactoryBean </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> HessianProxyFactoryBean {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> MyHessianProxyFactory proxyFactory </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MyHessianProxyFactory();<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> readTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10000</span><span style="color: rgb(0, 0, 0);">;<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> connectTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10000</span><span style="color: rgb(0, 0, 0);">;<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getReadTimeOut() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> readTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setReadTimeOut(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> readTimeOut) {<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.readTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> readTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getConnectTimeOut() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> connectTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setConnectTimeOut(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> connectTimeOut) {<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.connectTimeOut </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> connectTimeOut;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> afterPropertiesSet() {<br />         proxyFactory.setReadTimeout(readTimeOut);<br />         proxyFactory.setConnectTimeOut(connectTimeOut);<br />         setProxyFactory(proxyFactory);<br />         </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.afterPropertiesSet();<br />     }<br /> }</span></div> <br /> <br /> <img src ="http://m.tkk7.com/dongbule/aggbug/340894.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/dongbule/" target="_blank">陈于?/a> 2010-12-16 14:46 <a href="http://m.tkk7.com/dongbule/archive/2010/12/16/340894.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个伴随已久的旉操作工具c?/title><link>http://m.tkk7.com/dongbule/archive/2010/12/09/340174.html</link><dc:creator>陈于?/dc:creator><author>陈于?/author><pubDate>Thu, 09 Dec 2010 10:37:00 GMT</pubDate><guid>http://m.tkk7.com/dongbule/archive/2010/12/09/340174.html</guid><wfw:comment>http://m.tkk7.com/dongbule/comments/340174.html</wfw:comment><comments>http://m.tkk7.com/dongbule/archive/2010/12/09/340174.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/dongbule/comments/commentRss/340174.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/dongbule/services/trackbacks/340174.html</trackback:ping><description><![CDATA[     摘要: 下面一个伴随了好几个工E的旉操作的工L,提供了一些常用的旉操作和计的Ҏ,每段旉都会q行一ơ整?希望能去冗余和得到好的扩? Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->package com.***....  <a href='http://m.tkk7.com/dongbule/archive/2010/12/09/340174.html'>阅读全文</a><img src ="http://m.tkk7.com/dongbule/aggbug/340174.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/dongbule/" target="_blank">陈于?/a> 2010-12-09 18:37 <a href="http://m.tkk7.com/dongbule/archive/2010/12/09/340174.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>memcached一些应用点?/title><link>http://m.tkk7.com/dongbule/archive/2010/12/08/340066.html</link><dc:creator>陈于?/dc:creator><author>陈于?/author><pubDate>Wed, 08 Dec 2010 07:00:00 GMT</pubDate><guid>http://m.tkk7.com/dongbule/archive/2010/12/08/340066.html</guid><wfw:comment>http://m.tkk7.com/dongbule/comments/340066.html</wfw:comment><comments>http://m.tkk7.com/dongbule/archive/2010/12/08/340066.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://m.tkk7.com/dongbule/comments/commentRss/340066.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/dongbule/services/trackbacks/340066.html</trackback:ping><description><![CDATA[大名鼎鼎的分布式~存pȝmemcachedQ在开源社Z可谓是无Z知无Z晓,memcached支持分布式的横向扩展Q但memcached的服务端却是单实例,q无"分布?的功能,所谓的分布式只是客L在存储的主键做分布的存储Q还有memcachedlg~存对象Q如果组件无q行序列化必定无法正取得数据;如何使用memcached的javalg来监控memcached的运行状态;以上{等的问题是我在日常的工作中到q解决的,拿出来跟大家做个分n^_^<br /> <br /> <hr width="100%" size="2" /> <br /> <strong>对象的序列化</strong><br /> 首先memcached是独立的服务器组?独立于应用系l?从客L保存和读取对象到memcached是必通过|络传输,因ؓ|络传输都是二进制的数据,所以所有的对象都必ȝq序列化,否则无法存储到memcahced的服务器?<br /> 正如我们以往在集中应用的序列化一?memcached的序列化的性能也是往往让大家头?如果我们Ҏ们的domainc进行对象的序列?W一ơ序列化旉会比较长,但后l会优化,也就是说序列化最大的消耗不是对象的序列?而是cȝ序列?如果存储的只是一个String对象,q种情况是最理想?省去了序列化的操?实际上String对象本n已经实现了序列化接口,无法我们再次去进行序列化操作.<br /> <br /> <hr width="100%" size="2" /> <br /> <strong>memcached的原子加?/strong><br /> 记录一下上ơ犯得一个错?br /> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><%</span><span style="color: #000000;"><br /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> count </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> count</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br /> MemCachedClient mcc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> MemCachedClient();<br /> mcc.add(</span><span style="color: #000000;">"</span><span style="color: #000000;">test.html</span><span style="color: #000000;">"</span><span style="color: #000000;">, count);<br /> </span><span style="color: #000000;">%></span></div> <br /> q段代码的作用是test.html的用戯问次C存到memcached?_劣一看好像ƈ无错?但在高ƈ发时的出来的讉K数据一定是于实际的访问数?当然q里q不是memcached对象锁的问题,主要q是E序中线E的同步问题,但是如果使用java的synchronized或lock那么在性能上肯定是无法忍受?memcached客户端组件带有原子性的加法和减?br /> <br /> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><%</span><span style="color: #000000;"><br /> MemCachedClient mcc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> MemCachedClient();<br /> System.out.println(mcc.addOrIncr(</span><span style="color: #000000;">"</span><span style="color: #000000;">test.html</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br /> </span><span style="color: #000000;">%></span></div> <br /> long addORIncr(String key,long inc)数器值增加inc,如果计数器不存在,则保存inc数器的?必须注意的是服务器端不会对超q??2ơ方的行行检?br /> <br /> <hr width="100%" size="2" /> <br /> <br /> <strong>分布式的mencached</strong><br /> memcached虽然是属于分布式的缓存服务器,但实际上memcached服务端之间ƈ无分布式的功?不会互相通信׃n数据,如何q行分布?q完全是取决于客L的实?br /> <br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/dongbule/memcached/2.png" width="456" height="390" /><br /> <br /> 假设我们现在有三台memcached服务器分别ؓnode1Qnode2Qnode3Q应用程序要保存键名分别?test1","test2","test3",客户端实现的法是Ҏ键名来决定保存数据的memcached服务?我们?test1"保存到node1Q?test2"保存到node2Q?test3"保存到node3Qƈ且在d~存数据也是通过一L法从各台服务器上读取相应的key,q样通过一个最单的法不同的键保存到不同的服务器?实现了memcached的分布式.<br /> 但是q种法很难保每台服务器得到较为^均的数据?我们需要改变一下客L的算?单来?是Ҏ服务器的台数的余数进行分?br /> <br /> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><%<br /> "</span><span style="color: #000000;">test1</span><span style="color: #000000;">"</span><span style="color: #000000;">.hashCode()</span><span style="color: #000000;">%</span><span style="color: #000000;">3<br /> %><br /> </span></div> <br /> Ҏkey的java.lang.String.hashCode()取得散列?再将值模服务器的台数得到余数?我们再根据这个余数值来判定q个key要存入哪一台服务器,当key的数量越来越?对key的散列取模也会趋向^?基本可以保证几台memcached服务器所存储的缓存量向q_<br /> g很完?余数计算的方法很?数据的分散性也很优U,但也有其~点,是当需要添加或U除服务器时,~存的重l代h相当巨大?d或移除服务器?余数׃发生变化,q样无法取C原来~存时相同的服务?<br /> |上介绍的Consistent Hashing法基本上可以解册个问?q里做个单的说明,首先是求出memcached服务器节点的哈希?q将光|到0-2?2ơ方的圆?然后用同LҎ求出存储数据的键的哈希?q映到圆上.然后从数据映到的位|开始顺旉查找,数据保存到扑ֈ的第一个服务器?如果过2?2ơ方仍然找不到服务器,׃保存到第一台memcached服务器上<br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/dongbule/memcached/3.png" width="512" height="421" /><br /> <br /> 从上囄状态中d一台memcached服务器。余数分布式法׃保存键的服务器会发生巨大变化而媄响缓存的命中率,但Consistent Hashing中,只有在continuum上增加服务器的地炚w时针方向的W一台服务器上的键会受到影响<br /> <br /> <hr width="100%" size="2" /> <br /> <strong>几种q接客户端的Ҏ</strong><br /> 目前java的memcached主要有Java-Memcached-Client,Xmemached,Spymemcached三种,q三个客L的性能试可以?br /> http://xmemcached.googlecode.com/svn/trunk/benchmark/benchmark.html<br /> <br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/dongbule/memcached/4.png" width="942" height="612" /><br /> h的资源ؓ64Bytes,在低q发Java-Memcached-Client是占有一定的优势,但在q发数超q?00以后,Java-Memcached-Client是呈现直U下?q发数达?00已经无法承受,Spymemcached和Xmemached表现相对E_,特别是Xmemached无论在低q发或高q发都保持优U的性能表现<br /> <br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/dongbule/memcached/5.png" width="950" height="610" /><br /> <br /> q发数固定ؓ100?在小文g的请求Java-Memcached-Clientq是占有优势,当随着h的size来大,三者趋向于同一?br /> 如果你对memcached讉K的负载不?那么Java-Memcached-Client是一个不错的选择,如果你对memcached讉K的负载要求较?推荐使用Xmemached,如果需要异步的扚w处理,可以选择Spymemcached,如果你什么都不知?那么使用Xmemached,因ؓ无论在何U情?它都可以表现好的性能,虽然不是最?br /> <br /> <hr width="100%" size="2" /> <br /> <strong>监控memcached </strong><br /> 推荐使用nagios或cactisq行监控,nagios没有配置q?cactis是需要下载一个脚本插?br /> q里推荐一个从|上淘来的php,只要把它攑ֈ你的机器?当然你的机器要支持php环境,此php攑օ你的|页讉K|络可以访?br /> 下蝲<br /> <a href="/Files/dongbule/cacti/memcache.rar">http://m.tkk7.com/Files/dongbule/cacti/memcache.rar</a><br /> 修改php以下几个选项<br /> <br /> <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;">define</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">ADMIN_USERNAME</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">memcache</span><span style="color: #000000;">'</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Admin Username</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">define</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">ADMIN_PASSWORD</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">password</span><span style="color: #000000;">'</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Admin Password</span><span style="color: #008000;"><br /> </span><span style="color: #000000;"><br /> </span><span style="color: #800080;">$MEMCACHE_SERVERS</span><span style="color: #000000;">[] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">192.168.1.100:11211</span><span style="color: #000000;">'</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> add more as an array</span><span style="color: #008000;"><br /> #</span><span style="color: #008000;">$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array</span></div> <br /> 监控的^?br /> <br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/dongbule/memcached/6.png" width="992" height="422" /><br /> <br /> <hr width="100%" size="2" /> <br /> <strong>理解memcached的删除机?/strong><br /> memcached内部不会监视记录是否q期,而是在get时查看记录的旉?查记录是否过? q种技术被UCؓlazy(惰?expiration.因此,memcached不会在过期监视上耗费CPU旉<br /> memcached会优先用已时的记录的I间,q用LRU法来分配空?因此当memcached的内存空间不?׃最q违背用的记录中搜?q将I间分配l新的记?br /> 不过在某些情况下LRU机制会造成某些ȝ,如你q不惌淘汰已被~存q的记录,可以在memcached启动时添?-M 参数来禁止LRU,但这样在memcached的内存用时,memcached会返回错?是否使用LRU,在于你的需?br /> <br /> <br /> ----------------------------------------<br /> <br /> by 陈于?<br /> QQ:34174409<br /> Mail: dongbule@163.com<br /> <br /> <br /> <br /> <img src ="http://m.tkk7.com/dongbule/aggbug/340066.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/dongbule/" target="_blank">陈于?/a> 2010-12-08 15:00 <a href="http://m.tkk7.com/dongbule/archive/2010/12/08/340066.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>开发h员与软g试http://m.tkk7.com/dongbule/archive/2010/12/03/339721.html陈于?/dc:creator>陈于?/author>Fri, 03 Dec 2010 08:00:00 GMThttp://m.tkk7.com/dongbule/archive/2010/12/03/339721.htmlhttp://m.tkk7.com/dongbule/comments/339721.htmlhttp://m.tkk7.com/dongbule/archive/2010/12/03/339721.html#Feedback3http://m.tkk7.com/dongbule/comments/commentRss/339721.htmlhttp://m.tkk7.com/dongbule/services/trackbacks/339721.html
E序员和试人员的心理差?/strong>
E序员和试人员的心理差别可以简单的归纳Z下几U?br />
成功 / 不成?/strong>
什么才是一ơ成功的试,大多数的开发h员对自己的程序测试完没发现错?׃?q是一个成功的试",如果发现某些新的错误则称"q是不成功的试";而测试h员刚好相?当然q也是因为双方的职责不同而引L

l护 / 破坏,施虐
开发h员对试往往是一U维护性的试,目标在于证明自己开发的E序没有错误,可能跟我们开发h员经常做性工?更們֐创造事?而不是将事物破坏有关;而测试h员在试更多是一U破坏的q程,甚至是一U施?摆出一U把鸡蛋打碎搅黄来挑骨头的姿?br />
做了应该做的 / 做了不该做的
开发h员在做测试更多們֐于对实现的功能进行检?当测试证明了该完成的功能全部通过时就认ؓ试完毕?但程序即使能够完成预定的功能,也可能隐藏了错误,如做了应该做的东?同时也做了不应该做的东西

建立一U信?/ 必须?/strong>
试q个东西对于开发h员来?更多的是建立一U信心的保证,例如我们可能多的单元测?很多时候就是让我们信E序的正?q让我们建立信心,而对于测试h员来?试是他们的职责所?只有可能多的把pȝ挖出错误来才证明他们的存在h?br />
试的态度
试是一Ҏ术性的工作,但同时也涉及C心里学的一些重要的因素.
作ؓE序员的我们也经怼Ҏ们的E序q行相关的测?但你x我们Z么要Ҏ们的pȝ做测?原因大概有以下的几种

1.试是Z证明我们的系l是不存在错误的.
2.试的目的是惌明系l能够正完成预定的功能.
3.试是保证一个系l强壮性的信心q程.
4.试是上U规定要做的.

站在试人员的角度来?除了最后一?其他的都是本末倒置(难怪他们会抱怨我们做的测试很?

"试是Z证明我们的系l是不存在错误的",试想一?如果我们的目标是证明E序中不存在错误,那么我们的潜意识׃們֐于这个目?我们会們֐选择可能较少D出现E序失效的测试数?如果我们的测试目标证明程序中存在错误,我们设计的测试数据就可能更多的发现问?于前一U方法相?后一U方法会更多地增加程序的价?
每当试一个程?都是想ؓE序增加多一些的价?通过试发现pȝ的更多错误来增加E序的可靠性质?提高了程序的可靠?是指出ƈ最l修改程序的错误.?试是Z证明我们的系l是不存在错误的"比较,更适合的定义是
试时ؓ了发现错误而执行的q程

"试的目的是惌明系l能够正完成预定的功能",q个定义又会带来另一个问?E序即能够完成预定的功?也依然可能隐藏错?也就是说,当程序没有实现预期的功能,错误时能够清晰显C出?但程序做了不应该做的?q同样也是一个错?如我们程序实C一个除法计器,即我们通过各种数据正确的完成了计算d,但在完成了不应执行的d(除数?),E序依然是错误的,如果我们Y件测试作为发现错误的q程,而不是只是证?pȝ能够正确完成预定的功?,我们发现q类的错误可能性会打很?所以更好的定义?br /> 试是建立E序即能完成预定功?也发现存在完成非预定功能

ps:当然?试更多是跟软g成本斚w相关,q里׃l想?附上讲的ppt<实用的黑盒测试方?/a>>

----------------------------------------




]]>
[整理]工程中jar功能对照?/title><link>http://m.tkk7.com/dongbule/archive/2010/11/03/336905.html</link><dc:creator>陈于?/dc:creator><author>陈于?/author><pubDate>Wed, 03 Nov 2010 06:59:00 GMT</pubDate><guid>http://m.tkk7.com/dongbule/archive/2010/11/03/336905.html</guid><wfw:comment>http://m.tkk7.com/dongbule/comments/336905.html</wfw:comment><comments>http://m.tkk7.com/dongbule/archive/2010/11/03/336905.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://m.tkk7.com/dongbule/comments/commentRss/336905.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/dongbule/services/trackbacks/336905.html</trackback:ping><description><![CDATA[<br /> 把我上次的工E所用到的jar包做了一个整?也加了一些简单的描述,下面图是上次工程用到的所有jar?br /> <br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/dongbule/jar/1.png" width="549" height="600" /><br /> <br /> <table width="964" border="0" cellpadding="2" cellspacing="2" height="734"> <tbody> <tr> <td><strong>JAR包与描述对照?/strong></td> <td><strong>注:jar包尾后的版本号不代表当前最高版?/strong></td> </tr> <tr> <td>activation-1.1.jar</td> <td>Sun的JavaBeans Activation FrameworkQJAFQ?JavaMail要运行必M赖于它的支持</td> </tr> <tr> <td>asm-3.0.jar<br /> asm-commons-2.2.3.jar<br /> asm-util-2.2.3.jar<br /> </td> <td>asm是一个轻量字节码处理和分析框架</td> </tr> <tr> <td>alveole-struts2.jar<br /> alveole-tools.jar<br /> </td> <td> </td> </tr> <tr> <td valign="top">aspectjtools-1.5.3.jar<br /> </td> <td valign="top">Aspect提供的注释类库和相应的解析类?br /> </td> </tr> <tr> <td valign="top">atomikos-util.jar<br /> </td> <td valign="top">数据库提供分布式事务支持<br /> </td> </tr> <tr> <td valign="top">c3p0-0.8.5.2.jar<br /> </td> <td valign="top">C3PO是一个数据库q接?br /> </td> </tr> <tr> <td valign="top">cglib-nodep-2.2.jar<br /> </td> <td valign="top">CGLIB是一个强大的高质量高性能的代码生成库,在运行时可以用它来扩展Javac?br /> </td> </tr> <tr> <td valign="top">commons-attributes-api.jar<br /> </td> <td valign="top">Apache Commons包中的一?包含了一些属性的api<br /> </td> </tr> <tr> <td valign="top">commons-beanutils-1.6.jar<br /> </td> <td valign="top">Apache Commons包中的一个,包含了一些Bean工具cȝ<br /> </td> </tr> <tr> <td valign="top">commons-chain-1.1.jar<br /> </td> <td valign="top"><br /> </td> </tr> <tr> <td valign="top">commons-codec-1.3.jar<br /> </td> <td valign="top"><br /> </td> </tr> <tr> <td valign="top">commons-collections-3.2.jar<br /> </td> <td valign="top">Apache Commons包中的一?包含了一些Apache开发的集合c,功能比java.util.*强大<br /> </td> </tr> <tr> <td valign="top">commons-configuration.jar<br /> </td> <td valign="top">Apache Commons包中的一?用于协助d配置和首选项文g<br /> </td> </tr> <tr> <td valign="top">commons-digester-1.8.jar<br /> </td> <td valign="top">Apache Commons包中的一?通过它可以很方便的解析xml文g生成java对象<br /> </td> </tr> <tr> <td valign="top">commons-fileupload.jar<br /> </td> <td valign="top">Apache Commons包中的一?是一个通过Http接收上传的文件ƈ处理l果文g的库<br /> </td> </tr> <tr> <td valign="top">commons-lang-2.1.jar<br /> </td> <td valign="top">Apache Commons包中的一?包含了一些数据类型工LQ是java.lang.*的扩?br /> </td> </tr> <tr> <td valign="top">commons-httpclient-3.0.jar<br /> </td> <td valign="top">Apache Commons包中的一?提供一个框架啊处理http客户端的扩展<br /> </td> </tr> <tr> <td valign="top">commons-io.jar    <br /> </td> <td valign="top">Apache Commons包中的一?java.io.*的扩?输入输出,支持文g上传<br /> </td> </tr> <tr> <td valign="top">commons-logging-1.1.1.jar<br /> </td> <td valign="top">Apache Commons包中的一?包含日志功能<br /> </td> </tr> <tr> <td valign="top">commons-logging-api-1.1.jar<br /> </td> <td valign="top">Apache Commons包中的一?Commons Logging应用接口,一U超薄适配器允许其他可配置桥连?br /> </td> </tr> <tr> <td valign="top">commons-validator-1.3.0.jar<br /> </td> <td valign="top">Apache Commons包中的一?struts使用它进行表单验?br /> </td> </tr> <tr> <td valign="top">dom4j-1.6.1.jar    <br /> </td> <td valign="top">dom4j XML 解析?br /> </td> </tr> <tr> <td valign="top">ezmorph-1.0.4.jar<br /> </td> <td valign="top">json和java转化的辅助工?br /> </td> </tr> <tr> <td valign="top">freemarker-2.3.8.jar<br /> </td> <td valign="top">freemarker表现层模板工?br /> </td> </tr> <tr> <td valign="top">hessian-3.1.3.jar<br /> </td> <td valign="top">一个轻量的remoting onhttp工具Q用简单的Ҏ提供了RMI的功?br /> </td> </tr> <tr> <td valign="top">htmlparser.jar<br /> </td> <td valign="top">强大的html解析?br /> </td> </tr> <tr> <td valign="top">httpsqs4j.jar<br /> </td> <td valign="top">一Ƒ֟?HTTP GET/POST 协议的轻量开源简单消息队列服?br /> </td> </tr> <tr> <td valign="top">ibatis-2.3.3.720.jar<br /> </td> <td valign="top">iBATIS提供的持久层框架包括SQL Maps和Data Access ObjectsQDAOQ?br /> </td> </tr> <tr> <td valign="top">jamon-2.7.jar<br /> </td> <td valign="top">一个小巧的Q免费的Q高性能的,U程安全的性能监测工具<br /> </td> </tr> <tr> <td valign="top">java_memcached-release.jar<br /> </td> <td valign="top">memcachedq接的java客户?br /> </td> </tr> <tr> <td valign="top">jfreechart-1.0.1.jar<br /> </td> <td valign="top">使用java生成图表的工?br /> </td> </tr> <tr> <td valign="top">jcommon-1.0.8.jar<br /> </td> <td valign="top">使用java生成图表的工?br /> </td> </tr> <tr> <td valign="top">json-lib-2.2-jdk15.jar<br /> </td> <td valign="top">json和java转化的辅助工?br /> </td> </tr> <tr> <td valign="top">jta.jar<br /> </td> <td valign="top">Java事务API,为J2EEq_提供了分布式事务服务<br /> </td> </tr> <tr> <td valign="top">junit-3.8.1.jar<br /> </td> <td valign="top">是一个开放源代码的Java试框架Q用于编写和q行可重复的试。他是用于单元测试框架体pxUnit的一个实?br /> </td> </tr> <tr> <td valign="top">jxl.jar<br /> </td> <td valign="top">通过java操作excel表格的工L?br /> </td> </tr> <tr> <td valign="top">log4j-1.2.9.jar<br /> </td> <td valign="top">通过使用Log4jQ我们可以控制日志信息输送的目的地是控制台、文件、GUIlg、甚x套接口服务器<br /> </td> </tr> <tr> <td valign="top">lucene-core-2.3.2.jar<br /> </td> <td valign="top">是一个全文检索引擎的架构Q提供了完整的查询引擎和索引引擎<br /> </td> </tr> <tr> <td valign="top">mongo-2.2.jar<br /> </td> <td valign="top">javaq接mongodb客户端接?br /> </td> </tr> <tr> <td valign="top">morphia.jar</td> <td valign="top">是一个轻量的类型安全的JavacdQ用来将在MongoDB和Java对象之间q行映射</td> </tr> <tr> <td valign="top">mysql-connector-java.jar<br /> </td> <td valign="top">mysqlq接驱动<br /> </td> </tr> <tr> <td valign="top">ognl-2.6.11.jar<br /> </td> <td valign="top">OGNL表达?支持EL<br /> </td> </tr> <tr> <td valign="top">oscache-2.3.2.jar<br /> </td> <td valign="top">Java对象的缓存工?比ehcache的优势在于页面缓?br /> </td> </tr> <tr> <td valign="top">poi-3.0.1.jar<br /> </td> <td valign="top">apache工具,用于操作pdf和excel的工L<br /> </td> </tr> <tr> <td valign="top">quartz-1.6.0.jar<br /> </td> <td valign="top">自动d处理功能cd<br /> </td> </tr> <tr> <td valign="top">proxool-0.9.1.jar<br /> </td> <td valign="top">Proxool JDBC q接?工具<br /> </td> </tr> <tr> <td valign="top">servlet.jar<br /> </td> <td valign="top"><br /> </td> </tr> <tr> <td valign="top">spring-2.0.2.jar</td> <td valign="top"><br /> </td> </tr> <tr> <td valign="top">spring-2.0.2.jar<br /> </td> <td valign="top"><br /> </td> </tr> <tr> <td valign="top">velocity-1.6.3-dep.jar<br /> velocity-tools-1.4.jar    <br /> </td> <td valign="top">velocityZjava的模板引?允许用简单的模板语言来引用java定义的对?br /> </td> </tr> <tr> <td valign="top">xwork-core-2.1.6.jar<br /> </td> <td valign="top">web work的核心库<br /> </td> </tr> </tbody> </table> <br /> <hr width="100%" size="2" /> <br /> 下蝲<<a >http://download.csdn.net/source/2806071</a>><br /> <br /> ?blogjava上传不了,我传到csdn<br /> <br /> ----------------------------------------<br /> <br /> by 陈于?<br /> QQ:34174409<br /> Mail: dongbule@163.com<br /> <br /> <br /> <img src ="http://m.tkk7.com/dongbule/aggbug/336905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/dongbule/" target="_blank">陈于?/a> 2010-11-03 14:59 <a href="http://m.tkk7.com/dongbule/archive/2010/11/03/336905.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>解析xml旉到的一些问?/title><link>http://m.tkk7.com/dongbule/archive/2010/09/28/333262.html</link><dc:creator>陈于?/dc:creator><author>陈于?/author><pubDate>Tue, 28 Sep 2010 09:10:00 GMT</pubDate><guid>http://m.tkk7.com/dongbule/archive/2010/09/28/333262.html</guid><wfw:comment>http://m.tkk7.com/dongbule/comments/333262.html</wfw:comment><comments>http://m.tkk7.com/dongbule/archive/2010/09/28/333262.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://m.tkk7.com/dongbule/comments/commentRss/333262.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/dongbule/services/trackbacks/333262.html</trackback:ping><description><![CDATA[先感谢同事renial?lt;解析xml旉到的一些问?gt;技术分?下面是一些记录和实际操作<br /> <br /> <strong>1.使用Dom4j解析大文件时内存溢出的问?/strong><br /> <br /> 问题是这L,当我用dom4j去解析一个几十M的xml?出现out of memory.当然?q也是根据你的机器性能而定?我们都知道dom4j在各UDOM解析器中应该是性能最好的,q大名鼎鼎的Hibernate都是用dom4j来解析XML配置文g?br /> 问题出在于用dom4j的SAXReader是会把整个XML文g一ơ性读?如果XML文gq大׃抛出out of memory,但即使是使用SAXParser扚wd解析,但它也是一ơ解析完,假设XML文g有几万条数据,那么解析后就必须在内存放入这几万条对?<br /> <br /> 常用的Dom4j文g解析方式Q?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 0);">InputStream is </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileInputStream(filePath);<br /> SAXReader reader </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> SAXReader();  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">整个XML构徏Z个Document</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">Document doc </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> reader.read(is);<br /> Element root </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> doc.getRootElement(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 获得根节?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (Object obj : root.elements()) { </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 遍历每个节点</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">Element e </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Element)obj;            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 对当前节点进行操?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">}<br /> </span></div> <br /> 解决Ҏ:使用ElementHandler解析文g<br /> <br /> 通过查API可以发现ElementHandler接口,下面是接口的介绍<br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 0);">ElementHandler </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> defines a handler of Element objects. It is used primarily in event based processing models such as </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> <br /> processing large XML documents as they are being parsed rather than waiting until the whole document is parsed. <br /> </span></div> <br /> 好了,它就是我们想要的,通过实现以下两个method,可以达成我们的需?br /> <br /> onEnd(ElementPath elementPath)<br />           Called by an event based processor when an elements closing tag is encountered.<br /> onStart(ElementPath elementPath)<br />           Called by an event based processor when an elements openning tag is encountered.<br /> <br /> 下面是代?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 0);">FileInputStream fis </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileInputStream(addPath);<br /> SAXReader reader </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> SAXReader();<br /> ElementHandler addHandler </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MyElementHandler();  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">建立 MyElementHandler 的实?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">reader.addHandler(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/root/test1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, addHandler); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 节点</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">reader.addHandler(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/root/test2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, addHandler); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 节点</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">reader.read(fis);</span></div> <br /> ...<br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyElementHandler </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> ElementHandler {<br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onStart(ElementPath ep) {}<br />   <br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onEnd(ElementPath ep) {<br />     Element e </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ep.getCurrent(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 获得当前节点<br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 对节点进行操作。。?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    e.detach(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 处理完当前节点后Q将其从dom树中剪除</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">  }<br /> }</span></div> <br /> 因ؓ每次处理完一个节点后q没有保存在dom树中Q所以不会出现内存溢出的情况<br /> 上面的省略了一些业务代?不知道你是否明白或有更好的方?可以和我联系QQ:34174409<br /> <br /> <strong>2.BOM头问?/strong><br /> <br /> 使用java.io.ReaderdXML文gq行解析时出现异?br /> org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog.<br /> Nested exception: <br /> org.xml.sax.SAXParseException: Content is not allowed in prolog.<br /> 原因在于:UTF-8~码文g存在BOM_ReadercL法正识?br /> <br /> 解决ҎQ?br /> (1).使用16q制~辑器手动删除BOM?br /> <br /> q个...自行解决<br /> <br /> (2).InputStreamd中前面的字W,看是否有BOM,如果有BOM,q掉BOM?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 0);">PushbackInputStream pis </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> PushbackInputStream(in);<br /> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ch </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pis.read();<br /> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (ch </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0xEF</span><span style="color: rgb(0, 0, 0);">){<br />      testin.unread(ch);<br /> } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((ch </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pis.read()) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0xBB</span><span style="color: rgb(0, 0, 0);">){<br />      pis.unread(ch);<br />      pis.unread(</span><span style="color: rgb(0, 0, 0);">0xef</span><span style="color: rgb(0, 0, 0);">);<br /> } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((ch </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pis.read()) </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0xBF</span><span style="color: rgb(0, 0, 0);">){<br />      </span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> IOException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wrong format</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /> {<br /> }</span></div> <br /> (3).InputStreamd完文Ӟq掉BOM?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 0);">FileInputStream fin </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileInputStream(fileName);<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">写入临时文g</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">InputStream in </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getInputStream(fin);<br /> String tmpFileName </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fileName </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.tmp</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /> FileOutputStream out </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileOutputStream(tmpFileName);<br /> </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);"> b[] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">4096</span><span style="color: rgb(0, 0, 0);">];<br /> <br /> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /> </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (in.available() </span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />      len </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> in.read(b, </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">4096</span><span style="color: rgb(0, 0, 0);">);<br />      out.write(b, </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, len);<br /> }<br /> in.close();<br /> fin.close();<br /> out.close();<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">临时文g写完Q开始将临时文g写回本文件?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">in </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileInputStream(tmpFileName);<br /> System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> fileName </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> out </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileOutputStream(fileName);<br /> <br /> </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (in.available() </span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">){<br />      len </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> in.read(b, </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">4096</span><span style="color: rgb(0, 0, 0);">);<br />      out.write(b, </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, len);<br /> }<br /> in.close();<br /> out.close();</span></div> <br /> <strong>(3).非法XML字符?/strong><br /> <br /> 解析XML文g时出现非法字W的ExceptionQ即使该字符位于CDATAD内Q:    org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0xb) was found in the CDATA section.<br /> 原因在于:ҎW3C标准Q有一些字W不能出现在XML文g中:<br />     0x00 - 0x08<br />     0x0b - 0x0c<br />     0x0e - 0x1f<br /> 解析XML旉到这些字W就会出?br /> <br /> 解决ҎQ?br /> Ҏ可能出问题的XML文gQ进行字W过滤后再进行解析?br /> <br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> String stripNonValidXMLChars(String str) {<br />   </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (str </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">.equals(str)) {<br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> str;<br />   }<br />   </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> str.replaceAll(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">);<br /> }</span></div> <br /> <br /> ----------------------------------------<br /> <br /> by 陈于?<br /> QQ:34174409<br /> Mail: chenyz@corp.netease.com <br /> <br /> <br /> <img src ="http://m.tkk7.com/dongbule/aggbug/333262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/dongbule/" target="_blank">陈于?/a> 2010-09-28 17:10 <a href="http://m.tkk7.com/dongbule/archive/2010/09/28/333262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://m.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://01shanzhai.com" target="_blank">ٸѾƷ</a>| <a href="http://xmllhb.com" target="_blank">Ʒվ</a>| <a href="http://fphs666.com" target="_blank">պһһ</a>| <a href="http://www44414.com" target="_blank">޹һ߹ۿ</a>| <a href="http://qiyupu.com" target="_blank">պĻƵ</a>| <a href="http://tzntrip.com" target="_blank">aëƬ</a>| <a href="http://klno1.com" target="_blank">Ѿþˬˬav</a>| <a href="http://8mav1007.com" target="_blank">йѹۿ߹ۿ </a>| <a href="http://xx9911.com" target="_blank">ƷһѲ</a>| <a href="http://yisousou.com" target="_blank">鶹AVþþƷ</a>| <a href="http://xxxck.com" target="_blank">aav鶹</a>| <a href="http://0939666.com" target="_blank">þҹ޾Ʒ</a>| <a href="http://zjhydouyaji.com" target="_blank">޴߶ר</a>| <a href="http://nmgqsyg.com" target="_blank">޾Ʒ2021</a>| <a href="http://783944.com" target="_blank">ؼëƬAëƬ100Ѳ</a>| <a href="http://sznsfe.com" target="_blank">Ƶѹۿ</a>| <a href="http://tbw77.com" target="_blank">պavѲ</a>| <a href="http://mordernshasha.com" target="_blank">պӰ߹ۿ</a>| <a href="http://szjmlr.com" target="_blank">18ŮվɫƬѹۿ </a>| <a href="http://www84847.com" target="_blank">˾þô߽</a>| <a href="http://zz198.com" target="_blank">Ƶۿ</a>| <a href="http://93qsgy.com" target="_blank">޾ƷVĻ</a>| <a href="http://hwjyrck.com" target="_blank">AVպۺһ</a>| <a href="http://cqyouyongpx.com" target="_blank">˵Ӱ</a>| <a href="http://tj-zhongfa.com" target="_blank">ۺ϶</a>| <a href="http://j2eesp.com" target="_blank">һ</a>| <a href="http://koukoub.com" target="_blank">޹</a>| <a href="http://wua72.com" target="_blank">ɫAëƬѿ</a>| <a href="http://hkschooltv.com" target="_blank">˾ƷƵ</a>| <a href="http://9196528.com" target="_blank">뾫Ʒһѿ</a>| <a href="http://micehunan.com" target="_blank">黨ýmvѹۿ</a>| <a href="http://xingqiu1.com" target="_blank">AƬһ</a>| <a href="http://xjscr.com" target="_blank">һƬ</a>| <a href="http://qihongxia.com" target="_blank">avƬ߹ۿ</a>| <a href="http://gdsanhai.com" target="_blank">þþƷAVȫ</a>| <a href="http://baicaijia666.com" target="_blank">Ʒþ</a>| <a href="http://cqyouyongpx.com" target="_blank">רר</a>| <a href="http://ylptt.com" target="_blank">ѾƷ99þùۺϾƷ</a>| <a href="http://doubaye.com" target="_blank">1024ƷƵר</a>| <a href="http://345504.com" target="_blank">avƬѿ</a>| <a href="http://gstywy.com" target="_blank">ƷƬ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>