2007年12月25日星期二

异步I/O实践:Linux & Java

Linux上面的称为AIO,可以参考这篇文章:

使用异步 I/O 大大提高应用程序的性能

编译时使用“gcc a.c -lrt"。这表示链接时包含库/usr/lib/librt.XX,这个库里面可能包含了aio的库,否则会出现"Undefined reference to aio_read"的错误。

2007年12月13日星期四

My linux enviroment setting

1. close noisy bell

setterm   -bfreq   0   -blength   0

2007年11月27日星期二

Rethink the big element in web page: Data Table

image

  Data Table is heavily used in information system. In most case, these tables just display a query result set. They are read only. The operations on them are: paging, sorting, single selection and multi-selection. The all elements of a table include: field, field title, sort and page number state, and value format(or convert). In some extend, a data table is not too much associated with other page elements. So it will be a better way to define a table in one place other than several places in application.

Commet, Grizzly and Tomcat 6

  一个比较早的对这方面讨论的帖子在这里: Comet,下一代Ajax? DW上面好几篇文章做了介绍:Comet:基于 HTTP 长连接的“服务器推”技术面向 Java 开发人员的 Ajax: 使用 Jetty 和 Direct Web Remoting 编写可扩展的 Comet 应用程序。在《Ajax Design Patterns》这本书上也有介绍,不过称为HTTP Streaming。Java EE 迎合 Web 2.0 采用事件驱动的异步架构应对现代 Web 应用程序带来的挑战这篇文章从理论的高度说明了异步模式对提高系统吞吐量的作用。
如果要实现Comet,Server端就需要NIO来解决HTTP长连接的问题,否则Server端支持的并发数就会大大减少。

  Servlet和NIO的使用可以参考Servlet API 和 NIO: 最终组合在一起 使用非阻塞 I/O 构建基于 Servlet 的 Web 服务器。虽然是一篇较早的文章,仍然提供了很多关于NIO的有用信息。

  Tomcat 6里面相关NIO的配置可以参考其文档,只用修改Connector的protocol即可。接下来可以写一个简单的Servlet A,其implements CometProcessor接口。为测试方便,将Sender存放在Application变量中,在另外一个Servlet B中就可以调用Sender的send(...)方法,当浏览器访问A时,可以动态看到Server端push过来的信息(这时浏览器会一直处于页面正在装载的状态,实际应用中可以用AJAX来接受信息)。

  由于是长连接,数据在传送过程中就存在缓冲的问题。一般的一个Server端的发送到客户端的消息(注意这里不是一个响应)不会很大,而默认的Tomcat缓冲配置偏大,导致客户端响应滞后,出现这种奇怪的现象:1)当send(...)方法触发多次后,server才会一次把消息全部push到客户端(即是每次使用了flush方法);2)1 发生后,每次的send(...)的调用会导致server段立即把这条消息发送到客户端(这是buffer又好像不存在一样)。

  对于Tomcat 6自带例子里面的bug,也有位哥们在他的blog中提到:Asynchronous IO is hard!



 

2007年11月25日星期日

Use JPA with Hibernate in Netbeans(Tomcat)

0. prerequisite
  Tomcat 6.0.14
  Netbeans 6 rc1
  Hibernate core 3.2.5GA
  Hibernate EntityManager 3.3.1GA


1. Add a new JPA library implement
  The default used TopLink. Change to Hibernate.
Untitled


2. Adjust in JSF design
The default data provider ObjectArrayDataProvider works not well with JSF design UI.

image

  My target is when let a jsf table "Bind to Data", the all fields of a Hibernate JOPO can be chosen as the  above dialog. These displayed fields come from provider's getFieldKeys method which is part of interface DataProvider signature. So the simplest way is to extends ObjectArrayDataProvider and overide that method. Or just use setObjectType(Student.class). Be sure to compile that class manually so the JSF Design can take this change into account.

Why not use :

private ObjectArrayDataProvider objectArrayDataProvider1 = new ObjectArrayDataProvider(someType);

It does not work. Even I put them in _init() method of backend bean, as the CachedRowSet provider does.

3. Done.

2007年10月31日星期三

A nice article about JSF state

  developerWorks recently posted an article: Auto-save JSF forms with Ajax. The article provided an approach to implement auto-save function(some like Word's auto-save) in web tie. It told us where the data saved and how to recover from the saved data. This article used much JSF knowledge(some like component state) which is different from JSP tag-lib or other technology and in this case we can see JSF and Ajax is powerful.
And more, in some other scenario where auto-save is no need. For example, the customer want to keep some crucial form and he can resume from it later, he can do it by click a "Save" button by himself. Because auto-save isn't needed everywhere. And auto-save sometimes makes customers confused. Only the customer thinks that he has finished the form nearly, then recover from that point is constructive for him. Though AJAX is useless here, the way the author provided is also helpful and suggestive if JSF is still the choice of web framework.

  So from the viewpoint of an end user, a good designed page should tell him, whether the current form is auto-save or not. Something should notify the user, whether the save action is success or failed. And the user can find all forms he has saved somewhere. So he can resume from there conveniently. At last the system looks maybe some like a workflow system.

2007年10月14日星期日

Skip List查找算法

最先是从InfoQ上(Java集合类、Skip列表以及Google)看到的。可以将该算法形象的比喻为带索引的地址本。一般的说,该算法属于Red-Black tree(Java中TreeMap的算法)的变形。




to be continued