??xml version="1.0" encoding="utf-8" standalone="yes"?>快乐12开奖结果查询:BlogJava - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/黑灵的没啥技术含量的技术博? -> //zjumty.iteye.comzh-cnMon, 15 Oct 2018 09:23:11 GMTMon, 15 Oct 2018 09:23:11 GMT60在传输层上压缩WebService的请求和响应 - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/06/23/400884.html黑灵黑灵Sun, 23 Jun 2013 13:45:00 GMT//www.ot7t.com.cn/mstar/archive/2013/06/23/400884.html//www.ot7t.com.cn/mstar/comments/400884.html//www.ot7t.com.cn/mstar/archive/2013/06/23/400884.html#Feedback1//www.ot7t.com.cn/mstar/comments/commentRss/400884.html//www.ot7t.com.cn/mstar/services/trackbacks/400884.html阅读全文

黑灵 2013-06-23 21:45 发表评论
]]>
在MongoDB里实现循环序列功? - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/04/26/398469.html黑灵黑灵Fri, 26 Apr 2013 14:57:00 GMT//www.ot7t.com.cn/mstar/archive/2013/04/26/398469.html//www.ot7t.com.cn/mstar/comments/398469.html//www.ot7t.com.cn/mstar/archive/2013/04/26/398469.html#Feedback1//www.ot7t.com.cn/mstar/comments/commentRss/398469.html//www.ot7t.com.cn/mstar/services/trackbacks/398469.html

环境是这样的:服务器是用Java做的?数据库是MongoDB

 

需求是这样的:我们的系统里要生成一个唯一ID,前面的部分有一定的格式,并和时间关联, 精确到微秒,考虑到同一微秒内有可能存在并发情况? 所以后面在加两位序列号?系统需要定义为1毫秒内的并发小于100个,所以后面两位就够用了? Java服务器端有多台机器都可以用来生成这个唯一ID,所以需要在不同的机器上不能生成相同的序列号,所以需要在某一点上做全局的范围同步来保存这序? 号的唯一性?其实如果不考虑需求里的唯一ID是有一定意义的格式的, 用UUID或MongoDB的ObjectId都是更好的选择,完全不需要在某一点上进行同步,性能会更好?/p>

 

这个可以生成序列号的点, 我们可以做一个序列号生成服务器来对应?也可以用数据库来对应? 单单为这个简单的功能准备一个服务器来做显然不合适?但是我们用的MongoDB并没有类似于MySQL或Oracle中的SELECT FOR UPDATE这样的锁机制?所以没有办法简单的对这个序列号做原子操作? 但是MongoDB的对单个document进行update操作中有很是具有原子性的?例如

  • $set
  • $unset
  • $inc
  • $push
  • $pushAll
  • $pull
  • $pullAll

我们可以利用这些原子操作,在数据库层以乐观锁的形式来实现循环序列字段。为了方便调用我把这段逻辑做成数据库中的Javascript函数?类似与MySQL中的存储过程?/p>

 

首先我们需要一个collection来存放序列号,并对需要的需要的序列号进行初始化。我们叫它counters?/p>

Js代码  收藏代码
  1. db.counters.save({_id:"SerialNo1", val:0, maxval:99})  

 

然后我们想system.js里添加一个Javascript函数

Js代码  收藏代码
  1. db.system.js.save({_id:"getNextUniqueSeq",  
  2. value:function (keyName) {  
  3.     var seqObj = db.counters.findOne({_id:keyName});  
  4.     if (seqObj == null) {  
  5.         print("can not find record with key: " + keyName);  
  6.         return -1;  
  7.     }  
  8.       
  9.     // the max value of sequence  
  10.     var maxVal = seqObj.maxval;  
  11.     // the current value of sequence  
  12.     var curVal = seqObj.val;  
  13.       
  14.     while(true){  
  15.         // if curVal reach max, reset it  
  16.         if(curVal >= maxVal){  
  17.             db.counters.update({_id : keyName, val : curVal}, { $set : { val : 0 }}, falsefalse);  
  18.             var err = db.getLastErrorObj();  
  19.             if( err && err.code ) {  
  20.                 print( "unexpected error reset data: " + tojson( err ) );  
  21.                 return -2;  
  22.             } else if (err.n == 0){  
  23.                 // fail to reset value, may be reseted by others  
  24.                 print("fail to reset value: ");  
  25.             }   
  26.   
  27.             // get current value again.  
  28.             seqObj = db.counters.findOne({_id:keyName});  
  29.             maxVal = seqObj.maxval;  
  30.             curVal = seqObj.val;  
  31.             continue;  
  32.         }   
  33.           
  34.         // if curVal not reach the max, inc it;  
  35.         // increase   
  36.         db.counters.update({_id : keyName, val : curVal}, { $inc : { val : 1 }}, falsefalse);  
  37.         var err = db.getLastErrorObj();  
  38.         if( err && err.code ) {  
  39.             print( "unexpected error inc val: " + tojson( err ) );  
  40.                return -3;  
  41.         } else if (err.n == 0){  
  42.             // fail to reset value, may be increased by others  
  43.             print("fail to inc value: ");  
  44.               
  45.             // get current value again.  
  46.             seqObj = db.counters.findOne({_id:keyName});  
  47.             maxVal = seqObj.maxval;  
  48.             curVal = seqObj.val;  
  49.             continue;  
  50.         } else {  
  51.             var retVal = curVal + 1;  
  52.             print("success to get seq : " + retVal);  
  53.             // increase successful  
  54.             return retVal;  
  55.         }  
  56.     }  
  57. }  
  58. });  

