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

2007年9月12日星期三

The unique of Tapestry

  What's the difference between web development and traditional C/S application such Swing? Personally I like Swing because it let me more efficient. All the code is Java code and there is little type-cast. And Tapestry also follows this way. The author admitted that:

An advantage for Tapestry, I think, is the way the two pages (the firsts containing the form, the second displaying the result) communicate ... in proper, type-safe Java code.
  This means if you want to invoke a web page, what you face is a java class. Blow is a example:
public IPage doSubmit()
{
ShowProject showProject = getShowProject();
showProject.setProject(getProject());
return showProject;
}

  Get a page class(by injection), give it some value and then you can forward to that page. The page class has included a page and we can say it is the interface of the page. But the best way, I think, is the function like way. Some likes "showProject(project);". Compared with Object initialization which is not undocumented and not self-explaining, function maybe is more intuitive.

  Tapestry just resolved the problem of the two pages communicate. As to multi-page, or a conversation, there are a lot of work. For example, how to get a page's return value?

  Unlike this way that page and bean are bound together, JSF let page outside the backend bean. And a jsf page can bind multi-bean. A backend bean can be used in many jsf pages. This loose bind let a page include another page easily.

2007年8月1日星期三

JPDA(Java Platform Debugger Architecture)

How let your changed java code updated to server? It is a big problem in develpping any non-trivial JEE project.

Resently, I run a JBoss 4.04 outside of Eclipse. The application archive(EAR) is so big that we can not run it in IDE environment. Then the debug process is boring. After having modify the java code, I have to package it, copy to deploy directory and restart the server. Ant can help me a lot but a debug round still takes me much of time.

In this situation, JPDA will useful. With the help of eclipse, I can use it to connect to a "Remote Java Application". You can find it in "Debug" dialog. Then you can inspect value of a varible and even modify the java code. Before that, you have to add a breakpoint to your java code. Only java with breakpoint can be modified and replaced to the server. This is a nice feature when you need modify code many times. When eclipse has attached remote server, any modified java code could be updated to the server. This is so-called "Hotswap Bug Fixing". Of course the source code should be added to the debug instance's "Source lookup path". Otherwise the server didn't suspend at your breakpoint. And you should mind that the compiled classes were swapped just in the server's memory and didn't updated to the server's file system. So after finished debug, you should copy the classes to the server's deploy directory.

Some other approaches I often used include Hot deploy and restart application (not server).

Hot deploy is used to deploy a archive such as .war or .jar(EJB) automatically when you put them in deploy directory. Most servers has this feature.

Restart application or Reload Context is used to reload all resource(classes and configuration) in one application eg EJB, or War. This approach is heavier than others. Tomcat will throw exception "Out of memory" after reloaded a web application many times.

To Netbeans, It provides debug mode also. There are some differences on "hot class replace" between it and eclipse.

Above is netbeans 6M10 debug tool bar. It has a "Apply Code Changes" button to submit the changed code to server. It need you do manually. This way is different from eclipse which done automatically.

2007年6月19日星期二

Cygwin tips

1.if the os is windows, the file type will be dos by default. So if you new a shell script, then run it in cygwin, the error like "who/r command not found" will happen. The solution is changing the file type to unix. A approch is using vi. In vi, use ":set ff?" to inquiry file type. Then use ":set fileformat=unix" or ":set ff=unix".

2.In my machine, cygwin.bat is:bash --login -i. This will let shell be a login shell. But only an interactive shell that is not a login shell will read and execute ~/.bashrc. If we want our personal bash, we should create a .profile file in home directory instead of of .bashrc. One example is:

alias h="history"
alias hg="h|grep"

#alias ls='ls --color=auto'
alias la='ls -A'
alias lf='ls -F'
alias ll='ls -l'

 3.use wget with proxy. create a .wgetrc in home directory.

http_proxy = proxy.com:81
no_proxy = localhost

This will make wget to fetch data with proxy by default and you didn't need to swith to proxy model by " -Y on".

2007年6月6日星期三

RESTful Web development

  What RESTful means? I found this document : RESTful Rails development is very clear to express this point. And It is also quickly to test it in Rails. Soon you will find the amazing of RESTful. It just used HTTP GET, POST, DELETE, PUT to manage the resource and some likes JDBC CRUD implement. And more it is a new RPC implement. It has been used widely in web service. So a web service can be invoked by RESTful URL. And I think it make web service more powerful. In some traditional web application, for example a struts or JSF application, RESTful just provides nice URL format.

  But obviously RESTful will be more important when application need to invoke 3rd party functions

2007年6月2日星期六

Visual library of NetBeans

Today I evaluated Visual library of NetBeans org. There are not too much information about it on Sun's site. And it is a part of NetBeans 6 M9. I guest it was just separated from Netbeans as a standalone project. It's many feature is same as JGraph which has been used by many users. But I think this library provides more about UI design but not only concerning on graph as done by JGrah. For example, it has a feature "ComponentWidget - AWT/Swing components on scene". So you can do this:

JButton button = new JButton();
button.setText("xixi");
ComponentWidget widget = new ComponentWidget(this,button);

Then I have a question: Is Visual Web (netbeans) developed on this library? If so, Visual Library is very cool! (From this post, just page flow navigator is based on Visual Library.)
There is a video which created a demo on Visual library. You can find that a JasperReports tool called jarvis is also base on this library. It looks powerful.

2007年5月7日星期一

Question on JSF state saving

JSF state is restored or used in "Restore View" of JSF life cycle. These state can be stored in 2 places:client and server. There are some links about the difference of them:

http://www.jroller.com/page/mert?entry=state_saving_method_client_side
http://www.jroller.com/page/cagataycivici?entry=jsf_state_saving_best_of
http://www.jroller.com/page/cenkcivici?entry=changing_default_state_management_method

These states are used to store data about the JSF component tree(the elements in f:view), including components and component id. They can be seen between 2 request. That is the difference from backend bean. At first thought most of components have no state, or just have a value which presents its state. A text field has state? But YES. Disable/Enable is a common state. In the component implement java code, saveState and restoreState methods deal all these work: choose some attributes and save them to an object which can be found as a hidden field named javax.faces.ViewState in client's browser source code. It was Base64 encoding. And of course, the view state has been configurated to be stored in the client. In Sun's webui implement, many attributes are stored in view state, such as javascript event method and style.

Some component such as Table can be very complex and has its own states such as the current page number and ordered column. If these states doesn't be stored in component tree, we have to save them in hidden input field.

Myfaces even has a component t:saveState just for storing data. See here.

This technology is powerful when user's action is within one page. When page is refreshed, all he has done are saved. If he just jumps between page, navigate from one page to another page, then the state of a page is discarded, and more it is useless. In one page scenario, AJAX is also a good choice. They all improve user's experience.

Somebody provides stateless approach.

There is a cool tool called FacesTrace to monitor JSF application. It displays not only session data, but also the whole JSF life cycle dynamically.

2007年5月4日星期五

test code and use Microsoft Writer

