You need to create following files for creating a simple hessian application:
- Calculation.java
- CalculationImpl.java
- web.xml
- hessian-servlet.xml
- client-beans.xml
- Client.java
It is the simple interface containing one method cube.
- package com.javatpoint;
- public interface Calculation {
- int cube(int number);
- }
2) CalculationImpl.java
This class provides the implementation of Calculation interface.
- package com.javatpoint;
- public class CalculationImpl implements Calculation{
- public int cube(int number) {
- return number*number*number;
- }
- }
3) web.xml
In this xml file, we are defining DispatcherServlet as the front controller. If any request is followed by .http extension, it will be forwarded to DispatcherServlet.
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <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>
4) hessian-servlet.xml
It must be created inside the WEB-INF folder. Its name must be servletname-servlet.xml. It defines bean for CalculationImpl and HessianServiceExporter.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>
- <bean name="/Calculation.http"
- class="org.springframework.remoting.caucho.HessianServiceExporter">
- <property name="service" ref="calculationBean"></property>
- <property name="serviceInterface" value="com.javatpoint.Calculation"></property>
- </bean>
- </beans>
5) client-beans.xml
In this xml file, we are defining bean for HessianProxyFactoryBean. You need to define two properties of this class.
- serviceUrl
- serviceInterface
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="calculationBean"
- class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <property name="serviceUrl"
- value="http://localhost:8888/hessian/Calculation.http"></property>
- <property name="serviceInterface" value="com.javatpoint.Calculation"></property>
- </bean>
- </beans>
In this example, our project name is hessian, i.e. used as the context root in the serviceURL.
6) Client.java
This class gets the instance of Calculation and calls the cube method.
- package com.javatpoint;
- 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(5));
- }
- }
How to run this example
Start and deploy the project, here we are assuming that server is running on 8888 port number. If the port number is different, change the serviceURL in client-beans.xml.
Then, Compile and Run the Client.java file.