上面这段会把指定的序列号的val?1,如果val达到上限则从0开始。所以叫循环序列?/p>

 

其实上面的实现在原理上和Java里的AtomicInteger系列的功能实现是类似的,利用循环重试和原子性的CAS来实现。这种实现方式在多线程的环境里由于锁(Monitor)的范围很小,所以并发性上比排他锁要好一些?/p>

 

下面我们用Java来测试一下这个函数的正确性?即在多线程的情况下会不会得到重复的序列号?/p>

 

第一个测试,val=0?maxval=2000?Java?0个线程每个线程循环调?00次??000次?所以正确的情况下,??999应该每个数字只出现一次?/p>

 

Java代码  收藏代码
  1. @Test  
  2. public void testGetNextUniqueSeq1() throws Exception {  
  3.   
  4.     final int THREAD_COUNT = 20;  
  5.     final int LOOP_COUNT = 100;  
  6.   
  7.     Mongo mongoClient = new Mongo("172.17.2.100"27017);  
  8.     DB db = mongoClient.getDB("im");  
  9.     db.authenticate("imadmin""imadmin".toCharArray());  
  10.     BasicDBObject q = new BasicDBObject();  
  11.     q.put("_id""UNIQUE_KEY");  
  12.   
  13.     BasicDBObject upd = new BasicDBObject();  
  14.     BasicDBObject set = new BasicDBObject();  
  15.     set.put("val"0);  
  16.     set.put("maxval", THREAD_COUNT * LOOP_COUNT);  
  17.     upd.put("$set", set);  
  18.   
  19.     db.getCollection("counters").update(q, upd);  
  20.   
  21.     Thread[] threads = new Thread[THREAD_COUNT];  
  22.     final int[][] results = new int[THREAD_COUNT][LOOP_COUNT];  
  23.     for (int i = 0; i < THREAD_COUNT; i++) {  
  24.         final int temp_i = i;  
  25.         threads[i] = new Thread("" + i) {  
  26.             @Override  
  27.             public void run() {  
  28.                 try {  
  29.                     Mongo mongoClient = new Mongo("172.17.2.100"27017);  
  30.                     DB db = mongoClient.getDB("im");  
  31.                     db.authenticate("imadmin""imadmin".toCharArray());  
  32.                     for (int j = 0; j < LOOP_COUNT; j++) {  
  33.                         Object result = db.eval("getNextUniqueSeq(\"UNIQUE_KEY\")");  
  34.                         System.out.printf("Thread %s, seq=%d\n", Thread.currentThread().getName(), ((Double) result).intValue());  
  35.                         results[temp_i][j] = ((Double) result).intValue();  
  36.                     }  
  37.                 } catch (UnknownHostException e) {  
  38.                     e.printStackTrace();  
  39.                 }  
  40.             }  
  41.         };  
  42.     }  
  43.   
  44.     for (Thread thread : threads) {  
  45.         thread.start();  
  46.     }  
  47.   
  48.     for (Thread thread : threads) {  
  49.         thread.join();  
  50.     }  
  51.   
  52.     for (int num = 1; num <= LOOP_COUNT * THREAD_COUNT; num++) {  
  53.         // every number appear 1 times only!  
  54.         int times = 0;  
  55.         for (int j = 0; j < THREAD_COUNT; j++) {  
  56.             for (int k = 0; k < LOOP_COUNT; k++) {  
  57.                 if (results[j][k] == num)  
  58.                     times++;  
  59.             }  
  60.         }  
  61.   
  62.         assertEquals(1, times);  
  63.     }  
  64. }  

 

