Spring Remoting: Hessian--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp
Concept Overview
The previous tutorial presents an overview of spring remoting and lists down various remoting protocols supported by Spring. In this tutorial we look at Spring support for Hessian. Hessian is a web service protocol that transfers binary data between a remote service and its client. Hessian has been released by Caucho Technology. It requires that the web service be hosted on an http server. The client uses HTTP protocol to invoke remote methods on the server. The important classes are : -
org.springframework.remoting.caucho.HessianServiceExporter - This exports the specified service as a servlet based http request handler. Hessian services exported by this class can be accessed by any hessian client.
org.springframework.remoting.caucho.HessianProxyFactoryBean - This is the factory bean for Hessian clients. The exposed service is configured as a spring bean. The ServiceUrl property specifies the URL of the service and theServiceInterface property specifies the interface of the implemented service.
Sample Program Overview
The example below is a GreetingService implemented as a remote Hessian service.
- aopalliance.jar
- commons-logging.jar
- log4j.jar
- org.springframework.aop.jar
- org.springframework.asm.jar
- org.springframework.beans.jar
- org.springframework.context.jar
- org.springframework.context.support.jar
- org.springframework.core.jar
- org.springframework.expression.jar
- hessian-3.1.5.jar
- Client sends a message call
- This message call is handled by a Hessian Proxy created by HessianProxyFactoryBean
- The Hessian Proxy converts the call into a remote call over HTTP
- The Hessian Service Adapter created by HessianServiceExporter intercepts the remote call over HTTP
- It forwards the method call to Service
Create the GreetingService interface as shown below.
Create a method named getGreeting() that takes a name as a parameter and returns the greeting message (see line 5 below).
1
2
3
4
5
6
|
package com.studytrails.tutorials.springremotinghessianserver; public interface GreetingService { String getGreeting(String name); } |
Create a class GreetingServiceImpl as shown below.
It implements the GreetingService interface (described earlier)
Implement the getGreeting() method by sending a greeting message (see lines 6-8 below).
1
2
3
4
5
6
7
8
9
10
|
package com.studytrails.tutorials.springremotinghessianserver; public class GreetingServiceImpl implements GreetingService{ @Override public String getGreeting(String name) { return "Hello " + name + "!" ; } } |
Create the hessian-servlet.xml file (see below).
Declare the 'greetingService' (see lines 14-15 below).
Export the 'greetingService' using Spring's HessianServiceExporter class (see lines 17-21 below).
Note the following properties of HessianServiceExporter class:
- service: the service class bean which shall handle the Hessian call (see line 18 below)
- serviceInterface: The interface to be used by Spring to create proxies for Hessian (see line 19 below)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? xml version = "1.0" encoding = "UTF-8" ?> xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc < bean id = "greetingService" class = "com.studytrails.tutorials.springremotinghessianserver.GreetingServiceImpl" /> < bean name = "/greetingService.http" class = "org.springframework.remoting.caucho.HessianServiceExporter" > < property name = "service" ref = "greetingService" /> < property name = "serviceInterface" value = "com.studytrails.tutorials.springremotinghessianserver.GreetingService" /> </ bean > </ beans > |
Create the web.xml file (see below).
Create the servlet mapping for the url pattern '*.http' (see line 16 below) for Spring's DispatcherServlet (see line 10 below)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" < web-app > < display-name >Spring Remoting: Hessian Server</ display-name > < servlet > < servlet-name >hessian</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >hessian</ servlet-name > < url-pattern >*.http</ url-pattern > </ servlet-mapping > </ web-app > |
Create the GreetingService interface as shown below.
Copy the GreetingService interface created for Hessian Server (described above) and paste it in Hessian Client source code while retaining the java package structure.
Note: For reference, the source code is shown below.
1
2
3
4
5
6
|
package com.studytrails.tutorials.springremotinghessianserver; public interface GreetingService { String getGreeting(String name); } |
Create a class TestSpringRemotingHessian shown below to test Spring Hessian Remoting.
Load spring configuration file (see line 11 below)
Get a reference to GreetingService using the bean name 'greetingService' (see line 12 below)
Call the GreetingService.getGreting() method by passing the name 'Alpha' (see line 13 below)
Print the greeting message (see line 14 below).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.studytrails.tutorials.springremotinghessianclient; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.studytrails.tutorials.springremotinghessianserver.GreetingService; public class TestSpringRemotingHessian { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-config-client.xml" ); GreetingService greetingService = (GreetingService)context.getBean( "greetingService" ); String greetingMessage = greetingService.getGreeting( "Alpha" ); System.out.println( "The greeting message is : " + greetingMessage); } } |
Create the spring-config-client.xml file (see below).
Declare the 'greetingService' using Spring's HessianProxyFactoryBean class (see lines 13-16 below).
Note the following properties of HessianProxyFactoryBean class:
- serviceUrl : refers the URL of the remote service (see line 14 below).
Note URL part 'greetingService' corresponds to bean name property of HessianServiceExporter bean defined in hessian-servlet.xml (defined earlier) - serviceInterface: The interface to be used by Spring to create proxies for Hessian (see line 15 below)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? xml version = "1.0" encoding = "UTF-8" ?> xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation=" < bean id = "greetingService" class = "org.springframework.remoting.caucho.HessianProxyFactoryBean" > < property name = "serviceUrl" value = "http://localhost:8080/springremotinghessianserver/greetingService.http" /> < property name = "serviceInterface" value = "com.studytrails.tutorials.springremotinghessianserver.GreetingService" /> </ bean > </ beans > |
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.
- Save the springremotinghessianserver-installer.jar on your machine
- Execute/Run the jar using Java Runtime Environment
(Alternatively you can go the folder containing the springremotinghessianserver-installer.jar and execute the jar using java -jar springremotinghessianserver-installer.jar command)
- You will see a wizard page as shown below
- Enter the location of the directory where you want the program to install and run (say, C:\Temp)
- The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
This shows that the Hessian Server program has run successfully on your machine
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.
- Save the springremotinghessianclient-installer.jar on your machine
- Execute/Run the jar using Java Runtime Environment
(Alternatively you can go the folder containing the springremotinghessianclient-installer.jar and execute the jar using java -jar springremotinghessianclient-installer.jar command)
- You will see a wizard page as shown below
- Enter the location of the directory where you want the program to install and run (say, C:\Temp)
- The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
This shows that the Hessian Client program has run successfully on your machine
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotinghessianserver . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.
The WAR file for this example is available as springremotinghessianserver.war in the download folder specified by you earlier (e.g. C:\Temp). The path for the WAR file is <DOWNLOAD_FOLDER_PATH>/springremotinghessianserver/dist/springremotinghessianserver.war.
This WAR file can be deployed in any webserver of your choice and example can be executed.
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotinghessianclient . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.
Spring Remoting: Hessian--转的更多相关文章
- Spring Remoting: Hessian
- spring与hessian整合例
spring与hessian的简单应用实现例: 开发环境:window7 64,jdk8,tomcat8,spring4.2.5,hessian4.0 开发语言:java hessianServer端 ...
- Spring Remoting: Remote Method Invocation (RMI)--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...
- Spring Remoting: HTTP Invoker--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview ...
- Spring Remoting: Burlap--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...
- Spring整合Hessian
Spring让Hessian变得不但强大,而且易用,但是易用背后,却有不少陷阱! 这个例子很简单,但实际上的确花费了我超过一小时的时间,排除了种种问题,最后问题终于水落石出. 整合以上篇Hel ...
- Lingo (Spring Remoting) : Passing client credentials to the server
http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...
- Spring整合Hessian访问远程服务
声明:该文章转载自Spring整合Hessian访问远程服务,本人搬过来只是为了记录下学习Hessian的过程,忘此博主理解,在此感谢,等本人有能力了再学一些原创的东东,本人实践了下,hessianS ...
- Asynchronous calls and remote callbacks using Lingo Spring Remoting
http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...
随机推荐
- JS的event对象--知识点总结
Event描述:event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. 需要注意的是:event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有 ...
- 攻城利器 —— Carthage简单介绍
01 Carthage简单介绍 主页:https://github.com/Carthage/Carthage.git 作者:Justin Spahr-Summers等 版本:0.18 目标:用最简单 ...
- 使用MFC提供的Http类下载和上传文件
1.下载文件 Download(const CString& strFileURLInServer, //待下载文件的URL const CString & strFileLocalF ...
- android: 从相册中选择照片
虽然调用摄像头拍照既方便又快捷,但并不是每一次我们都需要去当场拍一张照片的. 因为每个人的手机相册里应该都会存有许许多多张照片,直接从相册里选取一张现有的照 片会比打开相机拍一张照片更加常用.一个优秀 ...
- HTML 邮件链接,超链接发邮件
在网页中可以设置如“联系我们”.“问题反馈”等所谓的邮箱链接,类似网页超链接,只是可以直接打开默认邮箱程序. 使用<a href="mailto:youEMail@xxx.yyy&qu ...
- wmi详解,RPC和防火墙
135端口:Microsoft在这个端口运行DCE RPC end-point mapper为它的DCOM服务.这与UNIX 111端口的功能很相似.使用DCOM和RPC的服务利用计算机上的end-p ...
- 用 Mahout 和 Elasticsearch 实现推荐系统
原文地址 本文内容 软件 步骤 控制相关性 总结 参考资料 本文介绍如何用带 Apache Mahout 的 MapR Sandbox for Hadoop 和 Elasticsearch 搭建推荐引 ...
- javascript - 二叉树
都是些简单的东西,所以直接上代码了. /** * Created by huangjacky on 14-10-3. */ function Node(element, left, right) { ...
- [Lua]50行代码的解释器,用来演示lambda calculus
嗯,来写写经过: 在知乎上看见用Belleve牛用javascript写了一个精简的lisp解释器 => 我也想写一个,用lua写,能多简单呢? => 写了一个阉割的scheme解释器,包 ...
- CentOS 7.2 搭建 Ghost 博客
因为平时记录一些文档或想法基本使用 markdown 的语法,Mac 下推荐一款 markdown 的编辑器 Haroopad:上周无意发现 Ghost 有支持 Mac 的桌面版本了,并且同样开源 h ...