浅尝Hessian远程调用

来源:互联网 发布:大学高等数学软件 编辑:程序博客网 时间:2024/06/07 19:17

Hessian是一个远程调用的技术,hessian由两个部分组成,服务端和客户端,服务端供客户端调用。下面先上一个小例子,然后再作一个简单的说明。

服务端项目结构图:
这里写图片描述

HelloService接口代码:

public interface HelloService {        public void hello();    }

HelloServiceImpl实现类代码:

@Service("helloService")    public class HelloServiceImpl implements HelloService {        @Override        public void hello() {            System.out.println("hello hession !");        }    }

spring相关的配置文件

<bean id="helloService"            class="com.hession.HelloServiceImpl"/>     <bean name="/hello"      class="org.springframework.remoting.caucho.HessianServiceExporter">         <property name="service" ref="helloService"/>         <property name="serviceInterface"                   value="com.hession.HelloService"/>     </bean>

项目中导入hessian包,在web.xml配置hessian的servlet-mapping

<servlet-mapping>    <servlet-name>dispatcher</servlet-name>    <url-pattern>*.html</url-pattern>    <url-pattern>*.json</url-pattern>    <url-pattern>*.xml</url-pattern>    <url-pattern>*.jhtml</url-pattern>    <url-pattern>*.do</url-pattern>    <url-pattern>/hessian/*</url-pattern>(配置hessian路径)</servlet-mapping>

客户端:
这里写图片描述

HelloService接口和服务端的一致,HessianClient代码如下:

public class HessionClient {        public static void main(String[] args) {           //具体路径根据服务端的配置改变而改变              String url="http://localhost:8182"                        +"/hessian/hessian/hello";            HessianProxyFactory pf = new HessianProxyFactory();            HelloService hello = null;            try {                hello = (HelloService)pf.create(                            HelloService.class,url);                hello.hello();            } catch (MalformedURLException e) {                e.printStackTrace();            }        }    } 

运行main方法即可调用服务端的业务。

原创粉丝点击