然后我们再测试一下循环的情况?val=0, maxval=99?同样是Java?0个线程每个线程循环调?00次??000次。这次从0?9的数字每个应该取?0次?/p>

 

Java代码  收藏代码
  1. @Test  
  2. public void testGetNextUniqueSeq2() throws Exception {  
  3.   
  4.     final int THREAD_COUNT = 20;  
  5.     final int LOOP_COUNT = 100;  
  6.   
  7.     Mongo mongoClient = new Mongo("172.17.2.100"27017);  
  8.     DB db = mongoClient.getDB("im");  
  9.     db.authenticate("imadmin""imadmin".toCharArray());  
  10.     BasicDBObject q = new BasicDBObject();  
  11.     q.put("_id""UNIQUE_KEY");  
  12.   
  13.     BasicDBObject upd = new BasicDBObject();  
  14.     BasicDBObject set = new BasicDBObject();  
  15.     set.put("val"0);  
  16.     set.put("maxval", LOOP_COUNT);  
  17.     upd.put("$set", set);  
  18.   
  19.     db.getCollection("counters").update(q, upd);  
  20.   
  21.     Thread[] threads = new Thread[THREAD_COUNT];  
  22.     final int[][] results = new int[THREAD_COUNT][LOOP_COUNT];  
  23.     for (int i = 0; i < THREAD_COUNT; i++) {  
  24.         final int temp_i = i;  
  25.         threads[i] = new Thread("" + i) {  
  26.             @Override  
  27.             public void run() {  
  28.                 try {  
  29.                     Mongo mongoClient = new Mongo("172.17.2.100"27017);  
  30.                     DB db = mongoClient.getDB("im");  
  31.                     db.authenticate("imadmin""imadmin".toCharArray());  
  32.                     for (int j = 0; j < LOOP_COUNT; j++) {  
  33.                         Object result = db.eval("getNextUniqueSeq(\"UNIQUE_KEY\")");  
  34.                         System.out.printf("Thread %s, seq=%d\n", Thread.currentThread().getName(), ((Double) result).intValue());  
  35.                         results[temp_i][j] = ((Double) result).intValue();  
  36.                     }  
  37.                 } catch (UnknownHostException e) {  
  38.                     e.printStackTrace();  
  39.                 }  
  40.             }  
  41.         };  
  42.     }  
  43.   
  44.     for (Thread thread : threads) {  
  45.         thread.start();  
  46.     }  
  47.   
  48.     for (Thread thread : threads) {  
  49.         thread.join();  
  50.     }  
  51.   
  52.     for (int num = 1; num <= LOOP_COUNT; num++) {  
  53.         // every number appear 20 times only!  
  54.         int times = 0;  
  55.         for (int j = 0; j < THREAD_COUNT; j++) {  
  56.             for (int k = 0; k < LOOP_COUNT; k++) {  
  57.                 if (results[j][k] == num)  
  58.                     times++;  
  59.             }  
  60.         }  
  61.   
  62.         assertEquals(20, times);  
  63.     }  
  64. }  

 

这个测试跑了几次都是正确的?/p>

 

由于没有可以进行对比其他的实现方式(例如排他锁)所以没有做性能测试?/p>

 

写在最后?虽然MongoDB支持类似于存储过程的Stored Javascript,但是其实不建议使用这个来解决复杂问题。主要原因是没法调试,维护起来太不方便。而且?.4之前MongoDB对服务端 Javascript支持并不是很好, 一个mongod进程同时只能执行一段Javascript。如果能在应用层解决掉还是在应用层里实现逻辑比较好?/p>



黑灵 2013-04-26 22:57 发表评论
]]>
输出debug信息到postfix的log - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/04/26/398459.html黑灵黑灵Fri, 26 Apr 2013 11:25:00 GMT//www.ot7t.com.cn/mstar/archive/2013/04/26/398459.html//www.ot7t.com.cn/mstar/comments/398459.html//www.ot7t.com.cn/mstar/archive/2013/04/26/398459.html#Feedback0//www.ot7t.com.cn/mstar/comments/commentRss/398459.html//www.ot7t.com.cn/mstar/services/trackbacks/398459.html
/etc/postfix/main.cf

debug_peer_list = example.com
debug_peer_level = 2

/etc/postfix/master.cf
smtp      inet  n       -       n       -       -       smtpd -v