test code
if (component instanceof ValueHolder && (null != (converter = ((ValueHolder)


the css please refers here. make sure that css code block should be placed in <head> and started with .post.

  In your blog, you should enbrace your code with <code> tag in HTML view.  And in your code block, you cann't use <> these simble. You change it into HTML code by some tools like phpfi. It also provides highlighting feature.

  But this approach is some boring. Later I found M$ Live Writer. It is cool. You can edit blog on offline mode and post it when online. You also can download blog item, modify it, and update it to Blogger. You also can delete blog by Writer although Writer didn't remind you. The editor is css-enable. And more interesting it is extensible.

  It's disadvantage I found until now is that it cann't upload image file(Blogger does not support it). It can just add a image link.

  (6.4 add)Today Writer beta2 is released. It has many nice feature. The biggest imprement is that my article can use tag. Well Done! It looks really cool! I think Microsoft will let it more powerful.

  The reason of Writer's existen is:1)It provide offline edit mode; 2)It is WYEIWYS editor; 3) web editor is not so much convenient until now. You will find these problem specially when the article is some long.

2007年5月3日星期四

use ajax4jsf

Ajax4JSF is a framework which inserts AJAX function to JSF. It is easy to transform orginal JSF code to Ajax-able. Ajax4JSF just changed the way of http message.
It's document is here. It has a simple example. Look at blow code:


the reRender property points out whose data in JSF component tree should be return from server. After JSF engine renders that component, Ajax4JSF then sends response. So this approach didn't do harsh to JSF. We can code in JSF bean:

private HtmlOutputText rep2 = new HtmlOutputText();
......
String style="border-color: rgb(204, 0, 255); backgroun.....";
this.rep2.setStyle(style);

We can see the benefit of JSF component model. But this model don't consider the position of the javascript or it is hard to add javascript function. For example, if we want to hightlight the outputText, Should I write a new class extends HtmlOutputText which has a cool appearance? Maybe a Decrtor Pattern is better. No matter which approach we use, we should encapsulate the javascript into JSF component and give user a consistent usage. On the other hand, If we have a JSF component which is not ajax-able but provides hightlight function, we can use it directly in Ajax4JSF. So the problem is the matter of JSF instead of Ajax4JSF.

2007年4月4日星期三

why Ruby over Java

这个帖子很不错,可以参照来学习Ruby。总结来说Ruby的优点在于:
* closures
* multiple implementation inheritance via mixins (simplifies a lot designs where we would use decorators and factories in java)
* messages to objects as if they were first class methods (natural way of doing AOP, used a lot in ActiveRecord). This actually also counts for the famous java properties support planned for java 7...
缺点在于没有了Java的类型检查,而这是提高Java开发效率的关键:通过编译就可以排除大部分的错误,也提高了系统的安全性。但是在开发一个完整的 Java应用时,经常会遇到DSL语言的地方,比如jsp, sql,这些都是没有办法进行类型检查的地方(DSL的特点吗?)。再比如对Session的处理不可避免的会用到类型cast。
在RJS里面,html, rjs, rb这些文件之间存在的变量type和name的合约(或契约)更多,而只能手工检查。
疑问:Ruby里面method的参数的类型会在运行时进行类型检查?
那JRuby会起到一个粘合剂的作用吗?这会提高系统的复杂性,而且一个系统中使用两种语言也少见,除非是一个遗留系统。

2007年3月31日星期六

Sun的一些新动向

最近忙于找工作,荒疏了一些东西。
1.pet store 2.0发布了。看上去好像是web 2.0的跟风作品,AJAX,RSS,TAG,SEARCH都用上了,还没来仔细研究。用了不少开源的库,可以借鉴下。
2.一则netbeans有了新的插件:Visual Web Pack,看上去很不错的东西,应该是从creator里面提取出来的,这样比较eclipse的web开发,netbeans强了很多。记得原来使用 creator时,由于deploy服务器只能使用sun application server,速度很慢,还设法过把部署环境改为tomcat,这下方便多了。
产品的定位跟Microsoft visual web developer express类似。
相比下,eclipse的可视化编辑界面就差了很多,VE发展到现在还是个小玩具。个人感觉Swing比较swt可能难看点,但是灵活性更好些。
3.新的application server增加了很多SOA的支持,比如REST,BPEL。有个netbeans演示BPEL开发过程的Video,图形拖放的方式,很是cool。

2007年1月13日星期六

Questions on Jboss Seam

Obviously, seam has many cool features. There are some questions:
1.Seam introduced many kinds of context, and much more than Spring. Are they better than Spring?
2.any bean marked with @Name() will be treated as managed component. client just uses "DumyBean cc = (DumyBean) Component.getInstance("dumyBean", true);" to access the bean. Maybe this approach is clearer than Spring xml configuration. Is this approach less efficiently?
3.debug.seam page is right around thing! It should exist in Spring. Is it a servlet? Where is its configuration?
4.@Logger looks cool. This annotation is introduced by jboss. How to do this?In org.jboss.seam.Component, there is: if ( field.isAnnotationPresent(org.jboss.seam.annotations.Logger.class) )So, It looks that Logger is a build-in service. There is no way to configure it or add my service, yeah?
5.eclipse's command works very well with Ant? For example: when I click save button, eclipse automatically invokes Ant's task. How to do this?6.annotation is largely used in Seam. What about this?