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

Springリモート処理(Burlap例)

HoussianとBurlapはCouchoによって提供されています。

借助于 BurlapServiceExporter および BurlapProxyFactoryBean クラスは、Burlapが提供するリモートサービスを実現できます。Burlapの例はBurlapと同じですが、BurlapをBurlapに変更するだけで良いです。

Burlapを使用したリモート処理の例

シンプルなBurlapアプリケーションを作成するために、以下のファイルを作成する必要があります:

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

1、Calculation.java

これは、一つのメソッドを持つ多次元データセットを含むシンプルなインターフェースです。

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

2、CalculationImpl.java

このクラスはCalculationインターフェースの実装を提供します。

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

3、web.xml

このxmlファイルで、DispatcherServletをフロントコントローラーとして定義します。.httpエクステンションを持つリクエストがあれば、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

它必须在WEB-WEB内でなければなりません-servlet.xml。INFフォルダ内に作成されます。名前はservletnameでなければなりません CalculationImpl および BurlapServiceExporter 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

このxmlファイルでは、 BurlapProxyFactoryBean beanを定義しています。このクラスの2つの属性を定義する必要があります。

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>

このサンプルでは、プロジェクト名は麻布であり、serviceURLのコンテキストルートとして使用されます。


6、Client.java

このクラスはCalculationのインスタンスを取得し、メッシュデータセットメソッドを呼び出します。

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

この例をどうやって実行するか

プロジェクトを起動し、デプロイします。ここでは、サーバーが8888ポート番号で実行します。ポート番号が異なる場合は、clientを変更してください。-beans.xml内のserviceURL。

次に、Client.java ファイルをコンパイルして実行します。