Example of Remoting by Hessian



You need to create following files for creating a simple hessian application:

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

It is the simple interface containing one method cube.

  1. package com.javatpoint;  
  2. public interface Calculation {  
  3. int cube(int number);  
  4. }  

2) CalculationImpl.java

This class provides the implementation of Calculation interface.

  1. package com.javatpoint;  
  2. public class CalculationImpl implements Calculation{  
  3.     public int cube(int number) {  
  4.         return number*number*number;  
  5.     }  
  6. }  

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.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     
  8.     <servlet>  
  9.     <servlet-name>hessian</servlet-name>  
  10.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.     <load-on-startup>1</load-on-startup>  
  12. </servlet>  
  13.   
  14. <servlet-mapping>  
  15.     <servlet-name>hessian</servlet-name>  
  16.     <url-pattern>*.http</url-pattern>  
  17. </servlet-mapping>  
  18.   
  19. </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.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.       
  7. <bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>  
  8. <bean name="/Calculation.http"   
  9. class="org.springframework.remoting.caucho.HessianServiceExporter">  
  10.     <property name="service" ref="calculationBean"></property>  
  11.     <property name="serviceInterface" value="com.javatpoint.Calculation"></property>  
  12. </bean>  
  13.   
  14. </beans>  

5) client-beans.xml

In this xml file, we are defining bean for HessianProxyFactoryBean. You need to define two properties of this class.

  1. serviceUrl
  2. serviceInterface
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.       
  7. <bean id="calculationBean"   
  8. class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  9.     <property name="serviceUrl"   
  10.          value="http://localhost:8888/hessian/Calculation.http"></property>  
  11.     <property name="serviceInterface" value="com.javatpoint.Calculation"></property>  
  12. </bean>  
  13. </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.

  1. package com.javatpoint;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class Client {  
  6.  public static void main(String[] args){  
  7.   ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");  
  8.   Calculation calculation = (Calculation)context.getBean("calculationBean");  
  9.   System.out.println(calculation.cube(5));  
  10.  }  
  11. }  

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值