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 !
6837

被折叠的 条评论
为什么被折叠?



