欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hessian的java示例

程序员文章站 2022-04-20 23:03:17
...
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需要添加的配置
<servlet>
    <servlet-name>Alarm</servlet-name>
    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
    <init-param>
      <param-name>home-class</param-name>
      <param-value>my.hessian.service.impl.AlarmImpl</param-value>
    </init-param>
    <init-param>
      <param-name>home-api</param-name>
      <param-value>my.hessian.service.IAlarm</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Alarm</servlet-name>
    <url-pattern>/Alarm</url-pattern>
  </servlet-mapping>
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文件
<?xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
       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" >   
         
  <!-- 定义普通的bean实例 -->
  <bean id="Trip" class="my.hessian.service.impl.TripAgent"/>
    <!--  使用HessianServiceExporter 将普通bean导出成Hessian服务-->
    <bean name="/remoting" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <!--  需要导出的目标bean-->
     <property name="service" ref="Trip"/>
       <!--  Hess;ian服务的接口-->
     <property name="serviceInterface" value="my.hessian.service.ITripAgent"/>
    </bean>
  </beans>
4.服务端web.xml添加如下配置
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加监听器 -->
</listener>
<!-- 指定spring的配置文件在哪里,在这个配置文件中导出了Hessian服务 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:my/hessian/config/spring/bean/remoting-servlet.xml</param-value>
</context-param>
<!-- Hessian通过Servlet提供远程服务,需要将某个匹配的模式映射到hessian服务中,spring的dispatcherServlet能完成此功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务,web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。而HessianServiceExporter提供bean服务。 -->
<servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>

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

    public  String  saveTrip(String tripStr);
}

6.客户端添加一个remoting-client.xml文件
<?xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
       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" >   
<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<!--下面的value是客户端要访问服务端的请求   knowledgePoints是服务端的项目访问名 ,remoting与服务端的一致,客户端请求服务端,匹配的请求到达服务端后,对于能匹配的请求,会通过spring的DispatcherServlet转发hessian服务-->           
<value>http://192.168.2.243/knowledgePoints/remoting</value>
</property>                         
<property name="serviceInterface"> 
<value>my.hessianClient.ITripAgent</value>
</property>
</bean>
</beans>

7.客户端web.xml中添加一下代码
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!--添加监听器 -->
</listener>
<!-- 指定spring的配置文件在哪里,在这个配置文件中导出了Hessian服务 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/remoting-client.xml</param-value>
</context-param>
</web-app>

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个)。该示例可运行。
三、其他(在包下找文件的两种写法)
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/my/hessianClient/remoting-client.xml</param-  value>
</context-param>
中/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。