File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Naming.Lookup and Rebind Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Naming.Lookup and Rebind" Watch "Naming.Lookup and Rebind" New topic
Author

Naming.Lookup and Rebind

Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
I am totally new to the RMI. If I have the RMI server running at PC A and the data client at PC B, what should I fill in for the parameters in Rebind() and Lookup(), or what information I should look for to fill in the blanks?
Keith Wheeles
Greenhorn

Joined: Feb 21, 2001
Posts: 5
Here is my rebind code:
String name = "dataserver";
Naming.rebind(name, remoteData);
remoteData is instance of my class implementing the RMI remote interface.
Here is my lookup code:
String name = "rmi://" + host + "/dataserver";
db = (RemoteDataInterface) Naming.lookup(name);
host is:
* @paramString hostDNS name or dotted quad string of remote machine
*running database server.
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
What about the codebase property? It seems your given example does not require it, but I assume we need it, don't we?
Rudy
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
One more thing. What about the port number?
Rudy
Ajit Kumar
Ranch Hand

Joined: Jan 27, 2001
Posts: 81
hi,
Are we supposed to consider the case of accessing the db server through the http as well? i mean using a web server?
I am in doubt.

Thanks
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Rudy, the port number if not supplied, will default to 1099 which is the default port on which the registry runs.
If your registry is running on a different port then instead of 'host' you must specify 'host :port'.
Note that we are concerned about the port on which the RMI registry is running not that on which the server is running.
Ajit, My specs do not require this. Do your specs require this? Otherwise how did you get this idea? My specs clearly state RMI or sockets. RMI uses the JRMP or IIOP protocol. With sockets you may specify your own protocol for communicating an 'action' and the 'parameters' of the action and/or use object serialization.

[This message has been edited by Rahul Rathore (edited March 11, 2001).]
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Rahul,
I see your point. Thx. Just checked it out also in the Java Network Programming by O'Reilly.
Rudy
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Ajit and Rahul,
I think the codebase property makes us think that we need a http server. I read a codebase document in Sub website, and it writes " ... Generally, the classes needed to execute remote method calls should be made accessible from a network resource, sych as an HTTP or FTP server."
Now, it is back to my previous query again. Though I have not tried testing the RMI as I am still in the coding stage, I doubt if the client is able to find the stub class to run without the codebase property information.
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Hello,
I am currently taking a Advanced Java Networking at University and I have completed a few RMI examples. You only need a web server if you will be dynamically loading your stubs to the clients so called pushing whereas this assignment I believe that you only need to physically place the stubs at the client and your on your way.
Hope this helps
Lisa
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Remember that the ftp http server is for distributed clients on a network web etc and that locally the RMI is itself a server and it will suffice the needs of the client but again you will have to phyiscally place the stubs at the clients path and or directory.
Hope this helps
Lisa
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
I see your point. I guess you are referring to applets running on a java thin client or web client.
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Basically yes Applets..
PS You are moving right along with this project keep up the steam.
Lisa
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
A codebase property is not specified in code. Rather you will specify it at the command-line (or your batch file). The codebase points to the location of yourServer_Stub class files. When a remote-reference is passed (either to the RMI registry or to the remote client) what happens is that the bytecodes of a serialized stub object are sent (to the RMI registry VM or to the client VM). The RMIregistry/client VM on getting the stub object, needs the stub class files to interpret that object. These _Stub.class files may either be placed in the client/RMI registry VM's classpath so that it is directly accessible or ALTERNATIVELY the _Stub.class files may be dynamically downloaded from the server. It is when the _Stub.class files need to be dynamically downloaded, that we need the codebase property to show their location.
Note that the codebase may point to a http/ftp resource, which would require an http/ftp server. But there is no need for it in our project. We may simply supply a file URL (instead of htp/ftp URL) as the codebase. The file URL will suffice for the registry VM since it always has to be located on the local host. It will also suffice for the client VM if it is running on the same host. But if the client is running on a different host, we can provide the _Stub.class files on the client host itself to avoid the need for dynamically downloading the _Stub.class files.
[This message has been edited by Rahul Rathore (edited March 11, 2001).]
Debajyoti Pathak
Ranch Hand

