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

Traitement distant Spring (par exemple via Burlap)

Houssian et Burlap sont fournis par Coucho.

Grâce à BurlapServiceExporter et BurlapProxyFactoryBean Classe, nous pouvons réaliser des services distants fournis par Burlap. L'exemple Burlap est le même que Burlap, vous n'avez qu'à remplacer Burlap par Burlap.

Exemple de traitement distant via Burlap

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

Calculation.java CalculationImpl.java web.xml burlap-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 de frontend. 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>burlap</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>burlap</servlet-name>
    <url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>

4、burlap-servlet.xml

Il doit être dans WEB-dans le dossier INF. Son nom doit être servletname-servlet.xml. Il crée pour CalculationImpl et BurlapServiceExporter défini 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.BurlapServiceExporter">
    <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 BurlapProxyFactoryBean défini le bean. Vous devez définir les 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.BurlapProxyFactoryBean">
    <property name="serviceUrl" 
         value="http://localhost:8888/burlap/Calculation.http"></property>
    <property name="serviceInterface" value="com.w3codebox.Calculation"></property>
</bean>
</beans>

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


6、Client.java

Cette classe obtient l'instance de Calculation et appelle la méthode de jeu de données multidimensionnelles.

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

Comment exécuter cet exemple

Démarrage et déploiement du projet, ici nous supposons que le serveur est8888sur le port. Si le port est différent, modifiez client-serviceURL dans beans.xml.

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