java hessian 原理,hessian简单原理及实现例子–学习 | 学步园

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

hessian

com.caucho.hessian.server.HessianServlet

service-class

com.mooing.hessian.DemoService

hessian

/helloworld

客户端代码

注意如果新建客户端工程,需引入上述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中增加:

remote

org.springframework.web.servlet.DispatcherServlet

namespace

classes/remote-servlet

remote

/remote/*

以下是remote-servlet.xml的内容:

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

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,这个文件可随意命名,内容为:

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

com.mooing.hessian.DemoApi

客户端代码

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());

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值