首页 技术 正文
技术 2022年11月13日
0 收藏 840 点赞 2,279 浏览 10694 个字

原文地址: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

Spring Remoting: HTTP Invoker–转

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

Spring Remoting: HTTP Invoker–转

  • 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

Spring Remoting: HTTP Invoker–转

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
123456 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
12345678910 package com.studytrails.tutorials.springremotinghttpinvokerserver; public class GreetingServiceImpl implements GreetingService{ @Overridepublic 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
12345678910111213141516171819202122 <?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/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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
1234567891011121314151617 <!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

Spring Remoting: HTTP Invoker–转

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
123456 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
12345678910111213141516 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
123456789101112131415161718 <?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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 ProgramHttp 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

Spring Remoting: HTTP Invoker–转
(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

Spring Remoting: HTTP Invoker–转

  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)

Spring Remoting: HTTP Invoker–转

  • 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

Spring Remoting: HTTP Invoker–转

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

Spring Remoting: HTTP Invoker–转
(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

Spring Remoting: HTTP Invoker–转

  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)

Spring Remoting: HTTP Invoker–转

  • 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

Spring Remoting: HTTP Invoker–转

Browsing the ProgramHttp 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.Spring Remoting: HTTP Invoker–转

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. 
Spring Remoting: HTTP Invoker–转

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.Spring Remoting: HTTP Invoker–转

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896