hessian-spring简单应用
hessian-spring简单应用
冰雷卡尔 发表于4年前
hessian-spring简单应用
  • 发表于 4年前
  • 阅读 106
  • 收藏 1
  • 点赞 0
  • 评论 0
摘要: Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI的功能
1、服务端,这里是一个web项目:
项目结构

pom.xml文件中加入spring和hessian的依赖配置

web.xml中hessian的配置:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<!-- 手动指定applicationContext.xml文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
		</param-value>
	</context-param>

	<!-- Spring 启动加载设置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- hessian begin -->
	<servlet>
  	    <servlet-name>remote</servlet-name>
  	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	    <init-param>  
			<param-name>namespace</param-name>  
	        <param-value>classes/hessian/remote-servlet</param-value>  
	    </init-param>
	    <load-on-startup>1</load-on-startup>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>remote</servlet-name>
  		<url-pattern>/remote/*</url-pattern>
  	</servlet-mapping>
  	<!-- hessian end -->
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
applicationContext中加入<import resource="classpath:hessian/remote-servlet.xml"/>

remote-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 向外发布的hession用户接口 -->
	<bean id="userInfoServiceImpl" class="org.hessian.service.impl.UserInfoServiceImpl" />
	<bean name="/userInfoService" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="userInfoServiceImpl" />
		<property name="serviceInterface" value="org.hessian.service.IUserInfoService" /> 
	</bean>
</beans>
再接下来就是向外发布的接口、实现类和所需要用到的DO类,服务端拥有客户端相同的接口和DO类,并且DO类要序列化

启动服务,对外发布用户接口成功,接下来看客户端如何调用该服务

2、客户端,java项目、web项目都可以:
项目结构

pom.xml文件中加入spring和hessian的依赖配置

web.xml配置,只负责加载applicationContext文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<!-- 手动指定applicationContext.xml文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
		</param-value>
	</context-param>
	<!-- Spring 启动加载设置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>
applicationContext中加入
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
			 	<value>classpath:remote-hession.properties</value>
			</list>
		</property>
</bean>
<import resource="classpath:hessian/hessian-client.xml"/>



remote-hession.properties里面配置服务所在地址
hessian.http.path = http://localhost:9090/hessian_server

hessian-client.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- client调用发布的hession用户接口 -->
	<bean name="userInfoService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl">
			<value>${hessian.http.path}/remote/userInfoService</value>
		</property>
		<property name="serviceInterface">
			<value>org.hessian.service.IUserInfoService</value>
		</property>
	</bean>
</beans>



接下来就是和服务一致的接口类和DO类,下面就是测试代码:
package org.hessian.service;

import javax.annotation.Resource;

import org.hessian.service.common.JunitBase;
import org.hessian.service.common.UserInfo;
import org.junit.Test;

import com.caucho.hessian.client.HessianProxyFactory;

public class HessianClientTest extends JunitBase {

	@Resource
	private IUserInfoService userInfoService;
	
	/**
	 * 方式一
	 * java client(HessianProxyFactory)
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		UserInfo userinfo = new UserInfo();
		userinfo.setName("jack");
		
	    String url = "http://localhost:9090/hessian_server/remote/userInfoService";  
	    HessianProxyFactory factory = new HessianProxyFactory();
	    IUserInfoService userInfoService = (IUserInfoService) factory.create(IUserInfoService.class, url);
	    userInfoService.addUserInfo(userinfo);
	}
	
	/**
	 * 方式二,该junit测试会加载spring一系列配置文件
	 * spring web
	 * @throws Exception
	 */
	@Test
	public void testHessian() throws Exception {
		UserInfo userinfo = new UserInfo();
		userinfo.setName("china");
		userInfoService.addUserInfo(userinfo);
	}
}



JunitBase.java
package org.hessian.service.common;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class JunitBase
{
	private final Logger logger = Logger.getLogger(this.getClass());

	@Before
	public void before() {
		logger.debug("----------------------------Start Test---" + this.getClass() + "----------------------------------");
	}

	@After
	public void after() {
		logger.debug("----------------------------End Test---" + this.getClass() + "----------------------------------");
	}
}



HessianServiceExporter only supports POST requests

共有 人打赏支持
粉丝 30
博文 116
码字总数 81854
×
冰雷卡尔
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: