用spring和hessian構建分佈式應用(遠程接口)的方法

最近一期的《programmer》裏幾乎從頭至尾在講關於“J2EE without EJB”的事情,可憐的ejb啊,居然被描述成了遺產系統的重要組成部分。。。

其實有上面的結論,無外乎現在java裏面的新技術已經幾乎能完全取代ejb的優點,而克服ejb的缺點,entity bean和有狀態的session bean已經機乎被視爲垃圾,hibernate和spring大行其到,看看最進n期《programmer》中篇幅的比重就知道了。本來我個人的感覺是hibernate取代了entity bean,spring取代了session bean,但是ejb的遠程調用用hibernate和spring的架構還取代不了,可是在最近的一期《programmer》中我發現了Hessian!更爽的是,發現了spring原來可以和Hessian結合使用!看來真的可以say byebye to ejb了。

看到這麼振奮人心的消息,怎麼能不親自試驗一下呢,於是上http://www.caucho.com/以迅雷不及掩耳盜鈴之勢下載了Hessian的src jar和bin jar。create一個工程,把這些相關的jar統統扔進去,配置和coding就可以開始了。首先,開了如下幾個包:



whao.test.hessian.server
放遠程服務的接口

whao.test.hessian.server.impl
放遠程服務的實現類

whao.test.hessian.client
放客戶端應用



1. whao.test.hessian.server中寫一個MyService接口:
[code:1]
/*

* Created on 2005-7-25

*

*/

package whao.test.hessian.server;



/**

* @author Hao Wei

*

*/

public interface MyService {

public String doSomething(String s);

}
[/code:1]


2. whao.test.hessian.server.impl中寫一個實現類

[code:1]
/*

* Created on 2005-7-25

*

*/

package whao.test.hessian.server.impl;



import whao.test.hessian.server.MyService;



/**

* @author Hao Wei

*

*/

public class MyServiceImpl implements MyService {



/* (non-Javadoc)

* @see whao.test.hessian.server.MyService#doSomething(java.lang.String)

*/

public String doSomething(String s) {

return "HAHAHA: " + s;

}

}

[/code:1]

3. 配置遠程服務

Hessian的遠程服務要配置成servlet,配置如下:
[code:1]

<servlet>

<servlet-name>myservice</servlet-name>

<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

<load-on-startup>1</load-on-startup>

<init-param>

<param-name>service-class</param-name>

<param-value>whao.test.hessian.server.impl.MyServiceImpl</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>myservice</servlet-name>

<url-pattern>/myservice</url-pattern>

</servlet-mapping>

[/code:1]

這樣,當啓動了這個web應用,這個遠程服務就以url http://localhost:8080/test_web/myservice 的形式發佈了。然後就是開發客戶端來調用這個遠程服務。

[code:1]
/*

* Created on 2005-7-25

*

*/

package whao.test.hessian.client;



import whao.test.hessian.server.MyService;



import com.caucho.hessian.client.HessianProxyFactory;



/**

* @author Hao Wei

*

*/

public class TestMain {

public static void main(String[] args) throws Exception {

HessianProxyFactory proxyFactory = new HessianProxyFactory();

MyService service = (MyService) proxyFactory.create(MyService.class,

"http://localhost:8080/test_web/myservice");

System.out.println(service.doSomething("xixixixi"));

System.out.println("ok!");

}

}

[/code:1]

運行一把,顯示

HAHAHA:xixixi

ok!

不錯不錯,純Hessian的遠程調用就這樣搞定了。繼續研究《programmer》看到上面介紹用spring的遠程訪問解決方案來訪問ejb的遠程服務。什麼?spring還有遠程解決方案?沒聽說過嘛,看了《programmer》上的介紹,發現是真的。那麼既然spring能訪問ejb的遠程服務,那麼能訪問Hessian的遠程服務麼?打開spring.jar看看,居然發現了名曰org.springframework.remoting.caucho.HessianProxyFactoryBean的類!夏昕真不厚道啊,再他的spring中文教程中居然匿掉了spring裏這麼好的東西!於是打開sping英文版reference,終於找到了用spring配置Hessian客戶端的方法:
[code:1]
<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

<property name="serviceUrl">

<value>

http://localhost:8080/test_web/myservice

</value>

</property>

<property name="serviceInterface">

<value>whao.test.hessian.server.MyService</value>

</property>

</bean>

[/code:1]
然後客戶端就可以這麼玩啦:

[code:1]
/*

* Created on 2005-7-25

*

*/

package whao.test.hessian.client;



import whao.test.hessian.server.MyService;

import whao.util.spirng.SpringBeanFactory;



import com.caucho.hessian.client.HessianProxyFactory;



/**

* @author Hao Wei

*

*/

public class TestMain {



public static void main(String[] args) throws Exception {

testWithSpring();

}

public static void testWithSpring(){

MyService service = (MyService)SpringBeanFactory.getBean("myService");

System.out.println(service.doSomething("lllllllll"));

System.out.println("ok!");

}

public static void testWithoutSpring() throws Exception {

HessianProxyFactory proxyFactory = new HessianProxyFactory();

MyService service = (MyService) proxyFactory.create(MyService.class,

"http://localhost:8080/test_web/myservice");

System.out.println(service.doSomething("xixixixi"));

System.out.println("ok!");

}

}

[/code:1]

執行一下,輸出是:

HAHAHA:lllllllll

ok!

spring真是個好東東,呵呵,其中的SpringBeanFactory是這樣實現的:
[code:1]

/*

* Created on 2005-7-25

*

*/

package whao.util.spirng;



import javax.servlet.ServletContext;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;



/**

* @author Hao Wei

*

*/

public class SpringBeanFactory {

private static final Log log = LogFactory.getLog(SpringBeanFactory.class);

private static ApplicationContext ctx = null;



public static Object getBean(ServletContext context, String beanID) {

log.info("beanID=" + beanID);

ApplicationContext ac = WebApplicationContextUtils

.getWebApplicationContext(context);

return ac.getBean(beanID);

}



public static Object getBean(String beanID){

if(ctx == null){

ctx = new FileSystemXmlApplicationContext(

"D://whao-work//src//test_web//test_web//WEB-INF//applicationContext.xml");

}

return ctx.getBean(beanID);

}

}

[/code:1] 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章