黑灵 2013-04-26 19:25 发表评论
]]>
Java里的CompareAndSet(CAS) - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/04/24/398351.html黑灵黑灵Wed, 24 Apr 2013 09:20:00 GMT//www.ot7t.com.cn/mstar/archive/2013/04/24/398351.html//www.ot7t.com.cn/mstar/comments/398351.html//www.ot7t.com.cn/mstar/archive/2013/04/24/398351.html#Feedback0//www.ot7t.com.cn/mstar/comments/commentRss/398351.html//www.ot7t.com.cn/mstar/services/trackbacks/398351.html Atomic 从JDK5开? java.util.concurrent包里提供了很多面向并发编程的? 使用这些类在多核CPU的机器上会有比较好的性能.
主要原因是这些类里面大多使用(失败-重试方式?乐观锁而不是synchronized方式的悲观锁.

今天有时间跟踪了一下AtomicInteger的incrementAndGet的实?
本人对并发编程也不是特别了解, 在这里就是做个笔? 方便以后再深入研?

1. incrementAndGet的实?br />
    public final int incrementAndGet() {
        
for (;;) {
            
int current = get();
            
int next = current + 1;
            
if (compareAndSet(current, next))
                
return next;
        }
    }

首先可以看到他是通过一个无限循?spin)直到increment成功为止. 
循环的内容是
1.取得当前?br />2.计算+1后的?br />3.如果当前值还有效(没有?的话设置那个+1后的?br />4.如果设置没成?当前值已经无效了即被别的线程改过?, 再从1开?

2. compareAndSet的实?br />
    public final boolean compareAndSet(int expect, int update) {
        
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }

直接调用的是UnSafe这个类的
compareAndSwapInt方法
全称?/span>
sun.misc.Unsafe. 这个类是Oracle(Sun)提供的实? 可以在别的公司的JDK里就不是这个类了

3.
compareAndSwapInt的实?br />
    /**
     * Atomically update Java variable to <tt>x</tt> if it is currently
     * holding <tt>expected</tt>.
     * 
@return <tt>true</tt> if successful
     
*/
    
public final native boolean compareAndSwapInt(Object o, long offset,
                                                  
int expected,
                                                  
int x);

可以看到, 不是用Java实现? 而是通过JNI调用操作系统的原生程?

4.
compareAndSwapInt的native实现
如果你下载了OpenJDK的源代码的话在hotspot\src\share\vm\prims\目录下可以找到unsafe.cpp
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
  UnsafeWrapper(
"Unsafe_CompareAndSwapInt");
  oop p 
= JNIHandles::resolve(obj);
  jint
* addr = (jint *) index_oop_from_field_offset_long(p, offset);
  
return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
UNSAFE_END

可以看到实际上调?/span>
Atomic类的cmpxchg方法.

5.
Atomic?/span>cmpxchg
这个类的实现是跟操作系统有关, 跟CPU架构也有? 如果是windows下x86的架?br />实现在hotspot\src\os_cpu\windows_x86\vm\目录的atomic_windows_x86.inline.hpp文件?br />
inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
  
// alternative for InterlockedCompareExchange
  int mp = os::is_MP();
  __asm {
    mov edx, dest
    mov ecx, exchange_value
    mov eax, compare_value
    LOCK_IF_MP(mp)
    cmpxchg dword ptr [edx], ecx
  }
}

在这里可以看到是用嵌入的汇编实现? 关键CPU指令?/span>
cmpxchg
到这里没法再往下找代码? 也就是说CAS的原子性实际上是CPU实现? 其实在这一点上还是有排他锁? 只是比起用synchronized, 这里的排他时间要短的? 所以在多线程情况下性能会比较好.

代码里有?/span>
alternative for InterlockedCompareExchange
这个
InterlockedCompareExchange是WINAPI里的一个函? 做的事情和上面这段汇编是一样的
//msdn.microsoft.com/en-us/library/windows/desktop/ms683560%28v=vs.85%29.aspx

6. 最后再贴一下x86的cmpxchg指定

Opcode CMPXCHG


CPU: I486+
Type of Instruction: User

Instruction: CMPXCHG dest, src

Description: Compares the accumulator with dest. If equal the "dest"
is loaded with "src", otherwise the accumulator is loaded
with "dest".

Flags Affected: AF, CF, OF, PF, SF, ZF

CPU mode: RM,PM,VM,SMM
+++++++++++++++++++++++
Clocks:
CMPXCHG reg, reg 6
CMPXCHG mem, reg 7 (10 if compartion fails)


 


