RmiのSpringでの使用のRmiServiceExport
3527 ワード
今日の面接で聞かれましたが、簡単に聞いただけで、使ったことがないので、短い時間でSpringのrmi文書を見に行きました.
1.org.springframe ewark.remoting.rmi.RmiProxyFactoryBen
使用しているのはrmi契約の実現です.
実現過程は,まず任務に服することである.
エクスポートクラスを定義します.
サービスを開始した時、コントロールはずっと運行状態にあります.終わったら、またrmiプロセスが実行されます.そのインターフェースプロトコルはrmi://hostname:1199/xxxxxxx
1.org.springframe ewark.remoting.rmi.RmiProxyFactoryBen
使用しているのはrmi契約の実現です.
実現過程は,まず任務に服することである.
エクスポートクラスを定義します.
public interface AccountService {
String getUsername();
}
public class AccountServiceImpl implements AccountService{
@Override
public String getUsername() {
return "RMI Test!";
}
}
rmi.xml<?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="accountService" class="example.AccountServiceImpl">
<!-- any additional properties, maybe a DAO? -->
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- rmi://hostname:1199/xxxxxxx xxxxxxx -->
<property name="serviceName" value="AccountService"/>
<!-- -->
<property name="service" ref="accountService"/>
<!-- , , , , coreInterface
, -->
<property name="serviceInterface" value="example.AccountService"/>
<!-- , 1099, -->
<property name="registryPort" value="1199"/>
</bean>
</beans>
サービスを開始public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("rmi.xml");
AccountService service = context.getBean("accountService", AccountService.class);
String userName = service.getUsername();
System.out.println(userName);
}
}
次のクライアントは以下の通りです.public interface AccountService {
String getUsername();
}
rmi.xml<?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="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- rmi -->
<property name="serviceUrl" value="rmi://localhost:1199/AccountService"/>
<!-- rmi -->
<property name="serviceInterface" value="example.AccountService"/>
</bean>
</beans>
起動プログラムpublic class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("rmi.xml");
AccountService service = context.getBean("accountService", AccountService.class);
String userName = service.getUsername();
System.out.println(userName);
}
}
これでサーバー側でRMI Testを得ることができます.サービスを開始した時、コントロールはずっと運行状態にあります.終わったら、またrmiプロセスが実行されます.そのインターフェースプロトコルはrmi://hostname:1199/xxxxxxx