Hessian 示例

Hessian是caucho公司开发的一种基于二进制RPC协议(Remote Procedure Call protocol)的轻量级远程调用框架

1、web.xml中新增servlet

<web-app version="3.0" 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_3_0.xsd">

	<display-name>Archetype Created Web Application</display-name>

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

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:/spring-hessian.xml
		</param-value>
	</context-param>
	
	<!-- 整合Spring实现 -->
	<servlet>
		<servlet-name>hello</servlet-name>
		<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>hello</servlet-name>
		<url-pattern>/hello</url-pattern>
	</servlet-mapping>
	
	<!-- 不整合Spring实现  -->
	<servlet>
		<servlet-name>hi</servlet-name>
		<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
		<init-param>
			<param-name>home-class</param-name>
			<param-value>com.sky.learn.impl.HelloServiceImpl</param-value>
		</init-param>
		<init-param>
			<param-name>home-api</param-name>
			<param-value>com.sky.learn.inf.HelloService</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>hi</servlet-name>
		<url-pattern>/hi</url-pattern>
	</servlet-mapping>

</web-app>
2、整合Spring方式,需添加bean

bean的name必须与servlet的name一致,本例中为hello

<?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:mvc="http://www.springframework.org/schema/mvc"
     xmlns:cache="http://www.springframework.org/schema/cache"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc.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">   
  	
  	<context:annotation-config />
  	
  	<context:component-scan base-package="com.sky.learn" />
  	
  	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
  	
  	<bean name="hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
  		<property name="service" ref="helloService" />
  		<property name="serviceInterface" value="com.sky.learn.inf.HelloService" />
  	</bean>
  	
</beans>
3、定义接口与实现类

package com.sky.learn.inf;

public interface HelloService {

	void sayHello(String name);
	
	void sayHi(String name);
	
}
package com.sky.learn.impl;

import org.springframework.stereotype.Service;

import com.sky.learn.inf.HelloService;

@Service("helloService")
public class HelloServiceImpl implements HelloService {

	@Override
	public void sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("Hello World ! Hello " + name + " !");
	}

	@Override
	public void sayHi(String name) {
		// TODO Auto-generated method stub
		System.out.println("Hi, World ! Hi, " + name + " !");
	}

}
4、测试类
package com.sky.learn.test;

import com.caucho.hessian.client.HessianProxyFactory;
import com.sky.learn.inf.HelloService;

public class Test {

	public static void main(String[] args) {
		try {
			String url = "http://localhost:8080/hessian-web/hello";
			HessianProxyFactory factory = new HessianProxyFactory();
			HelloService helloService = (HelloService) factory.create(HelloService.class, url);
			helloService.sayHello("sky");
			
			String url2 = "http://localhost:8080/hessian-web/hi";
			HelloService helloService2 = (HelloService) factory.create(HelloService.class, url2);
			helloService2.sayHi("sky");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
将项目发布,运行测试类控制台输出:

Hello World ! Hello sky !
Hi, World ! Hi, sky !






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值