ActiveMQ: UnknownHostException on startup

在ArchLinux上使用ActiveMQ,执行bin/activemq,报错UnknownHostException,Transport Connection无法建立,可以取到/etc/ec.conf中设置的hostname(默认myhost)

解决方法,编辑/etc/hosts,添加127.0.0.1 myhost myhost。再次启动即可。Ubuntu上hosts自动把计算机名解析到127.0.0.1,ArchLinux上需要你手动做这件事了。

Inside Grails Flash Scope

Grails的在Servlet API的基础上增加了一个非常实用的FlashScope,FlashScope的生命周期为两次请求(也就是在一次重定向)。它的典型应用是POST方式提交后显示服务器端发给用户的提示信息,在平时的应用中会经常使用。

Grails的FlashScope接口定义在org.codehaus.groovy.grails.web.servlet包中,这个接口继承了Map接口,并定义了一个方法next()。

Grails的默认实现在org.codehaus.groovy.grails.web.servlet包中,GrailsFlashScope。这个实现内部定义了两个Map(生命周期为两个请求),current和next,这两个Map不断滚动,保持在一个请求中可以且仅可以访问到当前和前一次请求的上下文。
[codesyntax lang="java"]
public void next() {
current.clear();
current = (HashMap)next.clone();
next.clear();
reassociateObjectsWithErrors(current);
}
[/codesyntax]

put的时候,Grails只把新制放到next表中,因为next将在下一次请求时继续保存
[codesyntax lang="java"]
public Object put(Object key, Object value) {
// create the session if it doesn’t exist
registerWithSessionIfNecessary();
if(current.containsKey(key)) {
current.remove(key);
}
storeErrorsIfPossible(next,value);

return next.put(key,value);
}
[/codesyntax]

get的时候,Grails在两个Map中查找
[codesyntax lang="java"]
public Object get(Object key) {
if(next.containsKey(key))
return next.get(key);
return current.get(key);
}
[/codesyntax]

此外,FlashScope本身还是被放在session中
[codesyntax lang="java"]
private void registerWithSessionIfNecessary() {
GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
HttpSession session = webRequest.getCurrentRequest().getSession(true);
if(session.getAttribute(GrailsApplicationAttributes.FLASH_SCOPE) == null)
session.setAttribute(GrailsApplicationAttributes.FLASH_SCOPE, this);
}
[/codesyntax]

然后,滚动FlashScope的行为在GrailsWebRequestFilter中被调用,GrailsWebRequestFilter继承自spring-web的OncePerRequestFilter
[codesyntax lang="java"]
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

// Set the flash scope instance to its next state. We do
// this here so that the flash is available from Grails
// filters in a valid state.
FlashScope fs = webRequest.getAttributes().getFlashScope(request);
fs.next();

}
[/codesyntax]

史有先例的一跤

听说要来台风,今天特地想着把自行车推进楼道里。楼道到平地有一个坡道,就是大家都见过的那种坡道。于是我骑着自行车冲了上去,试图把动能转化成势能。不料时间停止在了坡道的三分之二处。现在不仅大脑的并行处理能力下降,小脑更是时常挂起。你说我当时怎么就没有想到使把劲蹬上去呢,我确实没有这么想,连条件反射都没有。事后觉着大概是沉醉于那一刻的静止了。两个轮子终归是不暇流连的,接下来势能没有舍得转化给摩擦,它一定程度上在我的授意之下换了个方向通通变成动能了。躺在地面上的时候我就在想上次如此贴近大地是什么时候了,大约应该在最近三十年之内。整个过程发生在下班回家热火最朝天的时候,我的视界里倒是没有发现其他眼睛,但是可以确定的是此时此刻一定有不少痴痴的目光对着我的后脑勺。希望这一切没有颠覆牛顿们这么多年来给他们建立的世界观,不过其实整个过程更象是风力作用的自然现象,只可惜台风还远,另外,我不知道风是在哪一个方向吹。

Another simple micro-blogging tool initialized

A simple micro blogging tool based on Java web framework stack (Struts2/Spring/iBatis). It costs me five days to develop such a prototype version which supports basic functions(view, post, follow and tag). And I will try to deploy it on GAE later(I hope it is possible). More improvements will also come up in next severals day.

The prototype is just simple and plain which, i think, might be a good instance for books that titled with “Teach Yourself Java Web in 7 Days”,(hough it’s not a popular topic any more):). I’m also thinking of the table of content:

  1. Introduction to 3-Tier Java Web Develop
  2. Setup Your Maven Environment
  3. Setup Your Eclipse IDE
  4. Creating Domain Objects and Tables
  5. Writing Down Your First iBatis DAO
  6. Spring for Bean Wiring
  7. Unit Test with JUnit in Spring
  8. Struts2 in Action
  9. Integrating Everything with Spring
  10. Create Ajax enabled Frontend
  11. Easy Deployment

This is my first maven managed web project. When using maven with hsqldb, there is no extra configuration needed if you switch develop environment. Really effective!