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

SpringとRMIの統合

Spring RMIを使用すると、RMIインフラストラクチャを通じてサービスを公開できます。

Springはorg.springframework.remoting.rmiを使用して。 RmiProxyFactoryBean およびorg.springframework.remoting.rmi。 RmiServiceExporter クラス。

RmiServiceExporter

RMIオブジェクトにエクスポートサービスを提供します。通常のRMIまたはRmiProxyFactoryBeanを通じてこのサービスにアクセスできます。

RmiProxyFactoryBean

これはRmiエージェントのファクトリーベーンです。Beanリファレンスとして使用できるエージェントサービスを公開しています。

SpringとRMI統合の例

SpringアプリケーションとRMIを統合するシンプルな手順を見てみましょう:

Calculation.java CalculationImpl.java applicationContext.xml client-beans.xml Host.java Client.java


必要なJarファイル

このサンプルを実行するには、以下のJarファイルをロードする必要があります:

Spring Core jarファイル Spring Remoting jarファイル Spring AOP jarファイル

Springのすべてのjarファイルをダウンロードしてください、core、web、aop、mvc、j2ee、remoting、oxm、jdbc、ormなどです。


1Calculation.java

これは一つのメソッドを持つシンプルなインターフェースです。

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

2CalculationImpl.java

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

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

3applicationContext.xml

このxmlファイルでは、CalculationImplクラスと RmiServiceExporter このクラスはbeanを定義しています。RmiServiceExporterクラスの以下の属性に値を提供する必要があります。

サービス serviceInterface serviceName replaceExistingBinding registryPort

<?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 class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="service" ref="calculationBean"></property>
    <property name="serviceInterface" value="com.w3codebox.Calculation"></property>
    <property name="serviceName" value="CalculationService"></property>
    <property name="replaceExistingBinding" value="true"></property>
    <property name="registryPort" value="1099></property>
</bean>
</beans>

4、client-beans.xml

このxmlファイルでは、 RmiProxyFactoryBean 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.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.w3codebox.Calculation"></property>
</bean>
</beans>

5、Host.java

それはApplicationContextのインスタンスを取得しています。ただし、このサンプルを実行するにはまずこのクラスを実行する必要があります。

package com.w3codebox;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Host{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("リクエストを待っています");
}
}

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