IIOP impl. export ok but toStub() failed, NoSuchObjectException!!!

My jre version is 1.4.2_05-b04. I'm using IIOP implementation with RMI.

I created my remote service, and then export the service with PortableRemoteObject.exportObject(), everything seemd ok. But when I try to get service stub with PortableRemoteObject.toStub(), I got a NoSuchObjectException indicating my service was not exported!!! But why exportObject() did throw any exception?

Here is my code:

package wuwei.rmi.server;

import java.io.IOException;

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.util.Enumeration;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

import wuwei.rmi.intf.EchoServiceIF;

publicclass Server

{

staticprivatefinal Properties sysProps =new Properties();

static

{

initialization();

}

privatestaticboolean initialization()

{

// 1. load properties from config file

try

{

sysProps.load( ClassLoader.getSystemResourceAsStream("config.ini") );

}

catch (IOException ioe)

{

ioe.printStackTrace();

returnfalse;

}

System.out.println("read properties from config file:");

for (Enumeration names = sysProps.propertyNames();

names.hasMoreElements(); )

{

String prop_name = (String) names.nextElement();

String prop_value = sysProps.getProperty(prop_name);

System.out.println(prop_name +"=" + prop_value);

}

System.out.println("-\n");

// 2. set codebase system property

System.setProperty(

"java.rmi.server.codebase",

sysProps.getProperty("java.rmi.server.codebase")

);

returntrue;

}

publicstaticvoid main(String[] args)

{

// 1. create service object

EchoServiceIF service =new EchoServiceImpl();

System.out.println("create service ok.");

// 2. export to be available for IIOP invocation

Remote _stub =null;

try

{

PortableRemoteObject.exportObject(service);

_stub = (Remote) PortableRemoteObject.toStub(service);

System.out.println("export service ok.");

}

catch (RemoteException re)

{

re.printStackTrace();

return;

}

// 3. bind stub to Registry

Context namingCtx =null;

try

{

namingCtx =new InitialContext(sysProps);

namingCtx.rebind("echo", _stub);

System.out.println("bind service to RMI Registry ok.");

}

catch (NamingException ne)

{

ne.printStackTrace();

}

finally

{

try{

if (namingCtx !=null){

namingCtx.close();

}

}catch (NamingException ne){

ne.printStackTrace();

}

}

}

}

Here is the config file config.ini:

java.rmi.server.codebase=http://192.168.1.66:8080/RMI_codebase/codebase.jar

java.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory

java.naming.provider.url=iiop://localhost:1050

Is there anyone who can help me? Thank you very much!

[5964 byte] By [ww_yolandaa] at [2007-11-26 13:46:25]
# 1

Dunno, but the stub is also returned by PortableRemoteObject.exportObject() so you don't need the toStub() call at all. Also you don't really need the stub at the server side, you can bind the server object itself to the naming service and it will sort all that out behind the scenes for you.

BTW does EchoServiceImpl extend UnicastRemoteObject by any chance? It mustn't if you want RMI/IIOP. In fact it would normally extend PortableRemoteObject, in which case you can get rid of the exportObject() call too.

ejpa at 2007-7-8 1:21:28 > top of Java-index,Core,Core APIs...
# 2

Dear ejp,

First, unlike UnicastRemoteObject, PortableRemoteObject.exportObject() doesn't return anything.

The EchoServiceImpl doesn't extend PortableRemoteObject, I export my service explicitly. The two approaches are the same.

Yes, I don't need to call toStub() method, exportObject(service) is ok. I have already tried this approach, but also failed :-(

ww_yolandaa at 2007-7-8 1:21:28 > top of Java-index,Core,Core APIs...
# 3

Hmm, OK, got me there.

Have you created the stub with rmic -iiop? If you didn't specify -iiop you will have created a JRMP stub. PortableRemoteObject.exportObject will then export the object as a JRMP object (don't ask me why) but PortableRemoteObject.toStub() doesn't know about that.

and btw does java.rmi.server.codebase really work with RMI/IIOP?

ejpa at 2007-7-8 1:21:28 > top of Java-index,Core,Core APIs...