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 the ServiceInterface 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).
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).
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)
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)
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.
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).
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)
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.
(Alternatively you can go the folder containing the springremotinghessianserver-installer.jar and execute the jar using
java -jar springremotinghessianserver-installer.jar
command)
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.
(Alternatively you can go the folder containing the springremotinghessianclient-installer.jar and execute the jar using
java -jar springremotinghessianclient-installer.jar
command)
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.