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 RMIHessian 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

Required Libraries

  • 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

Interaction Flow

  • 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

Http Invoker Server Code Package Structure

Http Invoker Server Source Code

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).

GreetingService.java

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).

GreetingServiceImpl.java

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)
httpinvoker-servlet.xml

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" ?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<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)

web.xml

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"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

<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>

Http Invoker Client Code Package Structure

Http Invoker Client Source Code

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.

GreetingService.java

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).

TestSpringRemotingHttpInvoker.java

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)
spring-config-client.xml

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"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<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>

Running Sample Program

Http Invoker Server Sample Program

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.

Download And Automatically Run Http Invoker Server Sample Program

  • 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

Http Invoker Client Sample Program

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.

Download And Automatically Run Http Invoker Client Sample Program

  • 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

Browsing the Program

Http Invoker Server Sample Code

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.

Redeploying this sample program in a different web server

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. 

Http Invoker Client Sample Code

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.

时间: 2024-07-30 00:05:08

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 http request than RMI and works well across the firewall. By the help of HttpInvokerServiceExporter and HttpInvokerProxyFactoryBean classes, we can im

Spring Remoting: Remote Method Invocation (RMI)--转

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring provides four ways to develop remote services. Remote services are services hosted on remote servers and accessed by clients over the network. For examp

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 Sp

Spring Remoting: Burlap--转

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the earlier tutorials we saw an introduction to Spring remoting and its support for RMI and Hessian. In this tutorial we look at another remoting protoco

Spring远程调用技术&lt;3&gt;-Spring的HTTP Invoker

前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙.  另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invker是一个新的远程调用模型,作为Spring框架的一部分,能够执行基于HTTP的远程调用(让防火墙不为难),并使用java的序列化机制(让开发者也乐观其变). Spring的HTTPinvoker把HTTP的简单性和java内置的对象序列化机制融合在一起.这使得HTTPinvoker成为替代RMI H

Spring远程调用HTTP invoker

前言 在日常的工作中,会有客户端调用远程服务端接口的需求,这样的需求实现,在网上查到有像RMI.hessian.Burlap等,下文给出HTTP Invoker实现远程调用,他不用使用者考虑对象序列化的问题,对使用者,非常的友好. 下面介绍在Spring中配置HTTP Invoker实现远程调用.分为服务提供方和服务消费方两部分. 案例 服务提供方 1.定义一个接口和接口实现类,服务提供方提供. WelcomeService.java package com.aaron.service; imp

[转]Spring——jar包详解

原文地址:http://my.oschina.net/huhaoren/blog/300856?p=1 spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试, 正式应用系统中是用不得这些类的. 除了spring.jar文件,Spring还包括有其它13个独立的jar包,各自包含着对应的Spring组件,用户可以根据自己的需要

Spring 4 官方文档学习 Spring与Java EE技术的集成

本部分覆盖了一下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Enterprise JavaBeans (EJB) integration -- EJB集成 Chapter 30, JMS (Java Message Service) -- JMS (Java 消息服务) Chapter 31, JMX Chapter 32, JCA CCI Chapter 33,

spring中各jar功能及jar包之间的依赖关系

(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类. (2) spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件.创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类.如果应用只需基本的IoC/DI支持,引入spri