`
zhuchengzzcc
  • 浏览: 440663 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java Hessian小试(转)

    博客分类:
  • RPC
阅读更多

      相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议(Binary),因为采用的是二进制协议,所以它很适合于发送二进制数据。Hessian通常通过Web应用来提供服务,因此非常类似于WebService。只是它不使用SOAP协议。 
      Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。Hessian的server端提供一个servlet基类, 用来处理发送的请求,而Hessian的这个远程过程调用,完全使用动态代理来实现的,,推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。

 

Hessian处理过程示意图:
客户端——>序列化写到输出流——>远程方法(服务器端)——>序列化写到输出流 ——>客户端读取输入流——>输出结果

环境搭建
Hessian的下载和安装请按如下步骤进行:
(1)登http://www.caucho.com/hessian/下载Hessian。
(2)把Hessian相应的Jar包放到Web应用下,所有的jar文件都应该放在WEB-INF/lib下,该文件也不例外。

 

 

两种方式
1.纯Hessian
这种方式主要是适用于工程中没有适用像spring框架的情况下,好处是配置方便,但是当内容多的情况下,配置的内容很多。

 

 

package test;

import java.util.List;

/**
 * @author zhuc
 * @version 2012-5-17 下午9:15:50
 */
public interface IService {
	/**
	 * @param name
	 * @return
	 */
	public String sayHello(String name);
	
	/**
	 * @return
	 */
	public Car getMyCar();
	
	/**
	 * @return
	 */
	public List<String> getList();
}


package test;

import java.util.ArrayList;
import java.util.List;

import com.caucho.hessian.server.HessianServlet;

/**
 * @author zhuc
 * @version 2012-5-17 下午9:16:35
 */
public class ServiceImpl extends HessianServlet implements IService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8385639368192939451L;

	@Override
	public String sayHello(String name) {
		return "hello: " + name;
	}

	@Override
	public Car getMyCar() {
		Car c = new Car();
		c.setName("哈哈车");
		return c;
	}

	@Override
	public List<String> getList() {
		List<String> list = new ArrayList<String>();
		list.add("haha");
		list.add("hehe");
		return list;
	}

}


package test;

import java.io.Serializable;

/**
 * @author zhuc
 * @version 2012-5-17 下午10:29:18
 */
public class Car implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1115598660168001267L;

	private String name;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
}

 

 

web.xml的详细配置

<servlet>
		<servlet-name>Hello</servlet-name>
		<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
		<init-param>
			<param-name>home-class</param-name>
			<param-value>test.ServiceImpl</param-value>
		</init-param>
		<init-param>
			<param-name>home-api</param-name>
			<param-value>test.IService</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>

 

客户端远程调用服务器端提供的接口,利用的就是Hessian的HessianProxyFactory,来实现远程代理

 

package test;

import java.net.MalformedURLException;
import java.util.List;

import com.caucho.hessian.client.HessianProxyFactory;

/**
 * @author zhuc
 * @version 2012-5-17 下午9:31:14
 */
public class ClientTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String url = "http://localhost:8080/Hessian/Hello";
		HessianProxyFactory factory = new HessianProxyFactory();
		try {
			IService hello = (IService) factory.create(IService.class, url);
			System.out.println(hello.sayHello("zhuc-no"));

			Car c = hello.getMyCar();
			System.out.println(c.getName());

			List<String> list = hello.getList();
			for (String string : list) {
				System.out.println(string);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

	}

}
 

 

输出结果,成功

hello: zhuc-no
 哈哈车
 haha
 hehe

 

 

2.Hessian与Spring整合
相比上一种方式,这个方式就有点麻烦了。Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务,web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。而HessianServiceExporter提供bean服务。
所以Hessian与Spring整合主要就是一下两个工作:
1:通过DispatcherServlet来拦截URL请求。
2:HessianServiceExporter提供bean服务,Spring使用HessianServiceExporter,将一个常规bean导出成Hessian服务。 

 

修改web.xml的详细配置 

 

<servlet>
		<servlet-name>remoting</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--   
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/remoting-servlet.xml</param-value>  
        </init-param>  
        
       	 在/WEB-INF/增加一个叫servlet-name-servlet.xml配置文件。该文件的命名有一定的规则,
       	 红色servlet-name需要和web.xml中的<servlet-name>springhessian</servlet-name>定义的名称相匹配,
       	 比如本例子应叫remoting-servlet.xml,这样简单也不容易出错。
       	 
		当然该文件也可以自己任意命名。如果是自定义的文件名称不符合上述默认的规则,
		需要在<servlet>中增加<init-param>相关属性,人为指定加载配置文件,否则会报错
         --> 
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>remoting</servlet-name>
		<url-pattern>/remoting/*</url-pattern>
	</servlet-mapping>

 

 

配置remoting-servlet.xml文件,将其放于/WEB-INF/下

 

<?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:context="http://www.springframework.org/schema/context"
	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.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 定义普通bean实例 -->
	<bean id="serviceImpl" class="test.ServiceImpl" />

	<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
	<bean name="/hessianCommentService"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!-- 需要导出的目标bean -->
		<property name="service" ref="serviceImpl" />
		<!-- Hessian服务的接口 -->
		<property name="serviceInterface" value="test.IService" />
	</bean>
</beans>

 

客户端定义一个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:context="http://www.springframework.org/schema/context"
	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.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="myServiceClient"
		class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://localhost:8080/Hessian/remoting/hessianCommentService</value>
		</property>
		<property name="serviceInterface">
			<value>test.IService</value>
		</property>
	</bean>

</beans>

 

 

客户端调用

 

 

package spring;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.Car;
import test.IService;

/**
 * @author zhuc
 * @version 2012-5-17 下午9:59:31
 */
public class ClientTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"spring/remoting-client.xml");
		IService hello = (IService) context.getBean("myServiceClient");
		System.out.println(hello.sayHello("zhuc-spring"));

		Car c = hello.getMyCar();
		System.out.println(c.getName());

		List<String> list = hello.getList();
		for (String string : list) {
			System.out.println(string);
		}
	}

}

 

 

输出结果同上。

 

注:上述spring方式,客户端中的请求URL

http://localhost:8080/Hessian/remoting/hessianCommentService 中的hessianCommentService需要和remoting-servlet.xml配置中HessianServiceExporter的bean name相同,否则将会找不到server端

分享到:
评论
1 楼 zyslovely 2014-02-20  
http://localhost:8080/Hessian/remoting/hessianCommentService

这里写错了,应该是
http://localhost:8080/remoting/hessianCommentService</value>

相关推荐

Global site tag (gtag.js) - Google Analytics