Spring Remoting: HTTP Invoker--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp
Concept Overview
In the earlier articles we saw an introduction to spring remoting and its support for RMI, Hessian and Burlap. In this tutorial we look at one more support for remoting - HttpInvoker. HttpInvoker combines the ease of Hessian and Burlap, in that it is very easy to set up. It serializes and deserializes java object for trasport over the network. However, probably the only drawback is that Http Invoker is bound to java and hence the clients all need to be java based. This is the recommended choice for remoting for java-java based communication. The main classes are :org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter - This is a servlet API based Http request handler. It is used to export the remote services. It takes in a service property that is the service to be exported and aServiceInterface that specifies the interface that the service is tied to.
org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean - This is a proxy factory for creating http invoker proxies. It has a serviceUrl property that must be an http url exposing an http invoker service. This class serializes the objects that are sent to remote services and deserializes the objects back.
Sample Program Overview
- 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
- org.springframework.web.jar
- org.springframework.web.servlet.jar
- Client sends a message call
- This message call is handled by a HTTP Proxy created by HttpInvokerProxyFactoryBean
- The HTTP Proxy converts the call into a remote call over HTTP
- The HTTP Service Adapter created by HttpInvokerServiceExporter 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.springremotinghttpinvokerserver; 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.springremotinghttpinvokerserver; public class GreetingServiceImpl implements GreetingService{ @Override public String getGreeting(String name) { return "Hello " + name + "!" ; } } |
Create the httpinvoker-servlet.xml file (see below).
Declare the 'greetingService' (see lines 14-15 below).
Export the 'greetingService' using Spring's HttpInvokerServiceExporter class (see lines 17-21 below).
Note the following properties of HttpInvokerServiceExporter class:
- service: the service class bean which shall handle the HTTP call (see line 19 below)
- serviceInterface: The interface to be used by Spring to create proxies for HTTP (see line 20 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.springremotinghttpinvokerserver.GreetingServiceImpl" /> < bean name = "/greetingService.http" class = "org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" > < property name = "service" ref = "greetingService" /> < property name = "serviceInterface" value = "com.studytrails.tutorials.springremotinghttpinvokerserver.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
|
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" < web-app > < display-name >Spring Remoting: Http Invoker Server</ display-name > < servlet > < servlet-name >httpinvoker</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >httpinvoker</ servlet-name > < url-pattern >*.http</ url-pattern > </ servlet-mapping > </ web-app > |
Create the GreetingService interface as shown below.
Copy the GreetingService interface created for Http Invoker Server (described above) and paste it in Http Invoker 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.springremotinghttpinvokerserver; public interface GreetingService { String getGreeting(String name); } |
Create a class TestSpringRemotingHttpInvoker shown below to test Spring Http Invoker 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.springremotinghttpinvokerclient; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.studytrails.tutorials.springremotinghttpinvokerserver.GreetingService; public class TestSpringRemotingHttpInvoker { 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 HttpInvokerProxyFactoryBean class (see lines 13-16 below).
Note the following properties of HttpInvokerProxyFactoryBean class:
- serviceUrl : refers the URL of the remote service (see line 14 below).
Note URL part 'greetingService.http' corresponds to bean name property of HttpInvokerServiceExporter bean defined in httpinvoker-servlet.xml (defined earlier) - serviceInterface: The interface to be used by Spring to create proxies for Http Invoker (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.httpinvoker.HttpInvokerProxyFactoryBean" > < property name = "serviceUrl" value = "http://localhost:8080/springremotinghttpinvokerserver/greetingService.http" /> < property name = "serviceInterface" value = "com.studytrails.tutorials.springremotinghttpinvokerserver.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 springremotinghttpinvokerserver-installer.jar on your machine
- Execute/Run the jar using Java Runtime Environment
(Alternatively you can go the folder containing the springremotinghttpinvokerserver-installer.jar and execute the jar using java -jar springremotinghttpinvokerserver-installer.jarcommand)
- 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 Http Invoker 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 springremotinghttpinvokerclient-installer.jar on your machine
- Execute/Run the jar using Java Runtime Environment
(Alternatively you can go the folder containing the springremotinghttpinvokerclient-installer.jar and execute the jar using java -jar springremotinghclient-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 Http Invoker 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 springremotinghttpinvokerserver . 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 springremotinghttpinvokerserver.war in the download folder specified by you earlier (e.g. C:\Temp). The path for the WAR file is <DOWNLOAD_FOLDER_PATH>/springremotinghttpinvokerserver/dist/springremotinghttpinvokerserver.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 springremotinghttpinvokerclient . 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: HTTP Invoker--转的更多相关文章
- Spring Remoting by HTTP Invoker Example--reference
Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for ...
- Spring Remoting: Remote Method Invocation (RMI)--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...
- 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 Remoting: Burlap--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...
- Spring Remoting: Hessian--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...
- Asynchronous calls and remote callbacks using Lingo Spring Remoting
http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...
- Spring远程调用技术<3>-Spring的HTTP Invoker
前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙. 另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invke ...
- spring3.2.2 remoting HTTP invoker 实现方式
最近跟朋友聊天,聊到他们现在项目的架构都是把数据层跟应用层分离开来,中间可以加memcached等的缓存系统,感觉挺好的,很大程度上的降低耦合,然后还明确分配了数据层跟应用层任务.也方便定位.找到问题 ...
- spring remoting源码分析--Hessian分析
1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...
随机推荐
- 虚方法的调用是怎么实现的(单继承VS多继承)
我们知道通过一个指向之类的父类指针可以调用子类的虚方法,因为子类的方法会覆盖父类同样的方法,通过这个指针可以找到对象实例的地址,通过实例的地址可以找到指向对应方法表的指针,而通过这个方法的名字就可以确 ...
- 解决eclipse使用Search弹出错误问题
在eclipse中搜索时,搜索完之后有时候会弹出错误对话框,虽然错误内容有时候不同,但是解决办法都一样. 这个问题是由于eclipse中文件不同步引起的.在eclipse中,工程文件是由eclipse ...
- 互联网的寒冬来了,BAT都不社招了
一 总理上次来到创业街,是四个月,要不就是五个月前了. 之后,全国创业形势一路走红,锣鼓喧天鞭炮齐鸣.大众创业万众创新,颇有大炼钢铁亩产万斤之势,尤其在媒体上. 再之后,2015 进入下半年,风投圈的 ...
- php - 执行Linux命令没有报错但也没有输出
今天我需要在同事访问我的PHP页面的时候执行一段python脚本,于是我的代码是这样写的: 1 <?php 2 function my_workjob(){ 3 $this->makeLo ...
- centos7 Linux 尝试使用crontab
一.安装crontab [root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum install crontabs 说明:vixie-cr ...
- ServiceManager 小结
1 ServiceManger 根据name优先从Map中获取IBinder,例如AMS.WMS.PMS:如果Map中没有对应的IBinder,我们获取Serviceanager的代理ServiceM ...
- C# Like参数化 小记
strBuilder.Append(" and b.name like '%' + @name + '%'"); parameters.Add(new SqlParameter(& ...
- Mac 下安装mitmproxy
环境: Mac OS X 10.9.4 1. 安装 直接用pip 安装 pip install mitmproxy 发现在安装依赖包 lxml 的时候报错 In : /private/tmp/pip ...
- [原创]自定义BaseAcitivity的实现,统一activity的UI风格样式
在开发过程中经常遇到多个activity是同一种样式类型的情况,如果分别对其进行UI的布局,不但比较繁琐,而且后续维护过程人力成本很高,不利于敏捷开发.解决的方案是采用抽象后的BaseActi ...
- MSSql使用SQL语句快速查看表对的就说明,及表字段描述及字段类型
--表描述 SELECT tbs.name 表名,ds.value 描述 FROM sys.extended_properties ds LEFT JOIN sysobjects tbs ON ds. ...