English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring traitement à distance (par exemple avec Hessian)

Avec HessianServiceExporter et HessianProxyFactoryBean Classe, nous pouvons implémenter le service distant fourni par Hessian.

Avantages de Hessian

Hessian fonctionne bien sur tout le pare-feu. Hessian est portable et peut être intégré avec d'autres langages tels que PHP et .Net.

Exemple de traitement distant Hessian

Vous devez créer les fichiers suivants pour créer une application hessian simple:

Calculation.java CalculationImpl.java web.xml hessian-servlet.xml client-beans.xml Client.java

1、Calculation.java

C'est une interface simple qui contient une méthode pour un ensemble de données multidimensionnelles.

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

2、CalculationImpl.java

Cette classe fournit l'implémentation de l'interface Calculation.

package com.w3codebox;
public class CalculationImpl implements Calculation{
    public int cube(int number) {
        return number*number*number;
    }
}

3、web.xml

Dans ce fichier xml, nous définissons DispatcherServlet comme contrôleur frontal. Si toute requête suivie de.l'extension .http, elle sera redirigée vers 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>

4et hessian-servlet.xml

Doit être dans WEB-Créé dans le dossier INF. Son nom doit être servletname-servlet.xml. Il de CalculationImpl et HessianServiceExporter Définit le 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

Dans ce fichier xml, nous avons pour HessianProxyFactoryBean Définit le bean. Vous devez définir deux propriétés de cette classe.

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>

Dans cet exemple, le nom de notre projet est hessian, qui est utilisé comme racine de contexte dans serviceURL.


6、Client.java

Cette classe obtient une instance de Calculation et appelle la méthode du jeu de données multidimensionnel.

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));
 }
}

Comment exécuter cet exemple

Démarrer et déployer le projet, ici nous supposons que le serveur est8888sur le port du serveur. Si le port est différent, veuillez modifier le client-serviceURL dans beans.xml.

Ensuite, compilez et exécutez le fichier Client.java.