黑灵 2013-04-24 17:20 发表评论
]]>
Apache Mina 中文文翻译 - 特?/title><link>//www.ot7t.com.cn/mstar/archive/2013/04/23/398309.html</link><dc:creator>黑灵</dc:creator><author>黑灵</author><pubDate>Tue, 23 Apr 2013 14:02:00 GMT</pubDate><guid>//www.ot7t.com.cn/mstar/archive/2013/04/23/398309.html</guid><wfw:comment>//www.ot7t.com.cn/mstar/comments/398309.html</wfw:comment><comments>//www.ot7t.com.cn/mstar/archive/2013/04/23/398309.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>//www.ot7t.com.cn/mstar/comments/commentRss/398309.html</wfw:commentRss><trackback:ping>//www.ot7t.com.cn/mstar/services/trackbacks/398309.html</trackback:ping><description><![CDATA[特?br />//mina.apache.org/mina-project/features.html<br /><br />MINA是一个简单的却有功能丰富的网络应用程序框架, 它提供如下特性:<br /><ul class="yibqv"><li class="yibqv">为各种传输类型提供一套统一的API</li></ul><blockquote><ul class="yibqv"><li class="yibqv">通过Java NIO实现 TCP/IP & UPD/IP通信</li></ul></blockquote><blockquote><ul class="yibqv"><li class="yibqv">通过RXTX实现串口通信(RS232?/li></ul></blockquote><blockquote><ul class="yibqv"><li class="yibqv">VM内部管道通信</li></ul></blockquote><blockquote><ul class="yibqv"><li class="yibqv">你可以实现自己的通信方式</li></ul></blockquote><ul class="yibqv"><li class="yibqv">通过Filter接口实现扩展点;类似与Servlet的Filter</li></ul><ul class="yibqv"><li class="yibqv">低级和高级的API</li></ul><blockquote><ul class="yibqv"><li class="yibqv">低级:使用ByteBuffer</li></ul></blockquote><blockquote><ul class="yibqv"><li class="yibqv">高级:用户自定义的消息对象和编码</li></ul></blockquote><ul class="yibqv"><li class="yibqv">可以自由定制的线程模?/li></ul><blockquote><ul class="yibqv"><li class="yibqv">单线?/li></ul></blockquote><blockquote><ul class="yibqv"><li class="yibqv">一个线程池</li></ul></blockquote><blockquote><ul class="yibqv"><li class="yibqv">多个线程池(例如 <a target="_blank" title="SEDA" >SEDA</a>?/li></ul></blockquote><ul class="yibqv"><li class="yibqv">利用Java5的SSLEngine实现的开箱即用的SSL,TLS, StartTLS功能</li></ul><ul class="yibqv"><li class="yibqv">过载保护 ?带宽限制</li></ul><ul class="yibqv"><li class="yibqv">通过Mock对象可以进行单体测试</li></ul><ul class="yibqv"><li class="yibqv">通过JMX管理服务?/li></ul><ul class="yibqv"><li class="yibqv">通过StreamIoHandler支持基于流的I/O</li></ul><ul class="yibqv"><li class="yibqv">可以整合进PicoContainer和Spring等常用容?/li></ul><ul class="yibqv"><li class="yibqv">很容易从Netty迁移过来?/li></ul><br /><img src ="//www.ot7t.com.cn/mstar/aggbug/398309.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="//www.ot7t.com.cn/mstar/" target="_blank">黑灵</a> 2013-04-23 22:02 <a href="//www.ot7t.com.cn/mstar/archive/2013/04/23/398309.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Apache Mina 中文文翻译 - 概述 - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/04/22/398241.html黑灵黑灵Mon, 22 Apr 2013 15:13:00 GMT//www.ot7t.com.cn/mstar/archive/2013/04/22/398241.html//www.ot7t.com.cn/mstar/comments/398241.html//www.ot7t.com.cn/mstar/archive/2013/04/22/398241.html#Feedback0//www.ot7t.com.cn/mstar/comments/commentRss/398241.html//www.ot7t.com.cn/mstar/services/trackbacks/398241.html原文链接://mina.apache.org/mina-project/index.html

Apache MINA 是一个网络应用框架, 它可以帮助你简单容易的开发高性能,高可扩展性的网络应用程序。Apache MINA底层利用Java NIO实现,在TCP/IP和UPD/IP等传输层上提供一个抽象的基于事件驱动的异步API?br />
Apache MINA一般被称为?br />
  • 一个NIO框架或库
  • 客户端,服务器框架或?/li>
  • 一个网络Socket?/li>
尽管如此,Apache MINA要提供的比上面说的多得多?你可以看一下它的功能特性列表, 利用这些特性你可以快速开发网络应用程序, 你还可以看一下人们是怎么说MINA的?请下载MINA的包,尝试一下快速开始指南, 浏览一下FAQ或者加入我们的社区?br />
Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at . //www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.



