hessian分布式应用

使用hessian的好处在于哪里?因人而异。

hessian是一种提供远程服务的方式,类似webservice。

Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。Hessian的server端提供一个servlet基类,用来处理发送的请求,而Hessian的这个远程过程调用,完全使用动态代理来实现的,,采用面向接口编程,因此,Hessian服务建议通过接口暴露。

    Hessian的初衷就是支持动态类型,格式紧凑,跨语言Hessian是使用自己的序列化机制实现的编组和反编组,其支持的数据类型是有限制的,不支持复杂的对象,可以穿透防火墙。而JavaRMI则支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。他也有它的缺点,他只能通过RMI协议来进行访问无法通过HTTP协议访问,无法穿透防火墙。

用它来进行分布式应用最合适不过。


创建hessian的简单流程:

1.创建一个接口

2.实现这个接口

以上两步其实就是我们常用的service层的方式。

3.通过HessianProxyFactory创建(服务端)客户端

4.配置web.xml。


1.创建接口

package com.skyecho.shenzhenairlines.report.service;

import java.util.List;
import java.util.Map;

import com.skyecho.shenzhenairlines.commons.service.GenericService;
import com.skyecho.shenzhenairlines.report.search.DeptSearchCriteria;
import com.skyecho.shenzhenairlines.report.vo.DeptVO;


public interface DeptService extends GenericService<DeptVO> {

	/**
	 * 分页查询
	 * @param dsc 查询条件实体类
	 * @return
	 */
	public List<DeptVO> getDeptListByPage(DeptSearchCriteria dsc);
	/**
	 * 统计记录总数
	 * @param dsc 查询条件实体类
	 * @return
	 */
	public Integer getCountDept(DeptSearchCriteria dsc);
	
	public void insertTransaction();
}


2.实现接口

package com.skyecho.shenzhenairlines.report.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.skyecho.shenzhenairlines.commons.exception.DaoException;
import com.skyecho.shenzhenairlines.commons.service.impl.GenericServiceImpl;
import com.skyecho.shenzhenairlines.report.handle.DeptHandle;
import com.skyecho.shenzhenairlines.report.handle.DeptHandle2;
import com.skyecho.shenzhenairlines.report.search.DeptSearchCriteria;
import com.skyecho.shenzhenairlines.report.service.DeptService;
import com.skyecho.shenzhenairlines.report.vo.DeptVO;

public class DeptServiceImpl extends GenericServiceImpl<DeptVO> implements DeptService {

	private DeptHandle deptHandle;	
	private DeptHandle2 deptHandle2;

	public void setDeptHandle2(DeptHandle2 deptHandle2) {
		this.deptHandle2 = deptHandle2;
	}

	@Override
	protected void initService() {
		handle = deptHandle;		
	}

	public void setDeptHandle(DeptHandle deptHandle) {
		this.deptHandle = deptHandle;
	}


	@Override
	public List<DeptVO> getDeptListByPage(DeptSearchCriteria dsc) {
		return deptHandle.getDeptListByPage(dsc);
	}

	@Override
	public Integer getCountDept(DeptSearchCriteria dsc) {
		return deptHandle.getCountDept(dsc);
	}

	@Override
	public void insertTransaction() {
		/*Map<String,Object> map = new HashMap<String,Object>();
		map.put("deptno", 50);
		map.put("dname", "testt");
		map.put("loc", "Chinat");
		try {
			boolean d = deptHandle.update(map);
			boolean d2 = deptHandle2.update(map);
			System.out.println("d="+d+" ,d2="+d2);
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		/*DeptVO dept = new DeptVO();
		dept.setDeptno(70);
		dept.setDname("testTrans2");
		dept.setLoc("locTrans2");
		
		try {
			boolean d2 = deptHandle2.insert(dept);
			boolean d = deptHandle.insert(dept);
			
			System.out.println("d="+d+" ,d2="+d2);
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		DeptSearchCriteria dsc = new DeptSearchCriteria();
		
		int count1 = deptHandle.getCountDept(dsc);
		int count2 = deptHandle2.getCountDept(dsc);		
		int count3 = deptHandle.getCountDept(dsc);
		int count4 = deptHandle2.getCountDept(dsc);	
		System.out.println("-------------------------------------");
		System.out.println("count1="+count1+"count2="+count2+"count3="+count3+"count4="+count4);
		System.out.println("-------------------------------------");
		
	}
	
	

}

3.配置服务端与客户端

服务端:

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd 
                    	http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
                    	http://www.springframework.org/schema/aop 
                    	http://www.springframework.org/schema/aop/spring-aop.xsd
                    	http://www.springframework.org/schema/context 
                    	http://www.springframework.org/schema/context/spring-context-3.0.xsd
  						http://www.springframework.org/schema/cache
  						http://www.springframework.org/schema/cache/spring-cache.xsd ">

	<!-- 远程服务 -->  
	<bean name="/deptservice" class="org.springframework.remoting.caucho.HessianServiceExporter">  
	    <property name="service" ref="deptService"/>  
	    <property name="serviceInterface">  
	        <value>  
	            com.skyecho.shenzhenairlines.report.service.DeptService
	        </value>  
	    </property>  
	</bean> 
</beans>
客户端

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:conf/properties/systemconf.properties</value><!--该配置文件包含客户端路径的url配置-->
			</list>
		</property>
	</bean>
	<!-- 客户端Hessian代理工厂Bean -->
 	<bean id="hessianTest" 
 		class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> 
<!-- 		 请求代理Servlet路径  -->
 		<property name="serviceUrl"> 
 			<value>http://127.0.0.1/szAirlinesSystem/deptservice</value> 
 		</property> 
<!-- 		 接口定义  -->
 		<property name="serviceInterface"> 
 			<value>com.skyecho.shenzhenairlines.report.service.DeptService</value> 
 		</property> 
 	</bean>
 	<bean id="configUtil" class="com.skyecho.shenzhenairlines.commons.helper.ConfigUtil" lazy-init="false" factory-method="getInstance" autowire="byName">
		<property name="systemConfigService" ref="systemConfigService" />
	</bean>
 	<bean id="dictionaryUtil" class="com.skyecho.shenzhenairlines.commons.helper.DictionaryUtil" lazy-init="false" factory-method="getInstance" autowire="byName">
		<property name="dictionaryService" ref="dictionaryService" />
	</bean>
	
	<!-- 异常日志 -->
	<bean id="exceptionService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl" value="${szAirlinesSystem.url}/exceptionservice"/>
		<property name="serviceInterface" value="com.skyecho.shenzhenairlines.commons.exception.service.ExceptionService"/> 	
 	</bean>

</beans>  

注意,此处配置了一个客户端路径

${szAirlinesSystem.url}//szAirlinesSystem.url=http\://localhost\:8080/szAirlinesSystem

最后配置容器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  	<!-- 防止内部资源泄露 -->
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:conf/spring/spring-admin.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>interservice</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
    		classpath*:conf/spring/spring-hessian.xml
    	</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>interservice</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>404</error-code>
    <location>/pages/exception/pageNotFound.jsp</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/pages/exception/pageError.jsp</location>
  </error-page>
</web-app>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值