hessian 初步

主页 http://hessian.caucho.com/

接口部分自定义,示例:

package kzg.hessian.hi.api;

public interface HelloService
{
	String hello(String msg);
	Cdata getData1();
}

Cdata(需要实现Serializable接口):

package kzg.hessian.hi.api;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Cdata implements Serializable
{
	private static final long serialVersionUID = 1L;
	private String s1;
	private int s2;
	private List ss = new ArrayList();
	private Map map = new HashMap();

	public Cdata(String arg)
	{
		map.put("m1", "m1v");
		ss.add("l1");
		s1 = arg;
		s2 = 12;
	}

	public String getS1()
	{
		return s1;
	}

	public int getS2()
	{
		return s2;
	}

	public List getSs()
	{
		return ss;
	}

	public Map getMap()
	{
		return map;
	}
}

服务需要继承HessianServlet,HelloService接口不是必须实现的,只要有这个方法即可:

package kzg.hessian.hi.service;

import com.caucho.hessian.server.HessianServlet;

import kzg.hessian.hi.api.Cdata;
import kzg.hessian.hi.api.HelloService;

@SuppressWarnings("serial")
public class HelloServiceImpl extends HessianServlet implements HelloService
{
	public String hello(String msg)
	{
		return "travel back from server:" + msg;
	}

	public Cdata getData1()
	{
		Cdata data = new Cdata("server data");
		return data;
	}
}

web.xml配置好连接,这和配置普通servlet没什么区别:

     <servlet>
		<servlet-name>hi</servlet-name>
		<servlet-class>
			kzg.hessian.hi.service.HelloServiceImpl
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>hi</servlet-name>
		<url-pattern>/hello.hs</url-pattern>
	</servlet-mapping>

客户端代码:

package kzg.hessian.hi.client;

import java.net.MalformedURLException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import kzg.hessian.hi.api.Cdata;
import kzg.hessian.hi.api.HelloService;

import com.caucho.hessian.client.HessianProxyFactory;

public class Hello
{
	public static void main(String[] args)
	{
		//		String url = "http://localhost:10010/hessian-test/hello.hs";
		
		//https begin
		String url = "https://localhost:10043/hessian-test/hello.hs";
		System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");

		HostnameVerifier hv = new HostnameVerifier() {
			public boolean verify(String urlHostName, SSLSession session)
			{
				return urlHostName.equals(session.getPeerHost());
			}
		};
		
		HttpsURLConnection.setDefaultHostnameVerifier(hv);
		//https end
		
		HessianProxyFactory factory = new HessianProxyFactory();
		HelloService api = null;
		try
		{
			api = (HelloService) factory.create(HelloService.class, url);
			Cdata data = api.getData1();
			System.out.println("call---->" + api.hello("hi."));
			System.out.println("string:" + data.getS1());
			System.out.println("int:" + data.getS2());
			System.out.println("array:" + data.getSs());
			System.out.println("map:" + data.getMap());
		}
		catch (MalformedURLException e)
		{
			System.out.println(e);
		}
	}
}


大家看到https部分是在使用https时需要使用的[其实不起作用],如果不使用https,则可以忽略。

关于httpclient https证书问题,参见http://blog.ureshika.com/archives/400.html 相关描述。


另外性能方面:

http://pt.alibaba-inc.com/wp/experience_1089/hessian-set-serializerfactory-performance.html

这文章我没看明白,还没怎么使用。


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1