Hessian

来源:互联网 发布:淘宝网旺旺在哪里 编辑:程序博客网 时间:2024/04/28 19:31

    公司不同pool之间提供service都是通过hessian来开放接口的,个人觉得hessian的使用非常简单。做个demo如下:

0.首先创建web工程,并加载hessian.jar。

1.创建service,即我们通过它来提供接口:

[java] view plaincopy
  1. public interface BasicService {  
  2.   
  3.     public void setServiceName(String serverName);  
  4.   
  5.     public String getServiceName();  
  6.       
  7.     public User createUser();  
  8.       
  9. }  


2.创建service实现:

[java] view plaincopy
  1. public class BasicServiceImpl implements BasicService {  
  2.   
  3.     private String serviceName;  
  4.       
  5.     @Override  
  6.     public void setServiceName(String serverName) {  
  7.         this.serviceName = serverName;  
  8.     }  
  9.       
  10.     @Override  
  11.     public String getServiceName() {  
  12.         return this.serviceName;  
  13.     }  
  14.       
  15.     @Override  
  16.     public User createUser() {  
  17.         return new User("zhangsan""123456");  
  18.     }  
  19. }  

3.创建需要通过hessian传递的对象(必须序列化):

[java] view plaincopy
  1. public class User implements Serializable {  
  2.   
  3.     private static final long serialVersionUID = 5792818254468116836L;  
  4.   
  5.     private String username;  
  6.       
  7.     private String password;  
  8.       
  9.     public User(String username, String password) {  
  10.         this.username = username;  
  11.         this.password = password;  
  12.     }  
  13.   
  14.     public String getUsername() {  
  15.         return username;  
  16.     }  
  17.   
  18.     public void setUsername(String username) {  
  19.         this.username = username;  
  20.     }  
  21.   
  22.     public String getPassword() {  
  23.         return password;  
  24.     }  
  25.   
  26.     public void setPassword(String password) {  
  27.         this.password = password;  
  28.     }  
  29. }  

4.配置web.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>HessianServer</display-name>  
  4.     
  5.   <servlet>  
  6.     <servlet-name>basic</servlet-name>  
  7.     <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>  
  8.     <init-param>  
  9.         <param-name>service-class</param-name>  
  10.         <param-value>com.loujinhe.service.impl.BasicServiceImpl</param-value>  
  11.     </init-param>  
  12.   </servlet>  
  13.     
  14.   <servlet-mapping>  
  15.     <servlet-name>basic</servlet-name>  
  16.     <url-pattern>/basic</url-pattern>  
  17.   </servlet-mapping>  
  18.     
  19. </web-app>  

5.创建测试用例:

[java] view plaincopy
  1. import java.net.MalformedURLException;  
  2.   
  3. import org.junit.Before;  
  4. import org.junit.Test;  
  5.   
  6. import com.caucho.hessian.client.HessianProxyFactory;  
  7. import com.loujinhe.service.BasicService;  
  8.   
  9.   
  10. public class BasicServiceTest {  
  11.       
  12.     private BasicService basicService;  
  13.   
  14.     @Before  
  15.     public void init() throws MalformedURLException {  
  16.         HessianProxyFactory factory = new HessianProxyFactory();  
  17.         String url = "http://localhost:8080/HessianServer/basic";  
  18.         basicService = (BasicService)factory.create(BasicService.class, url);  
  19.     }  
  20.       
  21.     @Test  
  22.     public void testBasic() {  
  23.         basicService.setServiceName("BasicService");  
  24.         System.out.println(basicService.getServiceName());  
  25.           
  26.         System.out.println(basicService.createUser().getUsername());  
  27.         System.out.println(basicService.createUser().getPassword());  
  28.     }  
  29. }  

6.启动服务器,运行测试用例,会得到如下结果:
0 0
原创粉丝点击