hessian的java示例

阅读更多
Hessian:hessian是一个轻量级的remoting onhttp工具。
一、hessian简单示例:
1.服务端接口:
public interface IAlarm {
  public  String  saveAlarm(String alarmStr);

  public  String  firstAlarm(String alarmStr);
}
2.服务端实现类
public class AlarmImpl extends HessianServlet implements IAlarm{

    public String saveAlarm(String alarmStr) {
        return  alarmStr+",保存成功。"; 
    }

    public String firstAlarm(String alarmStr) {
        return alarmStr+",通知。";
    }

}
3.服务端web.xml需要添加的配置

    Alarm
    com.caucho.hessian.server.HessianServlet
   
      home-class
      my.hessian.service.impl.AlarmImpl
   

   
      home-api
      my.hessian.service.IAlarm
   

    1
 

 
    Alarm
    /Alarm
 

4.客户端接口(跟服务端接口一致,接口所在包路径可不同)
public interface IAlarm {
  public  String  saveAlarm(String alarmStr);
 
  public  String  firstAlarm(String alarmStr);
}
5.客户端调用
    HessianProxyFactory factory =  new HessianProxyFactory();
    String url="http://192.168.2.243/knowledgePoints/Alarm";
    IAlarm alarm=(IAlarm)factory.create(IAlarm.class, url);
    System.out.println(alarm.saveAlarm("超速报警"));
6.示例说明
   服务端和客户端都需要引入hessian-4.0.7.jar包。该示例可运行。
 
二、hessian与spring整合示例:
  1.服务端接口
    public interface ITripAgent {

       public  String  saveTrip(String tripStr);
   }
  2.服务端实现类
public class TripAgent extends HessianServlet implements ITripAgent{

    public  String  saveTrip(String tripStr){
        return tripStr+",保存成功";
    };
}

3.服务端WEB-INF下加一个remoting-servlet.xml文件

       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >   
         
 
 
   
   
   
    
      
    
   

 

4.服务端web.xml添加如下配置

  org.springframework.web.context.ContextLoaderListener



    contextConfigLocation
    classpath:my/hessian/config/spring/bean/remoting-servlet.xml



    remoting
    org.springframework.web.servlet.DispatcherServlet
    1


remoting
/remoting/*


5.客户端接口(跟服务端接口一致,接口所在包路径可不同)
public interface ITripAgent {

    public  String  saveTrip(String tripStr);
}

6.客户端添加一个remoting-client.xml文件

       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >   


           
http://192.168.2.243/knowledgePoints/remoting
                         
 
my.hessianClient.ITripAgent




7.客户端web.xml中添加一下代码

org.springframework.web.context.ContextLoaderListener



    contextConfigLocation
    /WEB-INF/remoting-client.xml



8.客户端调用
   ApplicationContext context = new    ClassPathXmlApplicationContext("my/hessianClient/remoting-client.xml"); 
    ITripAgent trip = (ITripAgent) context.getBean("myServiceClient");
    System.out.println(trip.saveTrip("白石洲到茂华大厦的行程"));

9.示例说明
   服务端和客户端都需要引入spring的jar包(多个,不知道需要哪些,就把spring的jar全引入) ,服务端需要hessain的jar包(1个)。该示例可运行。
三、其他(在包下找文件的两种写法)
 
    contextConfigLocation
    /WEB-INF/classes/my/hessianClient/remoting-client.xml

中/WEB-INF/classes/my/hessian/config/spring/bean/remoting-servlet.xml
与 classpath:my/hessian/config/spring/bean/remoting-servlet.xml 是等价的

四、遇到的奇怪问题
hessian与spring整合的示例中, remoting-servlet.xml放在包路径my.hessian.config.spring.bean下时,二(7) 中配置 classpath:my/hessian/config/spring/bean/remoting-servlet.xml会报找不到 WEB_INF/remoting-servlet.xml文件的错误,报的路径找不到与我配置的路径是不一样的,这个错报的很离谱,将remoting-servlet.xml放到WEB-INF下时,就不会报错了,有知道原因的,请回复我,3q。

你可能感兴趣的:(hessian,示例代码)