English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
With HessianServiceExporter and HessianProxyFactoryBean class, we can implement the remote service provided by hessian.
Advantages of Hessian
Hessian works well on the entire firewall. Hessian is portable and can be integrated with other languages such as PHP and .Net.
You need to create the following files to create a simple hessian application:
Calculation.java CalculationImpl.java web.xml hessian-servlet.xml client-beans.xml Client.java
1, Calculation.java
This is a simple interface that contains a method for multidimensional datasets.
package com.w3codebox; public interface Calculation { int cube(int number); }
2, CalculationImpl.java
This class provides the implementation of the Calculation interface.
package com.w3codebox; public class CalculationImpl implements Calculation{ public int cube(int number) { return number*number*number; } }
3, web.xml
In this xml file, we define DispatcherServlet as the front controller. If any request follows the .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
Must be in WEB-Created in the INF folder. Its name must be servletname-servlet.xml. It provides CalculationImpl and HessianServiceExporter defines the bean.
<?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.w3codebox.CalculationImpl"></bean> <bean name="/Calculation.http" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="calculationBean"></property> <property name="serviceInterface" value="com.w3codebox.Calculation"></property> </bean> </beans>
5、client-beans.xml
In this xml file, we have HessianProxyFactoryBean defines the bean. 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.w3codebox.Calculation"></property> </bean> </beans>
In this example, the name of our project is hessian, which is used as the context root in the serviceURL.
6Client.java
This class retrieves an instance of Calculation and calls the multidimensional dataset method.
package com.w3codebox; 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)); } }
Start and deploy the project, here we assume the server is running on8888running on the port number. If the port number is different, please change the client-serviceURL in beans.xml.
Then, compile and run the Client.java file.