May 9, 2002
onlsun1, Solaris8システムでのJavaRMI-IIOPの実行
--- hello world実行#1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(http://www-online.kek.jp/~inoue/para-CAMAC/
Test/Hello-JavaRMI-IIOP.html)
高エネルギー加速器研究機構
素粒子原子核研究所
物理、オンライングループ
井上 栄二
(1). 現状確認
(A). S-4/20、onlsun1にjava 1.4.0 がインストールされている。
(2). ここでやるべきこと
Solaris8、onlsun1のマシン上でJavaRMI-IIOPでのhello world を実行する。
(3). コンパイル
(3-1). Javaのチェック
サーバ側
onlsun3[53]% java -version
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
onlsun3[54]%
クライアント側
onlsun1[347]% java -version
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
onlsun1[348]%
(3-2). サーバ側のコンパイル
onlsun3[104]% java -version
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
onlsun3[105]% which javac
/bin/javac
onlsun3[106]% which rmic
/bin/rmic
onlsun3[107]%
HelloInterface を実装するリモートオブジェクトの実装をコンパイルする。
onlsun3[108]% cat HelloImpl.java
//HelloImpl.java
import javax.rmi.PortableRemoteObject;
public class HelloImpl extends PortableRemoteObject implements HelloInterface {
public HelloImpl() throws java.rmi.RemoteException {
super(); // invoke rmi linking and remote object initialization
}
public void sayHello() throws java.rmi.RemoteException {
System.out.println( "It works! Hello World!!" );
}
}
onlsun3[109]%
onlsun3[109]% javac -d . -classpath . HelloImpl.java
HelloImplリモートオブジェクト実装のスタブおよびスケルトンを作成する。
onlsun3[110]% rmic -poa -iiop HelloImpl
onlsun3[111]%
上記のコマンドでよって、
_HelloInterface_Stub.class --- クライアントスタブ
_HelloImpl_Tie.class --- サーバスケルトン
の2つのファイルが作られた。
リモートインターフェース HelloInterface のインターフェース定義は以下の
とおり。
onlsun3[111]% cat HelloInterface.java
//HelloInterface.java
import java.rmi.Remote;
public interface HelloInterface extends java.rmi.Remote {
public void sayHello() throws java.rmi.RemoteException;
}
onlsun3[112]%
サーバのソースコード。
onlsun3[112]% cat HelloServer.java
//HelloServer.java
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;
import com.sun.corba.se.internal.POA.POAORB;
import org.omg.PortableServer.*;
import java.util.*;
import org.omg.CORBA.*;
import javax.rmi.CORBA.Stub;
import javax.rmi.CORBA.Util;
public class HelloServer{
public HelloServer(String[] args) {
try {
Properties p = System.getProperties();
// add runtime properties here
p.put("org.omg.CORBA.ORBClass",
"com.sun.corba.se.internal.POA.POAORB");
p.put("org.omg.CORBA.ORBSingletonClass",
"com.sun.corba.se.internal.corba.ORBSingleton");
ORB orb = ORB.init( args, p );
POA rootPOA = (POA)orb.resolve_initial_references("RootPOA");
// STEP 1: Create a POA with the appropriate policies
Policy[] tpolicy = new Policy[3];
tpolicy[0] = rootPOA.create_lifespan_policy(
LifespanPolicyValue.TRANSIENT );
tpolicy[1] = rootPOA.create_request_processing_policy(
RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY );
tpolicy[2] = rootPOA.create_servant_retention_policy(
ServantRetentionPolicyValue.RETAIN);
POA tPOA = rootPOA.create_POA("MyTransientPOA", null, tpolicy);
// STEP 2: Activate the POA Manager, otherwise all calls to the
// servant hang because, by default, POAManager will be in the
// HOLD state.
tPOA.the_POAManager().activate();
// STEP 3: Instantiate the Servant and activate the Tie, If the
// POA policy is USE_ACTIVE_OBJECT_MAP_ONLY
HelloImpl helloImpl = new HelloImpl();
_HelloImpl_Tie tie = (_HelloImpl_Tie)Util.getTie( helloImpl );
String helloId = "hello";
byte[] id = helloId.getBytes();
tPOA.activate_object_with_id( id, tie );
// STEP 4: Publish the object reference using the same object id
// used to activate the Tie object.
Context initialNamingContext = new InitialContext();
initialNamingContext.rebind("HelloService",
tPOA.create_reference_with_id(id,
tie._all_interfaces(tPOA,id)[0]) );
System.out.println("Hello Server: Ready...");
// STEP 5: Get ready to accept requests from the client
orb.run();
}
catch (Exception e){
System.out.println("Problem running HelloServer: " + e);
e.printStackTrace();
}
}
public static void main(String args[]){
new HelloServer( args );
}
}
onlsun3[113]%
クライアントのソースコード。
onlsun3[113]% cat HelloClient.java
//HelloClient.java
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import javax.rmi.*;
import java.util.Vector;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.naming.Context;
public class HelloClient{
public static void main(String args[]){
Context ic;
Object objref;
HelloInterface hi;
try {
ic = new InitialContext();
} catch (NamingException e) {
System.out.println("failed to obtain context" + e);
e.printStackTrace();
return;
}
// STEP 1: Get the Object reference from the Name Service
// using JNDI call.
try {
objref = ic.lookup("HelloService");
System.out.println("Client: Obtained a ref. to Hello server.");
} catch (NamingException e) {
System.out.println("failed to lookup object reference");
e.printStackTrace();
return;
}
// STEP 2: Narrow the object reference to the concrete type and
// invoke the method.
try {
hi = (HelloInterface) PortableRemoteObject.narrow(
objref, HelloInterface.class);
hi.sayHello();
} catch (ClassCastException e) {
System.out.println("narrow failed");
e.printStackTrace();
return;
} catch( Exception e ) {
System.err.println( "Exception " + e + "Caught" );
e.printStackTrace();
return;
}
}
}
onlsun3[114]%
サーバ、クライアントおよびリモートインターフェースコードをコンパイル
する。
onlsun3[114]% javac -d . -classpath . HelloInterface.java HelloServer.java HelloClient.java
onlsun3[115]%
(3-3). クライアント側のコンパイル
onlsun1[65]% java -version
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
onlsun1[66]% which javac
/bin/javac
onlsun1[67]% which rmic
/bin/rmic
onlsun1[68]%
上記(3-2)と同様のコンパイルをする。
onlsun1[69]% javac -d . -classpath . HelloImpl.java
onlsun1[70]% rmic -poa -iiop HelloImpl
onlsun1[71]% javac -d . -classpath . HelloInterface.java HelloServer.java HelloClient.java
onlsun1[72]%
(4). 実行
<<< サーバ側実行 >>>
(4-1). ネームサービスを起動する
この例では、Object Request Broker Daemon(orbd) を使用する。
onlsun3[116]% orbd -ORBInitialPort 1060 -ORBInitialHost onlsun3 &
[1] 551
onlsun3[117]%
(4-2). サーバを起動する
onlsun3[117]% java -classpath . -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://onlsun3:1060 HelloServer &
[2] 552
onlsun3[118]% Hello Server: Ready...
It works! Hello World!!
(4-3). クライアントアプリケーションを起動する
<<< クライアント側実行 >>>
onlsun1[73]% java -classpath . -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://onlsun3:1060 HelloClient &
[1] 1703
onlsun1[74]% Client: Obtained a ref. to Hello server.
サーバ側は、"It works! Hello World!!" を画面に表示して、正常に実行
できたことを示している。