Feb 23

Observer pattern implementation in web application using Comet

     Observer pattern can be implemented easily in C/S based applications, but in web application area it is not easy to implement caused by the shortcoming of HTTP protocol. Currently we have some solutions : 

  1. browser plugins: we can use socket communication in flash or applet etc. by this we can implement C/S likely applications, the server side can push messages to the small application in browser side,  when the small application receives messages, it can communication with javascript engine. 
  2. comet : it also can be called "server push". it was based on long time polling, we start a request from browser side, the server side application keep the request live, by this we can keep sending messages to browser side. nowadays, some web application servers start to support comet, like : tomcat6/tomcat7 , jetty etc.. In tomcat, NIO is used to optimized the IO performance and decrease the expense of CPU. 
  3. HTML5 web socket. 

    Thinking of implementing the Observer pattern:

  1. send a request with the concerning message types, when server side received these messages, save these concerning message types in the session scope. Of course we can persistence these concerning message type in database, then we can design a customer independent notification user experience.  
  2. open a long time polling from the browser side, when server side received this request, we save the request related  response in a message send thread, at the same time we must keep this request live. Then we can send message signal to the message send thread. In the message send thread we can determine which message must write to which response.
May 22

误用jakata commons-beanutils引起bug分析

    项目里面大量的使用到了jakata的commin包,包括beanutils这个包,主要是用来copy bean property,以前一直用的很好,也没有去深究它内部的实现机制,评自己猜想就只是运用反射去取得属性的值,然后设置值吧。接着就出现了现在遇到的问题,项目中一个vo属性的类型为Byte包装类型,这种属性在实例化的时候默认为null,这个特性被我用到了业务中,也就是我会通过判断这个属性是否为null来实现某些逻辑,结果发现这个属性竟然会在没有任何人动的情况下自己被赋值为0,然后就导致依赖这个属性的业务逻辑错掉,debug一步一步的跟踪发现只要一经过beanutils,copy出来的bean的这个属性自动变为0了,以为是beanutils的bug,找来源码看了看,发现BeanUtils中其实是调用了BeanUtilsBean的copyProperties方法,BeanUtilsBean中实现了一个简单的工厂模式,将的一个实例绑定在当前线程中,有点儿类似ThreadLocal,也就是当前线程中所有调用都会采用同一机制进行copy,这里说到copy的机制其实就是指Converter,对于各种类型都有一个特定的Converter,包内部提供了一些jdk内部类型的Converter,每种类型Converter都有可以提供一种默认值,而在BeanUtils中对于Byte,Integer等数值类型提供的默认值是:0 ,所以就会出现把Byte类型值为null转换成了0。其实这么做也有他的好处,可以避免出现烦人的NullPointerExceprtion。知道原因,解决办法就很容易想到了,自己重新初始化一下BeanUtilBean中的ByteConverter。

Dec 15

Mutex vs. Semaphore

Mutex:

Is a key to a toilet. One person can have the key – occupy the toilet – at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: "Mutexes are typically used to serialise access to a section of  re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."
Ref: Symbian Developer Library

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count – the count of keys – is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)."
 

Dec 10

NSIS制作B/S架构安装程序

    开门见山,B/S架构应用程序的安装程序无非就是拷贝一些文件,检查一下环境配置,注册服务,启动服务之类的了。拷贝文件就没什么好说的了。下面着记录一下如下几点:

  1. 端口检查
  2. 读写注册表
  3. 调用外部可执行文件
  4. 注册、启动、停止服务
  5. 注册环境变量

Continue reading

Jul 19

GB2312简体中文编码表

GB2312简体中文编码表

code +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
A1A0   、 。 · ˉ ˇ ¨ 〃 々 — ~ ‖ … ‘ ’
A1B0 “ ” 〔 〕 〈 〉 《 》 「 」 『 』 〖 〗 【 】
A1C0 ± × ÷ ∶ ∧ ∨ ∑ ∏ ∪ ∩ ∈ ∷ √ ⊥ ∥ ∠
A1D0 ⌒ ⊙ ∫ ∮ ≡ ≌ ≈ ∽ ∝ ≠ ≮ ≯ ≤ ≥ ∞ ∵
A1E0 ∴ ♂ ♀ ° ′ ″ ℃ $ ¤ ¢ £ ‰ § № ☆ ★
A1F0 ○ ● ◎ ◇ ◆ □ ■ △ ▲ ※ → ← ↑ ↓ 〓 Continue reading

Jun 27

Resin启动参数详解

1.命令行参数(Command-line arguments)

-install (Windows) install Resin as a service (but doesn’t automatically start.)

-install-as xxx (Windows) install Resin as a named service (but doesn’t utomatically start.)

-remove (Windows) install Resin as a service (but doesn’t automatically start.)

-remove-as xxx (Windows) remove Resin as a named service (but doesn’t automatically start.) Continue reading

Apr 06

java加密解密学习心得

    在java中可以利用keytool工具生成非对称密钥对(PrivateKey和PublicKey),PublicKey和PrivateKey也 可以用KeyPairGeneerator来生成.keytool工具生成的密钥对保存在keystore文件中,这个keystore中可以保存多个密 钥对,每个密钥对都有一个唯一的名称(alias)与之对应,可以选择从这个keystore中导出某一个名称对应的密钥对的公钥,导出后的公钥叫做数字 证书,就是说数字证书中保存的就是一个公钥.而从keystore中导出的数字证书与一般自己生成的公钥是有区别的,数字证书能够将一个公钥与某个具体的 实体相对应(人或者企业机构),这个数字证书是否受到信任取决于你是否信任该证书的证书链上的任意一个颁发机构.如果证书受到信任即可以确认该证书中存储 的公钥确实是属于某个实体.在java中利用数字证书对信息进行签名,实质上做的事情是,先取得信息的摘要,然后对摘要进行数字签名(即用自己的私钥对摘 要进行数字签名).因此使用数字证书可以实现两个功能:防止信息被篡改(由摘要实现),防止抵赖(消息摘要是用自己的私钥进行的签名).举例:A要跟B通 信,A有B的证书(B的公钥),B有A的证书(A的公钥),A要给B发送信息的话,A就用自己的私钥对信息进行签名,接着将签名与要发送的信息一起发送给 B,B收到签名与信息后,用A的证书(A的公钥)验证签名,如果验证通过则B可以确信两件事情,第一信息在传输过程中没有受到篡改,第二,改信息确实由A 发送而来。 Continue reading