黑灵 2013-04-22 23:13 发表评论
]]>
Properties.storeToXML方法抛出空指针哦! - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/04/19/398100.html黑灵黑灵Fri, 19 Apr 2013 08:42:00 GMT//www.ot7t.com.cn/mstar/archive/2013/04/19/398100.html//www.ot7t.com.cn/mstar/comments/398100.html//www.ot7t.com.cn/mstar/archive/2013/04/19/398100.html#Feedback0//www.ot7t.com.cn/mstar/comments/commentRss/398100.html//www.ot7t.com.cn/mstar/services/trackbacks/398100.html
类似下面这段代码:
    @Test(expected = IOException.class)
    
public void testPropertiesStoreToXml() throws IOException {
        Properties props 
= new Properties();
        props.put(
"key1"true);
        ByteArrayOutputStream baos 
= new ByteArrayOutputStream();
        props.storeToXML(baos,
null);
        String xml 
= new String(baos.toByteArray());
        Assert.fail(
"should not go to here");
    }

在生成XML的时候会抛出IOException. 导致这个IOException的是做XMLTransform的时候出现了NullPointerException

感觉很奇? 调试进Properties的代码看了一?

    public String getProperty(String key) {
    Object oval 
= super.get(key);
    String sval 
= (oval instanceof String) ? (String)oval : null;
    
return ((sval == null&& (defaults != null)) ? defaults.getProperty(key) : sval;
    }

原来Properties这货, 不是String的属性一码色的返回NULL?

结果在XMLTransform的时? 直接对这个NULL进行方法调用.

后来看了一下Properties文, Properties继承至Hashtable, 所以有put和putAll之类的方? 但是不建议使?
因为这些方法不限定String类型. 推荐使用setProperty方法, 这个方法的值一定是String.

Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key.

OK,我承认是我不好好看文?就用? 但是我脚的如果你把非String的值调用一下toString再使用不是更好吗?



黑灵 2013-04-19 16:42 发表评论
]]>
关于spring-mvc的InitBinder注解的参?/title><link>//www.ot7t.com.cn/mstar/archive/2013/04/17/397945.html</link><dc:creator>黑灵</dc:creator><author>黑灵</author><pubDate>Tue, 16 Apr 2013 16:42:00 GMT</pubDate><guid>//www.ot7t.com.cn/mstar/archive/2013/04/17/397945.html</guid><wfw:comment>//www.ot7t.com.cn/mstar/comments/397945.html</wfw:comment><comments>//www.ot7t.com.cn/mstar/archive/2013/04/17/397945.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>//www.ot7t.com.cn/mstar/comments/commentRss/397945.html</wfw:commentRss><trackback:ping>//www.ot7t.com.cn/mstar/services/trackbacks/397945.html</trackback:ping><description><![CDATA[通过Spring-mvc的@InitBinder注释的方法可以对WebDataBinder做一些初始化操作?br />比如设置Validator?<br />我一直在想能不能为每个Request或者每个Action方法单独设置Validator?br />也就是说Controller里有多个被@InitBinder标注的方法?在不同的Action时被分别调用?br /><br />我注意到了@InitBinder的value参数?br /><br />api docs里是这样描述的:<br />The names of command/form attributes and/or request parameters that this init-binder method is supposed to apply to. <p>Default is to apply to all command/form attributes and all request parameters processed by the annotated handler class. Specifying model attribute names or request parameter names here restricts the init-binder method to those specific attributes/parameters, with different init-binder methods typically applying to different groups of attributes or parameters. <br /></p><p>是乎是可以针对不同的Form对象或命令调用不同的InitBinder方法?/p><p>于是我写了下面的Controller试了一?/p><p></p><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>//www.CodeHighlighter.com/<br><br>--><span style="color: #000000; ">@Controller<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> HomeController {<br />    <br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">static</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> Logger logger </span><span style="color: #000000; ">=</span><span style="color: #000000; "> LoggerFactory.getLogger(HomeController.</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">);<br />    <br />    @InitBinder(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">action1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> initBinder1(WebDataBinder binder){<br />        logger.info(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">initBinder1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    }<br />    <br />    @InitBinder(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">action2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> initBinder2(WebDataBinder binder){<br />        logger.info(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">initBinder2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    }<br />    <br />    </span><span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * Simply selects the home view to render by returning its name.<br />     </span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br />    @RequestMapping(value </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; ">"</span><span style="color: #000000; ">, method </span><span style="color: #000000; ">=</span><span style="color: #000000; "> RequestMethod.GET)<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> String home(Model model) {        <br />        <br />        Date date </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Date();<br />        DateFormat dateFormat </span><span style="color: #000000; ">=</span><span style="color: #000000; "> DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);<br />        <br />        String formattedDate </span><span style="color: #000000; ">=</span><span style="color: #000000; "> dateFormat.format(date);<br />        <br />        model.addAttribute(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">serverTime</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, formattedDate );<br />        <br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">home</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />    }<br />    <br />    </span><span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * Simply selects the home view to render by returning its name.<br />     </span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br />    @RequestMapping(value </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">/doit</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, method </span><span style="color: #000000; ">=</span><span style="color: #000000; "> RequestMethod.POST, params</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">action1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> String doit1(@RequestParam(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">value1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> value1,<br />            @RequestParam(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">action1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">) String action, Model model) {        <br />        logger.info(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">value1={}</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,value1);<br />        <br />        Date date </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Date();<br />        DateFormat dateFormat </span><span style="color: #000000; ">=</span><span style="color: #000000; "> DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);<br />        <br />        String formattedDate </span><span style="color: #000000; ">=</span><span style="color: #000000; "> dateFormat.format(date);<br />        <br />        model.addAttribute(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">serverTime</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, formattedDate );<br />        <br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">home</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />    }<br />    <br />    </span><span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * Simply selects the home view to render by returning its name.<br />     </span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br />    @RequestMapping(value </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">/doit</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, method </span><span style="color: #000000; ">=</span><span style="color: #000000; "> RequestMethod.POST, params</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">action2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> String doit2(@RequestParam(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">value1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> value1,<br />            @RequestParam(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">action2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">) String action, Model model) {        <br />        logger.info(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">value1={}</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,value1);<br />        <br />        Date date </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Date();<br />        DateFormat dateFormat </span><span style="color: #000000; ">=</span><span style="color: #000000; "> DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);<br />        <br />        String formattedDate </span><span style="color: #000000; ">=</span><span style="color: #000000; "> dateFormat.format(date);<br />        <br />        model.addAttribute(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">serverTime</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, formattedDate );<br />        <br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">"</span><span style="color: #000000; ">home</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />    }<br />}<br /></span></div><p>画面上的Form是这样的</p><p><br /></p><p></p><div style="background-color:#eeeeee;font-size:13px;BORDER:1px solid #CCCCCC;PADDING-RIGHT: 5px;PADDING-BOTTOM: 4px;PADDING-left: 4px;PADDING-TOP: 4px;WIDTH: 98%;word-break:break-all"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>//www.CodeHighlighter.com/<br><br>--><span style="color: #0000FF; "><</span><span style="color: #800000; ">form </span><span style="color: #FF0000; ">action</span><span style="color: #0000FF; ">="doit"</span><span style="color: #FF0000; "> method</span><span style="color: #0000FF; ">="post"</span><span style="color: #FF0000; "> enctype</span><span style="color: #0000FF; ">="application/x-www-form-urlencoded"</span><span style="color: #0000FF; ">></span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">input </span><span style="color: #FF0000; ">type</span><span style="color: #0000FF; ">="text"</span><span style="color: #FF0000; "> name</span><span style="color: #0000FF; ">="value1"</span><span style="color: #FF0000; "> value</span><span style="color: #0000FF; ">="100"</span><span style="color: #0000FF; ">/></span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">input </span><span style="color: #FF0000; ">type</span><span style="color: #0000FF; ">="submit"</span><span style="color: #FF0000; "> name</span><span style="color: #0000FF; ">="action1"</span><span style="color: #FF0000; "> value</span><span style="color: #0000FF; ">="Action1"</span><span style="color: #0000FF; ">/></span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; "><</span><span style="color: #800000; ">input </span><span style="color: #FF0000; ">type</span><span style="color: #0000FF; ">="submit"</span><span style="color: #FF0000; "> name</span><span style="color: #0000FF; ">="action2"</span><span style="color: #FF0000; "> value</span><span style="color: #0000FF; ">="Action2"</span><span style="color: #0000FF; ">/></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; "></</span><span style="color: #800000; ">form</span><span style="color: #0000FF; ">></span></div><br /><p>我的意愿是如果画面上,点击action1按钮择调用initBinder1方法?如果点击action2按钮则调用initBinder2方法?/p><p>实际上也确实是这样的?<br /></p><p>当点击action1时,<span style="color: #000000; ">logger.info(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">initBinder1</span><span style="color: #000000; ">"</span><span style="color: #000000; ">); 会执?/span></p><p>当点击action2是,<span style="color: #000000; ">logger.info(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">initBinder2</span><span style="color: #000000; ">"</span><span style="color: #000000; ">); 会执?/span></p><p>是乎是可以实现“每个Action方法单独设置Validator”这个目的?/p><p>但是其实并不是这样的?/p><p>我调式进去了InitBinder相关的源代码InitBinderDataBinderFactory?结果发现它只是在对action这个参数绑定的时候调用了上面这个方法?对value1绑定的时候那个方法都没有调用。而我们常常要解决的是对同一个参数对于不同的action应用不同的Validator?<br /></p><p>所以目前还是没有好的方法能直接解决这个问题?/p><p>也许我们可以自己实现一个DataBinderFactory来解决这个问题?/p><p><br /></p><img src ="//www.ot7t.com.cn/mstar/aggbug/397945.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="//www.ot7t.com.cn/mstar/" target="_blank">黑灵</a> 2013-04-17 00:42 <a href="//www.ot7t.com.cn/mstar/archive/2013/04/17/397945.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>KMP算法里的next函数是怎么得到的? - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2013/04/14/397824.html黑灵黑灵Sun, 14 Apr 2013 15:24:00 GMT//www.ot7t.com.cn/mstar/archive/2013/04/14/397824.html//www.ot7t.com.cn/mstar/comments/397824.html//www.ot7t.com.cn/mstar/archive/2013/04/14/397824.html#Feedback0//www.ot7t.com.cn/mstar/comments/commentRss/397824.html//www.ot7t.com.cn/mstar/services/trackbacks/397824.html也就是这?p?p?p⑶?.p(k-1)?= ' p(j-k+1)p(j-k+2)……p(j-1)’?为什么找到这个k以后用k的元素比较字符串中i就行了?br />
现在找到一个最接近明白的就是百度百科上?br />
假设
主串:s: ‘s?s?s?……s(n)?;
模式?:p: ‘p?p?p⑶?.p(m)?br />把课本上的这一段看完后,继?br />现在我们假设 主串第i个字符与模式串的第j(j<=m)个字符‘失配’后,主串第i个字符与模式串的第k(k<j)个字符继续比较
此时,s(i)≠p(j),?br />主串:s⑴…?s(i-j+1)…?s(i-1?s(i) ………?
|| (相配) || ≠(失配?br />匹配串:p?...........p(j-1?p(j)
由此,我们得到关系式?br />‘p?p?p⑶?.p(j-1)?= ?s(i-j+1)……s(i-1)?br />由于s(i)≠p(j),接下来s(i)将与p(k)继续比较,则模式串中的前(k-1)个字符的子串必须满足下列关系式,并且不可能存在 k?gt;k 满足下列关系式:(k<j),
‘p?p?p⑶?.p(k-1)?= ?s(i-k+1)s(i-k+2)……s(i-1)?br />即:
主串:s⑴……s(i-k +1?s(i-k +2?……s(i-1?s(i) ………?
|| (相配) || ||(有待比较)
匹配串:p?p?…?.... p(k-1?p(k)
现在我们把前面总结的关系综合一?br />有:
s⑴…s(i-j +1)?s(i-k +1?s(i-k +2?…?s(i-1?s(i) …?br />|| (相配) || || || ≠(失配?br />p?……p(j-k+1?p(j-k+2??..... p(j-1?p(j)
|| (相配) || ||(有待比较)
p?p?…?..... p(k-1?p(k)
由上,我们得到关系:
'p?p?p⑶?.p(k-1)?= ' p(j-k+1)p(j-k+2)……p(j-1)?br />
不知道是从哪个课本上抄来的?其实还是不太明白最后一步?br />

黑灵 2013-04-14 23:24 发表评论
]]>
简单的监视某个端口的连接数的Linux命令 - 四川福利彩票快乐12快乐12开奖直播快乐12开奖辽宁福彩快乐12快乐彩12选5走势图//www.ot7t.com.cn/mstar/archive/2012/10/30/linux_shell_watch_connection.html黑灵黑灵Tue, 30 Oct 2012 01:38:00 GMT//www.ot7t.com.cn/mstar/archive/2012/10/30/linux_shell_watch_connection.html//www.ot7t.com.cn/mstar/comments/390432.html//www.ot7t.com.cn/mstar/archive/2012/10/30/linux_shell_watch_connection.html#Feedback0//www.ot7t.com.cn/mstar/comments/commentRss/390432.html//www.ot7t.com.cn/mstar/services/trackbacks/390432.html
#!/bin/sh
netstat 
-anp | grep :$1 | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c

然后执行命令:

watch -n 1 count_conn 8080

最后的参数就是你要监视的端?



四川福利彩票快乐12 2012-10-30 09:38 发表评论
]]>