Example of Remoting by Hessian
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
1) Calculation.java
It is the simple interface containing one method cube.
copy to clipboard
- package com.javatpoint;
- public interface Calculation {
- int cube(int number);
- }
package com.javatpoint; public interface Calculation { int cube(int number); }
2) CalculationImpl.java
This class provides the implementation of Calculation interface.
copy to clipboard
- package com.javatpoint;
- public class CalculationImpl implements Calculation{
- public int cube(int number) {
- return number*number*number;
- }
- }
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
- <?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">
hessian org.springframework.web.servlet.DispatcherServlet 1 hessian *.http
<?xml version=”1.0” encoding=”UTF-8”?>
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
- <?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 name=”/Calculation.http”
- 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.
- serviceUrl
serviceInterface
copy to clipboard
<?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">
<?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
- 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));
- }
- }
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)
还没有评论,来说两句吧...