Joined: Dec 18, 2000
Posts: 39
I also put a lot of thought into this issue. Finally I decided to include a webserver within my application to dynamically download stub files at runtime. It's really not too much of work and sun does provide a very basic webserver just for this purpose. For further details check the rmi chapter in Sun's Java Tutorial. http://java.sun.com/docs/books/tutorial/rmi/compiling.html
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Rahul and Lisa,
If that is the case, I think we are allowed to manually copy the stub classes to the server side so that we do not need to worry about the dynamic downloading that requires a http or ftp server to host those stub classes. I would like to hear some comments from others.
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Rudy I have one person at work that completed this cert around 8 months ago he told me that he manually placed them and others as well that I have seen posts from the other egroups.
Hope this helps
Lisa
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
Thank you for your advice. I think the manual copying approach really saves me a lot of time in designing another class for this particular purpose. Now, I can work towards the final stage of my project: the concurrent access.
BTW, do your friend who passed SCJD 8 months ago manage to get a Java related job?
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Yes he is on his way to becoming the senior in Java programming although he has been programming for about 2 years in java but mind you for this promotion they recommened this cert.
Congrats on your progress I have been getting bogged down with school taking 6 classes and trying to move along with this cert I will be graduating this semester.
Finally Lisa,
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
Congratulation to your friend. I myself also bogged down with my office work. I just get switch to a new development team working on XML and JSP under IBM Websphere, a e-business web client application development tool. It's very tough for me to absorb all this new stuff all at once. Anyway, I hope my SCJD cert. together with my experience in related field will strengthen my skill set.
Sorry, this has nothing to do with the current thread.
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
I have a question Rudy, previously you were elaborating on your design regarding interface. Correct me if I am wrong
I believe I will go this approach::
public class DataClientLocal extends Data implements DataClient
{
//only my constructor for opening the database
}
public class DataClientRMI implements DataClient
{
//constructor for rmi connection and public methods in Data
}
in my DataClient Interface I will implement all the Data class
methods throwing both remote and Database Exception.
then in my client
I will have::
DataClient data =null;
if(localmode)
{
data = new DataClientLocal(arg1,...)
}
else
{
data = new DataClientRMI(arg1,...)
}
I could use the 'data' reference to access the database
I believe this would make it quite efficient in switching back and forth between modes....In your interface how did you approach this???
Also I beleive I could put this in a function
public switchMode
{
DataClient data =null;
if(localmode)
{
data = new DataClientLocal(arg1,...)
}
else
{
data = new DataClientRMI(arg1,...)
}
}
and call it within my actionEvenet for book seats button what do you think???
Thanks Lisa
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
PS you dont have to be sorry getting off the topic every once in Awhile we need to refresh our minds..
Lisa
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
I think your design is good. You can also give come comments on mine. Here is what my design is like taking your class and interface name for your reference.
Rudy
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
One more thing. For your DataClientLocal class, I feel it may be a bit redundant to have only one single constructor, and yet I think you have already made extensive changes in data class.
Rudy

Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Rudy your design is pretty good. I see your point on my
public class DataClientLocal extends Data implements DataClient{//only my constructor for opening the database}
In your DataClientLocal you mention that you dont subclass so you implement an interface to get access to the data member functions??
Perhaps this is more feasible as specs point out you can subclass or modify.
Your design looks great and clean very decoupled.
Lisa
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Rudy this is what I did I had to make my class abstract since i dont this class dont implement these DataClient methods define please look and give me feedback .
package suncertify.db;
import java.io.*;
import java.util.*;
public abstract class DataClientLocal implements DataClient
{

public DataClientLocal(String dbname){
try
{
Data dbInstance = new Data(dbname);
}
catch(Exception ex)
{
System.out.println("Database connection failed");
}
}
}
here below is my interface which I will use for both local and remote is this look ok???

package suncertify.db;
import java.io.*;
import java.util.*;
import java.rmi.*;
public interface DataClient extends Remote
{
public FieldInfo [] getFieldInfo()throws RemoteException;
public DataInfo getRecord(int recNum)throws DatabaseException, RemoteException;
public DataInfo find(String toMatch)throws DatabaseException, RemoteException;
public void add(String [] newData)throws DatabaseException, RemoteException;
public void modify(DataInfo newData)throws DatabaseException, RemoteException;
public void delete(DataInfo toDelete)throws DatabaseException, RemoteException;
public void close() throws RemoteException;
public void lock(int record)throws IOException, RemoteException;
public void unlock(int record)throws RemoteException;
public DataInfo[] criteriaFind(String criteria) throws DatabaseException, RemoteException;
public String[] getComboValues(int fieldNum) throws DatabaseException, RemoteException;
public int getMatchCount(String matchCount) throws DatabaseException,RemoteException;
}
thanks Lisa
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Hey rudy question I just recalled you cannot new an Instance of a abstract class is there around this to get rid of my abstarct class key and just have public when I compile DataClientLocal
without abstract keyword then I get a ton of errors staing class must be declared abstract it does not define all the methods in my interface//
Any Ideas perhaps making an adapter class for my interface??
Thanks Lisa
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
hello Rudy I would rather noy use an adapter class for my
interface DataClient here is my code again
package suncertify.db;
import java.io.*;
import java.util.*;
import java.rmi.*;
public interface DataClient extends Remote
{
public FieldInfo [] getFieldInfo()throws RemoteException;
public DataInfo getRecord(int recNum)throws DatabaseException, RemoteException;
public DataInfo find(String toMatch)throws DatabaseException, RemoteException;
public void add(String [] newData)throws DatabaseException, RemoteException;
public void modify(DataInfo newData)throws DatabaseException, RemoteException;
public void delete(DataInfo toDelete)throws DatabaseException, RemoteException;
public void close() throws RemoteException;
public void lock(int record)throws IOException, RemoteException;
public void unlock(int record)throws RemoteException;
public DataInfo[] criteriaFind(String criteria) throws DatabaseException, RemoteException;
public String[] getComboValues(int fieldNum) throws DatabaseException, RemoteException;
public int getMatchCount(String matchCount) throws DatabaseException,RemoteException;
}
then I have this code:::I would rather not extend Data I could I get around this???
package suncertify.db;
import java.io.*;
import java.util.*;
public class DataClientLocal implements DataClient
{

public DataClientLocal(String dbname){
try
{
Data dbInstance = new Data(dbname);
}
catch(Exception ex)
{
System.out.println("Database connection failed");
}
}
}
//next code::::: ataClientRMI
package suncertify.db;
import java.io.*;
import java.util.*;
import java.rmi.*;
public class DataClientRMI implements DataClient
{

public DataClientRMI(){
try
{
////////
}

catch(Exception ex)
{
System.out.println("Database connection failed");
}
}
}

then on the client:::::
public switchMode
{
DataClient data =null;
if(localmode)
{
data = new DataClientLocal(arg1,...)
}
else
{
data = new DataClientRMI(arg1,...)
}
}
Problem is when I try to compile this above classes::
DataClientRMI
DataClientLocal
//I get the errors these two classes must be declared abstract but if I do this I cant create an instance of these 2 classes.
Have you a solution Perhaps I am missing something here???
Thanks Lisa
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
You require those classes to be abstract because you do not implement all the methods specified in the interface. The Java compiler thinks that those classes are incomplete, and so it requires you to use the abstract keyword. So, the solution to it is to implement all the methods specified in your interface.
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Thanks Rudy I will do this do I have to declare just the methods and empty brackets???
Thanks Lisa
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
One more thing. I do not understand why you will have empty methods. For me, I do not have any empty methods. Basically, the interface is supposed to have all the same set of methods as in the data object except the constructor itself. Remember that in the spec, it mentions that you have to provide the same set of methods to your client for remote connection as the data class does.
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
yes rudy I am aliitle confused , Do I need to cut and paste all the methods from DataClass and Place them in my DataClientLOcal?
Thanks Lisa
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
In your DataClientLocal, say inside the modify() method, you have something like:

If you take a closer took, I think you actually do not need the dataClientLocal class. Instead, you directly call data.modify() in your program.
Rudy
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Rudy,
Thanks I am going to sleep now I am so tired cant think clearly.
Maybe I dont need this class I do directly call from my client FlyByNight class.
GoodNight Rudy Talk to you soon.
Lisa
Lisa Foster
Ranch Hand

Joined: Feb 28, 2001
Posts: 116
Ok rudy one more thing I will extend Data class instead of modifying it here is some code I am having problems with...
package suncertify.db;
import java.io.*;
import java.util.*;
public class DataClientLocal extends Data implements DataClient
{
public DataClientLocal(String dbName){

try
{
Data dbInstance = new Data("db.db");

}
catch(Exception ex)
{
System.out.println("Database connection failed sssss");
}
}

}

and in main I try to instantiate this I get database exception in made I hit the catch clause
here is main

public static void main( String args[] )
{

try
{
DataClientLocal data = new DataClientLocal("db.db");
JFrame fbn = new FlyByNight(data);
fbn.show();
}
catch(Exception ex)
{
System.out.println("Database connection failed");
}

I get the catch clause in main??? any Ideas
Thanks Lisa






}//end main

}//end class
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Lisa,
1) Please see the other post "Question for Rudy"
2) try printing out the exception message i.e. exp.getMessage() and see what it says.
Rudy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Naming.Lookup and Rebind
 
Similar Threads
NX:Question about RMI registry
Client/Server command line options
Java RMI Beginner
Server UI
Port Number while starting the server