マルチキャストオブジェクトを使用したサービスの検出

この例では、ORBのオブジェクトマルチキャスト機能を使用してネットワーク上のサービスを検索します。

サーバはマルチキャストオブジェクトを作成し、JNDI/COSプロバイダを使用してCOS Namingにオブジェクト参照を発行します。クライアントはJNDIを使用してこのオブジェクト参照を取得し、サービス名およびユニキャストコールバックオブジェクトを渡してそれを呼び出します。マルチキャストオブジェクトサーバントは、該当するサービスのオブジェクトを取得し、サービスを登録するために、クライアントにコールバックを実行します。

1 Discoverable

検出できるすべてのサービスは、このインタフェースを実装します。discoverメソッドは、指定された名前のサービスを検出するために、クライアントにより呼び出されます。Discovererインタフェースは、このサービスが検出者に登録し返せるように、このメソッド上で渡されます。

この機能をより有効にするために、Discoverableインタフェースは特定の属性に基づいてサービスの選択をサポートすることもできます。

package discovery;
                                                                           
import java.rmi.Remote;
import java.rmi.RemoteException;
                                                                           
public interface Discoverable extends Remote
{
    void discover(String serviceName, Discoverer discoverer)
        throws RemoteException;
}

2 Discoverableの実装

Discoverableインタフェースの実装は、サービス名およびサービスリモートオブジェクトを使用して作成されます。discoverメソッドは、サービス名が要求されたサービス名と一致する場合、 Discovererでサービスを登録します。
package discovery;
                                                                           
import java.rmi.Remote;
import java.rmi.RemoteException;
                                                                           
public class DiscoverableImpl implements Discoverable
{
    private Remote _service;
    private String _serviceName;
                                                                           
    DiscoverableImpl(String serviceName, Remote service)
    {
    |   _service     = service;
    |   _serviceName = serviceName;
    }
                                                                           
    public void discover(String serviceName, Discoverer discoverer)
        throws RemoteException
    {
    |   if (_serviceName.equals(serviceName))
    |       discoverer.registerService(_service);
    }
}

3 Discoverer

Discovererインタフェースは、サービスを検出するプロジェクトによるコールバックとして使用されます。
package discovery;
                                                                           
import java.rmi.Remote;
import java.rmi.RemoteException;
                                                                           
public interface Discoverer extends Remote
{
    void registerService(Remote service) throws RemoteException;
}

4 Discovererの実装

このDiscovererの実装では、Helloサービスが登録されます。サービスのsayHelloメソッドを呼び出します。
package discovery;
                                                                           
import java.rmi.Remote;
import java.rmi.RemoteException;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
import helloWorld2.Hello;
                                                                           
public class DiscovererImpl extends PortableRemoteObject
    implements Discoverer
{
    public DiscovererImpl() throws RemoteException {}
                                                                           
    public void registerService(Remote service) throws RemoteException
    {
    |   Hello hello = (Hello) service;
    |   System.out.println(hello.sayHello());
    }
}

5 サーバ

サーバは最初にマルチキャスト Groupを作成し、Discoverableオブジェクトを作成して、グループに参加させます。joinGroupにより返されるオブジェクト参照は、JNDIを使用したCOSネーミングで発行されます。サーバは呼び出しを待機します。
package discovery;
                                                                           
import util.Util;
                                                                           
import java.util.Hashtable;
import java.net.InetAddress;
                                                                           
import javax.naming.Context;
import javax.naming.InitialContext;
                                                                           
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
                                                                           
import helloWorld2.Hello;
import helloWorld2.HelloImpl;
                                                                           
import com.sssw.jbroker.api.multicast.Group;
                                                                           
public class Server
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a Group
    |   |   Group group = Group.createGroup(orb, InetAddress.
    |   |       getByName("230.0.0.1"), "discover");
    |   |                                                                  
    |   |   // create the discoverable
    |   |   Discoverable discoverable = new DiscoverableImpl(
    |   |       "hello", new HelloImpl());
    |   |                                                                  
    |   |   // make the discoverable join the Group
    |   |   Object objref = group.joinGroup(discoverable);
    |   |                                                                  
    |   |   // create the JNDI Environment
    |   |   Hashtable env = new Hashtable();
    |   |   env.put("java.naming.corba.orb", orb);
    |   |                                                                  
    |   |   // get the initial naming context
    |   |   Context ctx = new InitialContext(env);
    |   |                                                                  
    |   |   // bind the discoverable objref using JNDI
    |   |   ctx.rebind("discoverable", objref);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
COSネームスペースで発行されるマルチキャストオブジェクト参照は、parseIORユーティリティ("parseIOR iiopname://localhost/discoverable")を使用して表示できます。
Type Id  = RMI:discovery.Discoverable:0000000000000000
Profiles = 1

    Multicast Object Protocol (MOP) Profile:

version    = 1.0
        IP address = 230.0.0.1
port       = 2506
obj key    =
            4A424B52 00050005 00000000 E6000001  JBKR........?...
            64697363 6F766572                    discover

components = 1

            TAG_JBROKER_MULTICAST_TTL :
                01                                   .
注記: 上記のIORはマルチキャストオブジェクトプロトコルプロファイルを含み、TTLは1です。クライアントがこのオブジェクト参照を呼び出すと、ORBはIIOPの代わりにMOPを使用してこのオブジェクトと対話します。

6 クライアント

クライアントはJNDIを使用してCOSネームスペースからマルチキャストオブジェクトを検索し、Discovererコールバックオブジェクトを渡してdiscoverメソッドを呼び出します。
package discovery;
                                                                           
import util.Util;
                                                                           
import java.util.Hashtable;
                                                                           
import javax.naming.Context;
import javax.naming.InitialContext;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
public class Client
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create the JNDI Environment
    |   |   Hashtable env = new Hashtable(5, 0.75f);
    |   |   env.put("java.naming.corba.orb", orb);
    |   |                                                                  
    |   |   // get the initial naming context
    |   |   Context ctx = new InitialContext(env);
    |   |                                                                  
    |   |   // lookup the Discoverable objref
    |   |   Discoverable discoverable = (Discoverable) ctx.
    |   |       lookup("discoverable");
    |   |                                                                  
    |   |   // discover hello object(s)
    |   |   discoverable.discover("hello", new DiscovererImpl());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
サーバは渡されたオブジェクトでコールバックして、Helloサービスを登録し、クライアントは画面上でHello World!を出力するsayHelloメソッドを呼び出します。
Copyright © 2000-2003, Novell, Inc.All rights reserved.