学习Hessian-1
2016-05-21 22:49
423 查看
创建Hessian服务接口
1、创建一个maven项目2、在pom中加入相关依赖
无
3、创建Service接口
package org.didinem.service; /** * Created by didinem on 16-5-21. */ public interface IHessianService { public String serviceMethod(String name); }
4、maven install
搭建Hessian服务端
Hessian官方对于Hessian服务的说明The Hessian library makes it possible to write services by extending HessianServlet.
Any public method is treated as a service method. So adding new methods is as easy as writing a normal Java class.
Because the service is implemented as a Servlet, it can use all the familiar servlet data in the ServletContext, just like a normal servlet.
简单来说,如果自己想要实现一个Hessian的服务,只要继承HessianServlet,然后这个类所有的public方法就可以当作是Hessian服务的方法了。(那能不能排除指定的public method呢?)
1.创建maven web项目
2.引入相关依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.didinem</groupId> <artifactId>hessian-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
3、实现Hessian服务
/** * Created by didinem on 16-5-21. */ public class HessianServiceImpl extends HessianServlet implements IHessianService { @Override public String serviceMethod(String name) { return "Hello " + name; } }
4、配置Hessian服务
Since the HessianServlet is a standard servlet, it can also be configured in the standard servlet configuration.
配置Hessian服务就是配置Servlet
<servlet> <servlet-name>hessianService</servlet-name> <servlet-class>HessianServiceImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>hessianService</servlet-name> <url-pattern>/hessianService</url-pattern> </servlet-mapping>
5、配置并启动tomcat
这里的hessian-demo-server:war和hessian-demo-server:war exploded的区别猜测如下(待验证):
前者是把web工程打成一个war包部署到tomcat,而后者只是把web工程的目录拷贝到了tomcat的webapps下。
启动tomcat,访问地址http://localhost:8080/hessian-demo-server/hessianService
当页面出现如下图类似内容时,代表服务至少部署成功,能不能用就再说了
搭建Hessian客户端来测试
1、创建maven项目2、引入相关依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.didinem</groupId> <artifactId>hessian-demo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
3、编写客户端
Hessian官方对于客户端的说明
Using a Hessian service from a Java client is like calling a method. The HessianProxyFactory creates proxies which act like normal Java objects, with possibility that the method might throw a protocol exception if the remote connection fails. Using HessianProxyFactory requires JDK 1.3.
Each service will have a normal Java interface describing the service. The trivial hello, world example just returns a string. Because the Hessian services support Java serialization, any Java type can be used.
简单来说通过HessianProxyFactory获得服务的代理,然后像调用普通方法一样调用Hessian服务。
官方给出的样例:
package example; import com.caucho.hessian.client.HessianProxyFactory; public class BasicClient { public static void main(String []args) throws Exception { String url = "http://www.caucho.com/hessian/test/basic"; HessianProxyFactory factory = new HessianProxyFactory(); Basic basic = (Basic) factory.create(Basic.class, url); System.out.println("Hello: " + basic.hello()); } }
那这样就很简单了
package org.didinem.client; import com.caucho.hessian.client.HessianProxyFactory; import org.didinem.service.IHessianService; /** * Created by didinem on 16-5-22. */ public class HessianClient { public static void main(String[] args) { String url = "http://localhost:8080/hessian-demo-server/hessianService"; HessianProxyFactory factory = new HessianProxyFactory(); IHessianService hessianService = (IHessianService) factory.create(IHessianService.class, url); System.out.println(hessianService.serviceMethod("hehe")); } }
执行输出结果:
相关文章推荐
- hessian 在PHP中的使用介绍
- 简单的Hessian接口开发实例
- Hessian 介绍
- Hessian
- Spring3.x--使用远程调用
- hessian spring 调用
- hessian java调用
- Spring集成Hessian
- SpringMVC集成Hessian
- Java二进制协议之Hessian协议格式(3)
- Hessian初步使用(配合spring)
- Hessian 使用示例总结
- Spring4整合Hessian4(MavenWeb实例)
- 二、hessian与spring集成
- 一、hessian通讯协议简介
- Hessian接口学习笔记
- Spring Hessian整合 404 问题
- hessian学习
- hessian配置
- Hessian原理分析