Open In App

Spring – Remoting By Hessian

Improve
Improve
Like Article
Like
Save
Share
Report

We may use the HessianServiceExporter and HessianProxyFactoryBean classes to implement the hessian remoting service. The major advantage of Hessian’s is that Hessian works well on both sides of a firewall. Hessian is a portable language that may be used with other languages like PHP and.Net.

Implementation: To make a simple hessian application, you’ll need to make the following files:

  1. Calculation.java
  2. CalculationImpl.java
  3. web.xml
  4. hessian-servlet.xml
  5. client-beans.xml
  6. Client.java

The project structure looks as follows: 

A. File: Calculation.java

Java




// Java Program to illustrate Simple Interface
// Containing one method cube.
  
package com.geeksforgeeks;
  
// Interface
public interface Calculation {
    
    int cube(int number);
}


B. File: CalculationImpl.java

Java




// Java Program to Illustrate Implementation
// of the Calculation interface
  
package com.geeksforgeeks;
  
// Class
// Implementing interface
public class CalculationImpl implements Calculation {
    
    // Method
    public int cube(int number)
    {
        // Returning cube of a number
        return number * number * number;
    }
}


C. File: web.xml

The front controller is defined in this XML file called DispatcherServlet. Any request that ends in.http will be routed to DispatcherServlet.

XML




<?xml version="1.0" encoding="UTF-8"?>
<web-app
<servlet>
    <servlet-name>hessian</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
  
<servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>


D. File: hessian-servlet.xml

It has to be placed in the WEB-INF folder. It must be called servletname-servlet.xml. It specifies CalculationImpl and HessianServiceExporter as beans.

XML




<?xml version="1.0" encoding="UTF-8"?>
      
    <bean id="calculationBean" class="com.geeksforgeeks.CalculationImpl"></bean>
    <bean name="/Calculation.http" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="calculationBean"></property>
        <property name="serviceInterface" value="com.geeksforgeeks.Calculation"></property>
    </bean>
</beans>


E. File: client-beans.xml

The bean for HessianProxyFactoryBean is defined in this XML file. This class requires two attributes to be defined.

  1. serviceUrl
  2. serviceInterface

F. File: Client.java

This class obtains a Calculation object and runs the cube function.

Java




package com.geeksforgeeks;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class Client 
{
    public static void main(String[] args) 
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
        Calculation calculation = (Calculation)context.getBean("calculationBean");
        System.out.println(calculation.cube(4));
    }
}


Output:

 Note: In order to generate the above output,

  • Start and deploy the project, assuming that the server is listening on port 8888. Change the serviceURL in client-beans.xml if the port number is changed.
  • The Client.java file should then be compiled and run.


Last Updated : 24 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads