`
hz_chenwenbiao
  • 浏览: 996082 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Hessian实例(转)

阅读更多

试着写了一个Hessian的例子,是参考caucho官网上的一个example,很简单,也没什么实际的意义,但足以领会Hessian的用法。 

1、建立一个Remote Interface 

package com.hessian.test;

public interface MathService {
	public int add(int a, int b);
}

 2、Service Implementation 

package com.hessian.test;

import com.caucho.hessian.server.HessianServlet;

public class HessianMathService extends HessianServlet implements MathService {

	public int add(int a, int b) {
		return a + b;
	}
}

 3、在web.xml 配置文件里加入servlet处理类来处理远程调用

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<servlet>
		<servlet-name>math</servlet-name>
		<servlet-class>com.hessian.test.HessianMathService</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>math</servlet-name>
		<url-pattern>/hessian/math</url-pattern>
	</servlet-mapping>



	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 4、Java client 

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;
import com.hessian.test.MathService;

public class HessianClientTest {
	public static void main(String[] args) {
		String url = "http://localhost:8080/HessianServer/hessian/math";
		HessianProxyFactory factory = new HessianProxyFactory();
		MathService math = null;
		try {
			math = (MathService) factory.create(MathService.class, url);
		} catch (MalformedURLException e) {
			System.out.println("occur exception: " + e);
		}
		System.out.println("3 + 2 = " + math.add(3, 2));
	}
}

 

5 将服务端的接口打成jar包给client端引用,然后就可以运行client程序,看出hissian远程调用的结果了。

 

6 具体可以看附件

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics