S2RMIを使ってみる
昨日のS2Axis2はとりあえず動いたものの"Hello"という文字列を戻すだけのメソッドが12ミリ秒/回もかかるのでどこかおかしいとは思うのですが、そっちの調査はひとまず置いておいて今日はS2RMIを使ってみたいと思います。
SMART deployを使わない場合
SMART deployを使わない方は比較的簡単にできました。
Mavenでプロジェクト作成
archetypeはデフォルト(maven-archetype-quickstart)。
mvn archetype:create -DgroupId=hatena.rmi -DartifactId=S2RMI-Sample
pom.xmlに以下を追加
org.seasar.rmi s2rmi-server 1.1.1 runtime org.seasar.rmi s2rmi 1.1.1 runtime log4j log4j 1.2.13 runtime
eclipseにインポート
インポートする前に
の二つのフォルダを作成しておく。
mvn eclipse:eclipse
プロジェクトのルートにて上記のコマンドを実行してからeclipseにインポート。
サービスの作成
以下通りにインターフェイスと実装を作成。
package hatena.rmi; public interface Hello { String hello(); }
package hatena.rmi; public class HelloImpl implements Hello { public String hello() { return "Hello World"; } }
server.diconの作成
以下の内容でserver.diconを作成し、/S2RMI-Sample/src/main/resourcesに配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd"> <components xmlns:xi="http://www.w3.org/2001/XInclude"> <component name="helloService" class="hatena.rmi.HelloImpl"/> <component name="componentInvoker" class="org.seasar.extension.component.impl.ComponentInvokerImpl"/> <component name="RMIAdapptor" class="org.seasar.remoting.rmi.adaptor.impl.RMIAdaptorImpl"> <property name="invokerName">"componentInvoker"</property> </component> <component class="org.seasar.remoting.rmi.deployer.impl.RMIAdaptorDeployerImpl"> <property name="registryPort">1108</property> <property name="servicePort">1109</property> <initMethod name="deploy"/> <destroyMethod name="undeploy"/> </component> <component class="org.seasar.remoting.rmi.filter.impl.RMIExternalContextFilter"/> </components>
サーバーの実行
以下のコマンドを実行すると/S2RMI-Sample/src/main/webapp/WEB-INF以下に必要なリソースが配置されます。
mvn war:inplace
/S2RMI-Sample/src/main/webapp/WEB-INFにて以下のコマンドを実行するとサーバーが起動。
java -jar lib/s2rmi-server-1.1.1.jar --classpath classes --dicon server.dicon
クライアントの作成
クライアントはテストとして作成してみます。
package hatena.rmi; import org.seasar.framework.container.S2Container; import org.seasar.framework.container.factory.S2ContainerFactory; import junit.framework.TestCase; public class ClientTest extends TestCase { public void testHello() { S2Container container = S2ContainerFactory.create("client.dicon"); Hello hello = (Hello) container.getComponent(Hello.class); assertEquals("Hello World", hello.hello()); } }
client.diconの作成
以下の内容でclient.diconを作成し、/S2RMI-Sample/src/test/resourcesに配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd"> <components> <component name="helloService" class="hatena.rmi.Hello"> <aspect>remotingInterceptor</aspect> </component> <component name="remotingInterceptor" class="org.seasar.remoting.common.interceptor.RemotingInterceptor"/> <component class="org.seasar.remoting.rmi.connector.impl.RMIConnectorImpl"> <property name="baseURLAsString">"rmi://localhost:1108/"</property> <initMethod name="lookup"/> </component> </components>
hello.hello()を1000回実行してもすんなりいくのでパフォーマンスもよさげです。