java rmi server ExportException internal error ObjID already use

Problem:

Sep 21, 2013 9:12:09 AM suncertify.main.URLyBirdMain main
INFO: Executing in server mode.
Sep 21, 2013 9:12:09 AM suncertify.server.RMIFactoryServerImpl
INFO: rmi://127.0.0.1:1099/URLyBirdRMIServer
java.rmi.server.ExportException: internal error: ObjID already in use
at sun.rmi.transport.ObjectTable.putTarget(ObjectTable.java:169)
at sun.rmi.transport.Transport.exportObject(Transport.java:74)
at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:229)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:393)
at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:129)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:188)
at sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:100)
at sun.rmi.registry.RegistryImpl.(RegistryImpl.java:86)
at java.rmi.registry.LocateRegistry.createRegistry(LocateRegistry.java:186)
at suncertify.server.RMIFactoryServerImpl.(RMIFactoryServerImpl.java:38)
at suncertify.server.RMIFactoryServerImpl.(RMIFactoryServerImpl.java:43)
at suncertify.main.ServerModeMain.(ServerModeMain.java:54)
at suncertify.main.URLyBirdMain.main(URLyBirdMain.java:41)

and the class:


public class RMIFactoryServerImpl extends UnicastRemoteObject implements RMIFactoryServer  {

    private static final long serialVersionUID = 2364209308189771862L;
    private static Logger logger = Logger.getLogger("ServerMain");

public RMIFactoryServerImpl() throws NumberFormatException, RemoteException, FileNotFoundException, IOException{
//TODO implement GUI for the initial parameters
//TODO, is the bellow assumption right?
//The server will run with the default parameters  
        //This starts up the registry at port 1099 on the local machine.
        java.rmi.registry.LocateRegistry.createRegistry(Integer.parseInt( Utils.readPropertiesFile("serverPort")));
        logger.fine("RMI registry created on port :" + Utils.readPropertiesFile("serverPort"));
        String name = "rmi://" + Utils.readPropertiesFile("serverAdress").trim() + ":" + Utils.readPropertiesFile("serverPort").trim() + "/URLyBirdRMIServer";
        logger.info(name);
Naming.rebind (name, new RMIFactoryServerImpl());


Solution:

While trying to implement the Factory design pattern  for RMI the following line is generating the problem:

             Naming.rebind (name, new RMIFactoryServerImpl());

The error was caused by trying to rebind with a new instance of the class were it was running. It was solved by passing the class itself rather than a new instance:

       Naming.rebind (name, this);

Comments

Popular posts from this blog

Hibernate - HibernateException: createQuery is not valid without active transaction