Spring Remoting: HTTP Invoker

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

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)

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 usingjava -jar springremotinghttpinvokerserver-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 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 usingjava -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 calledspringremotinghttpinvokerserver
. 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 calledspringremotinghttpinvokerclient
. 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.