2019独角兽企业重金招聘Python工程师标准>>>

Hessian入门(与Spring集成)

By:wtang

说明 :

1.    讲述如何配置Hessian的服务器端(与Spring集成).

2.    讲述客户端如何调用

①   使用HessianProxyFactory  Hessian代理工厂直接调用

②   使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.

1.    讲述如何配置Hessian的服务器端(与Spring集成).

接口定义类: com.wtang.isay. Isay:

[java] view plain copy print ?
  1. package com.wtang.isay;
  2. public interface Isay {
  3. public String sayHello(String arg1,String arg2);
  4. }

package com.wtang.isay;public interface Isay { public String sayHello(String arg1,String arg2);}</pre>

接口具体实现类: com.wtang.isay. IsayImpl

[java] view plain copy print ?
  1. package com.wtang.isay;
  2. public class IsayImpl implements Isay {
  3. public String sayHello(String arg1, String arg2) {
  4. return "Hello:" + arg1 + arg2;
  5. }
  6. }

package com.wtang.isay;public class IsayImpl implements Isay { public String sayHello(String arg1, String arg2) { return "Hello:" + arg1 + arg2; }}</pre>

配置Web.xml:

[java] view plain copy print ?
  1. <servlet>
  2. <servlet-name>remote</servlet-name>
  3. <!-- 使用Spring的代理Servlet -->
  4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5. <init-param>
  6. <param-name>namespace</param-name>
  7. <param-value>classes/remote-servlet</param-value>
  8. </init-param>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>remote</servlet-name>
  13. <url-pattern>/remote/*</url-pattern>
  14. </servlet-mapping>

<servlet> <servlet-name>remote</servlet-name> <!-- 使用Spring的代理Servlet --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>classes/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></pre>

配置remote-servlet.xml[该文件位于src目录下,即编译后存在与classes下]:

[java] view plain copy print ?
  1. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  2. <beans>
  3. <!-- 接口的具体实现类 -->
  4. <bean id="impl" class="com.wtang.isay.IsayImpl" />
  5. <!-- 使用Spring的HessianServie做代理 -->
  6. <bean name="/helloSpring"
  7. class="org.springframework.remoting.caucho.HessianServiceExporter">
  8. <!-- service引用具体的实现实体Bean-->
  9. <property name="service" ref="impl" />
  10. <property name="serviceInterface" value="com.wtang.isay.Isay" />
  11. </bean>
  12. <!-- 可以配置多个HessianServiceExporter代理Bean -->
  13. </beans>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- 接口的具体实现类 --> <bean id="impl" class="com.wtang.isay.IsayImpl" /> <!-- 使用Spring的HessianServie做代理 --> <bean name="/helloSpring" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service引用具体的实现实体Bean--> <property name="service" ref="impl" /> <property name="serviceInterface" value="com.wtang.isay.Isay" /> </bean> <!-- 可以配置多个HessianServiceExporter代理Bean --></beans></pre>

注:

这个文件为什么叫remote-servlet.xml呢?

因为我们在web.xml中有配置:<servlet-name>remote</servlet-name>。

所以remote-servlet.xml的文件名必须以

<servlet-name>中配置的servlet-name作为文件名的开头,

且文件名的格式必须是[servlet-name]-servlet.xml格式,否则检测不到。

即:

<param-value>classes/remote-servlet</param-value>

所以文件名为remote-servlet.xml。

2.    讲述客户端如何调用

① 使用HessianProxyFactory  Hessian代理工厂直接调用

即:

[java] view plain copy print ?
  1. package com.wtang.test;
  2. import java.net.MalformedURLException;
  3. import com.caucho.hessian.client.HessianProxyFactory;
  4. import com.wtang.isay.Isay;
  5. public class NormalClient {
  6. public static void main(String[] args) throws MalformedURLException {
  7. //Spring Hessian代理Servelet
  8. String url = "http://localhost:8080/HessianSpring/remote/helloSpring";
  9. HessianProxyFactory factory = new HessianProxyFactory();
  10. Isay api = (Isay) factory.create(Isay.class, url);
  11. System.out.println(api.sayHello("chen", "weitang"));
  12. }
  13. }

package com.wtang.test;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;import com.wtang.isay.Isay;public class NormalClient { public static void main(String[] args) throws MalformedURLException { //Spring Hessian代理Servelet String url = "http://localhost:8080/HessianSpring/remote/helloSpring"; HessianProxyFactory factory = new HessianProxyFactory(); Isay api = (Isay) factory.create(Isay.class, url); System.out.println(api.sayHello("chen", "weitang")); }}</pre>

输出Hello:chenweitang

2.    讲述客户端如何调用

② 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.

配置客户端 remote-client.xml:

[java] view plain copy print ?
  1. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  2. <beans>
  3. <!-- 客户端Hessian代理工厂Bean -->
  4. <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
  5. <!-- 请求代理Servlet路径 -->
  6. <property name="serviceUrl">
  7. <value>http://localhost:8080/HessianSpring/remote/helloSpring</value>
  8. </property>
  9. <!-- 接口定义 -->
  10. <property name="serviceInterface">
  11. <value>com.wtang.isay.Isay</value>
  12. </property>
  13. </bean>
  14. </beans>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- 客户端Hessian代理工厂Bean --> <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <!-- 请求代理Servlet路径 --> <property name="serviceUrl"><value>http://localhost:8080/HessianSpring/remote/helloSpring</value> </property> <!-- 接口定义 --> <property name="serviceInterface"> <value>com.wtang.isay.Isay</value> </property> </bean></beans></pre>

调用:

[java] view plain copy print ?
  1. package com.wtang.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.wtang.isay.Isay;
  5. public class SpringClient {
  6. public static void main(String[] args) {
  7. ApplicationContext contex = new ClassPathXmlApplicationContext(
  8. "remote-client.xml");
  9. // 获得客户端的Hessian代理工厂bean
  10. Isay i = (Isay) contex.getBean("clientSpring");
  11. System.out.println(i.sayHello("chen", "weitang"));
  12. }
  13. }

package com.wtang.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wtang.isay.Isay;public class SpringClient { public static void main(String[] args) { ApplicationContext contex = new ClassPathXmlApplicationContext( "remote-client.xml"); // 获得客户端的Hessian代理工厂bean Isay i = (Isay) contex.getBean("clientSpring"); System.out.println(i.sayHello("chen", "weitang")); }}</pre>

输出Hello:chenweitang

转载于:https://my.oschina.net/201003674/blog/650263

Hessian入门(与Spring集成)相关推荐

  1. Kafka 入门和 Spring Boot 集成

    2019独角兽企业重金招聘Python工程师标准>>> Kafka 入门和 Spring Boot 集成 概述 kafka 是一个高性能的消息队列,也是一个分布式流处理平台(这里的流 ...

  2. Spring集成JMS入门

    1.预备知识 在学习Spring集成JMS之前最好可以是先去了解JMS基本概念和ActiveMQ 自己学习Spring整合JMS的一些心得,希望可以帮到大家 2.Spring整合JMS 2.1.使用点 ...

  3. 函数集成redis与Spring集成

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正 redis与Spring集成比较简单,本文不触及Spring的一些基本概念,读都需先具有Spring的相干知识. 先在maven中添 ...

  4. jersey spring_实施Jersey 2 Spring集成

    jersey spring Jersey是Oracle提供的出色的Java JAX-RS规范参考实现. 去年,当我们开始为大容量网站构建RESTful后端Web服务时,我们选择使用JAX-RS API ...

  5. 实施Jersey 2 Spring集成

    Jersey是Oracle提供的出色的Java JAX-RS规范参考实现. 去年,当我们开始为大容量网站构建RESTful后端Web服务时,我们选择使用JAX-RS API作为我们的REST框架和Sp ...

  6. 引入Spring集成

    在本文中,我们介绍Spring Integration . 如果您以前没有使用过Spring Integration,那么可能会帮助您复习Gregor Hohpe的Enterprise Integra ...

  7. 《springcloud超级入门》Spring Cloud和Dubbo的区别及各自的优缺点《三》

    了解为什么需要微服务.最初的服务化解决方案是给相同服务提供一个统一的域名,然后服务调用者向这个域发送 HTTP 请求,由 Nginx 负责请求的分发和跳转. 这种架构存在很多问题:Nginx 作为中间 ...

  8. 阿里RocketMq试用记录+简单的Spring集成

    CSDN学院招募微信小程序讲师啦      程序猿全指南,让[移动开发]更简单!        [观点]移动原生App开发 PK HTML 5开发     云端应用征文大赛,秀绝招,赢无人机! 阿里R ...

  9. 亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像)

    亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像) 大噶好,我们继续延续上一章,学习如何使用OSS存放用户头像代码示例; 在application.propert ...

最新文章

  1. 谢尔排序/缩减增量排序(C++)
  2. 数学知识笔记:拉格朗日乘子
  3. dropout理解(一)
  4. 操作篇 bgp协议了解与学习
  5. GIS应对新挑战——空间信息网格技术探寻
  6. 快速排序算法c语言lomuto,快速排序(N.Lomuto版)
  7. 支付宝蚂蚁森林入选2019年世界环境日实践案例
  8. spark学习-66-源代码:schedulerBackend和taskScheduler的创建(4)-yarn
  9. sql server 2005学习笔记之触发器简介(一)
  10. [VB]SaveSetting 语句 和 DeleteSetting 语句
  11. MySQL中的主键约束和外键约束
  12. [渝粤教育] 中国地质大学 管理信息系统 复习题
  13. 微软云 Azure 云服务器 Web应用服务云计算解决方案
  14. WebApp最佳实践用户体验篇:针对多种屏幕尺寸合理设计
  15. python 电路仿真spice_SPICE模型电路仿真器的用法及功能解析
  16. JS解决因循环绑定click事件失效
  17. OSAL 之功耗管理
  18. Whitelabel Error Page 的原因
  19. 步入J2EE架构和过程
  20. python namedtuple默认值_python 使用 namedtuple

热门文章

  1. 图像“位操作”有什么用?
  2. 【AutoML】强化学习如何用于模型蒸馏?
  3. 【每周NLP论文推荐】 对话管理中的标志性论文介绍
  4. Git的思想和基本工作原理
  5. ASTreeView Demo:Add, Edit Delete nodes
  6. 100. Same Tree同样的树
  7. Web前端基础——HTML
  8. FileOutStream
  9. easyui treegrid idField 所在属性中值有花括号(如Guid)当有鼠标事件时会报错,行记录一下...
  10. 产品治理体系:产品顶层框架