接到通知會在公司閒置一個月,真是開心啊!終於有時間研究研究新東西給自己充充電了~,hessian是我接觸過的最爲簡單的遠程調用框架,聽各位大大說效率也還不錯,好處也一大堆。俺就不說廢話了~
先寫一個接口,將來要給客戶端調用的
- package hessian.service;
- /**
- *
- * @author absolute
- */
- public interface HessianService {
- public String sayHello();
- }
然後再寫個類實現這個接口
- package hessian.service;
- import com.caucho.hessian.server.HessianServlet;
- /**
- *
- * @author alsolute
- */
- public class HessianServiceImpl extends HessianServlet implements HessianService {
- /** Creates a new instance of HessianServiceImpl */
- public HessianServiceImpl() {
- }
- public String sayHello() {
- return "hello hessian!";
- }
- }
OK,這兩個類寫好了任務就完成一大半了,只需要做一些簡單的配置任務就完成一大半了,啥也別說了來看配置文件吧
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>test</servlet-name>
- <servlet-class>hessian.service.HessianServiceImpl</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>test</servlet-name>
- <url-pattern>/test</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>
- 30
- </session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>
- index.jsp
- </welcome-file>
- </welcome-file-list>
- </web-app>
服務端已經做好了,現在來看客戶端是怎麼調用的,it is very 的簡單:
- package hessian.client;
- import com.caucho.hessian.client.HessianProxyFactory;
- import hessian.service.HessianService;
- import java.net.MalformedURLException;
- /**
- *
- * @author absolute
- */
- public class HessianClient {
- private String url = "http://localhost:8084/HessianTest/test";
- public HessianService getService() throws MalformedURLException {
- HessianProxyFactory factory = new HessianProxyFactory();
- return (HessianService) factory.create(HessianService.class, url);
- }
- public static void main(String[] args) {
- HessianClient client = new HessianClient();
- try {
- HessianService service = client.getService();
- System.out.println(service.sayHello());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }