`

Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI(Remote Method Invocation,远程方法调用)

 
阅读更多
Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI(Remote Method Invocation,远程方法调用)的功能。采用的是二进制RPC(Remote Procedure Call Protocol,远程过程调用协议)协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
  在进行基于Hessian的项目开发时,应当注意以下几点:

  ▲JAVA服务器端必须具备以下几点:

  ·包含Hessian的jar包。

  ·设计一个接口,用来给客户端调用。

  ·实现该接口的功能。

  ·配置web.xml,配好相应的servlet。

  ·对象必须实现Serializable 接口。

  ·对于复杂对像可以使用Map的方法传递。

  ▲客户端必须具备以下几点:

  ·java客户端包含Hessian.jar的包。

  ·具有和服务器端结构一样的接口。

·利用HessianProxyFactory调用远程接口。





下面是一个hessian的简单例子



场景1:服务端 客户端都不使用spring


Java服务器端:

环境:j2sdk1.4.2、Tomcat6.0

依赖的包:hessian-3.1.6.jar



新建一个名为HessianServer的web project。将hessian-3.2.0.jar放入WEB-INF/lib文件夹中。

创建接口
package com.mooing.hessian;
public interface DemoApi {

       public void setName(String name);

       public String sayHello();

       public User getUser();

}

实现接口
package com.mooing.hessian;

public class DemoService  implements DemoApi{
       private String name;
       @Override
       public void setName(String name) {
              this.name=name;
       }
       @Override
       public String sayHello() {
              System.out.println("hello,world!");
              return "hello:"+name;

       }
       @Override
       public User getUser() {
              return new User("ming","m123");

       }
}


创建User,注意实现序列化
 
package com.mooing.hessian;

import java.io.Serializable;
public class User implements Serializable {

 

       private static final long serialVersionUID = 1L;

 

       private String name = "kitty";

       private String psw = "nopww";

 

       public User() {

       }

 

       public User(String name, String psw) {

              super();

              this.name = name;

              this.psw = psw;

       }

 

       public String getName() {

              return name;

       }

 

       public void setName(String name) {

              this.name = name;

       }

 

       public String getPsw() {

              return psw;

       }

 

       public void setPsw(String psw) {

              this.psw = psw;

       }

}

 


配置web.xml
<servlet>

              <servlet-name>hessian</servlet-name>

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

              <init-param>

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

                     <param-value>com.mooing.hessian.DemoService</param-value>

              </init-param>

       </servlet>

 

       <servlet-mapping>

              <servlet-name>hessian</servlet-name>

              <url-pattern>/helloworld</url-pattern>

       </servlet-mapping>

客户端代码
注意如果新建客户端工程,需引入上述jar,还需要服务端导出DemoService和User两个类的jar,放入lib。或创建两个一摸一样的类。
package com.mooing.hessian;
import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;
public class HessianClient {

       public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

              String url = "http://localhost:8080/jms1/helloworld";

              HessianProxyFactory pf = new HessianProxyFactory();

              DemoApi da = (DemoApi) pf.create(url);

              da.setName("test");

              System.out.println(da.sayHello());

              System.out.println(da.getUser());

       }

}



tomcat启动服务端,运行客户端,结果如下

hello:test

ming

m123



场景2:服务端 客户端都使用spring(在上述基础上)
依赖的包:

1.  Hessian包:hessian-3.1.6.jar

2.  spring-framework-2.0.2包:

a)   aopalliance.jar

b)   commons-logging.jar

c)   log4j-1.2.14.jar

d)   spring.jar

e)   spring-aop.jar

f)   spring-beans.jar

g)   spring-context.jar

h)   spring-core.jar

i)   spring-jdbc.jar

j)   spring-jms.jar

k)   spring-web.jar

l)   spring-webmvc.jar

配置web.xml,web.xml中增加:
<servlet>

              <servlet-name>remote</servlet-name>

              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

              <init-param>

                     <param-name>namespace</param-name>

                     <param-value>classes/remote-servlet</param-value>

              </init-param>

       </servlet>

 

       <servlet-mapping>

              <servlet-name>remote</servlet-name>

              <url-pattern>/remote/*</url-pattern>

       </servlet-mapping>

以下是remote-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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

       <!-- 注入内部实现接口 -->

       <bean id="demoService" class="com.mooing.hessian.DemoService" />

           <!-- 注入hessian发布接口  name为调用名称-->

       <bean name="/helloDemo" class="org.springframework.remoting.caucho.HessianServiceExporter">

              <!-- 关联接口实现 -->

              <property name="service" ref="demoService" />

              <!-- 关联接口 -->

              <property name="serviceInterface" value="com.mooing.hessian.DemoApi" />

       </bean>

</beans>


1)     如果客户端不使用spring,客户端代码如下
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.mooing.hessian.DemoApi;
public class ClientSpring {

       public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

              String url = "http://localhost:8080/jms1/remote/helloDemo";

              HessianProxyFactory pf = new HessianProxyFactory();

              DemoApi da = (DemoApi) pf.create(url);

              da.setName("test");

              System.out.println(da.sayHello());

              System.out.println(da.getUser());

       }

}



2)    如果客户端使用spring,需要导入上述jar,在客户端工程src目录下,新建一个remote-hessian.xml,这个文件可随意命名,内容为:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

       <bean id="helloDemo" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

              <property name="serviceUrl">

                     <value>

                            http://localhost:8080/jms1/remote/helloDemo

                     </value>

              </property>

              <property name="serviceInterface">

                     <value>com.mooing.hessian.DemoApi</value>

              </property>

       </bean>

</beans>



客户端代码
import java.net.MalformedURLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.caucho.hessian.client.HessianProxyFactory;
import com.mooing.hessian.DemoApi;

public class ClientSpring {
       public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

              // String url = "http://localhost:8080/jms1/remote/helloDemo";

              // HessianProxyFactory pf = new HessianProxyFactory();

              // DemoApi da = (DemoApi) pf.create(url);

              // da.setName("test");

              // System.out.println(da.sayHello());

              // System.out.println(da.getUser());

              ApplicationContext context = new ClassPathXmlApplicationContext("remote-hessian.xml");

              DemoApi da = (DemoApi) context.getBean("helloDemo");

              da.setName("test spring");

              System.out.println(da.sayHello());

              System.out.println(da.getUser());

       }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics