Example of Remoting by Hessian

迈不过友情╰ 2022-08-12 02:59 98阅读 0赞



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.

copy to clipboard

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

package com.javatpoint; public interface Calculation { int cube(int number); }


2) CalculationImpl.java

This class provides the implementation of Calculation interface.

copy to clipboard

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

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.

copy to clipboard

  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. hessian
  8. org.springframework.web.servlet.DispatcherServlet
  9. 1
  10. hessian
  11. *.http

<?xml version=”1.0” encoding=”UTF-8”?> hessian org.springframework.web.servlet.DispatcherServlet 1 hessian *.http


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.

copy to clipboard

  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. <bean name=”/Calculation.http”
  7. class=”org.springframework.remoting.caucho.HessianServiceExporter”>

<?xml version=”1.0” encoding=”UTF-8”?>


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

    copy to clipboard

  3. <?xml version=”1.0” encoding=”UTF-8”?>

  4. <beans xmlns=”http://www.springframework.org/schema/beans“
  5. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
  6. xsi:schemaLocation=”http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd">
  8. <bean id=”calculationBean”
  9. class=”org.springframework.remoting.caucho.HessianProxyFactoryBean”>
  10. <property name=”serviceUrl”
  11. value=”http://localhost:8888/hessian/Calculation.http">

<?xml version=”1.0” encoding=”UTF-8”?>

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.

copy to clipboard

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

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.

download this example (developed using Myeclipse IDE)

发表评论

表情:
评论列表 (有 0 条评论,98人围观)

还没有评论,来说两句吧...

相关阅读