Pedro Nunes
Pedro Nunes

Reputation: 412

Stopping RMI Server (Java)

I created a sucessful RMI server, thank god for that :)

It works perfectly.. I have a JForm and it gets activated clicking on a button.

Altough i wanted to create too a button to deactivate it, but i am having a problems to unexport it.

Ok then and this is the way i was trying to exporting and terminating the RMI Server

private void btStopServerActionPerformed(java.awt.event.ActionEvent evt) {
 try {
            // Nome do objecto
            String objectname = txtObjectName.getText();
            // obtem o número da porta
            int port = Integer.parseInt(txtServerPort.getText());

            RemoteMessageObject remote = new RemoteMessageObject();
            Registry registry = LocateRegistry.getRegistry(port);
            UnicastRemoteObject.unexportObject(LocateRegistry.getRegistry(port), true);
            registry.unbind(objectname);


            System.out.println("Server offline");
        } catch (IOException ex) {
            Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }       

I get this exception:

java.rmi.NoSuchObjectException: object not exported

at the line:

UnicastRemoteObject.unexportObject(LocateRegistry.getRegistry(port), true);

What am i doing wrong here?

Solved................

I discovered it before :P Anywway thanks @EJP for the correct answer.

So the solution for me was to create the Register when the class starts as a public variable, so it could be use inside both click events (Start server and stop server).

I removed too a lot of nonsense stuff that was not necessary for disabling the RMI server just like @EJP sayed.

Its now working this way:

private void btStopServerActionPerformed(java.awt.event.ActionEvent evt) {                                             

    try {
        // Nome do objecto
        String objectname = txtObjectName.getText();
        // obtem o número da porta
        int port = Integer.parseInt(txtServerPort.getText());

        Registry registry = LocateRegistry.getRegistry(port);
        UnicastRemoteObject.unexportObject(this.registry, true);
        registry.unbind(objectname);

        System.out.println("Server offline");
    } catch (IOException ex) {
        GuiUtils.addText(txtLog, "Erro", "Servidor desligado");
        btStopServer.setEnabled(false);
        btStartServer.setEnabled(true);
    } catch (NotBoundException ex) {
        Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}      

Upvotes: 0

Views: 2034

Answers (1)

user207421
user207421

Reputation: 310985

You need to unexport the object returned by LocateRegistry.createRegistry(). The object returned by getRegistry() isn't the actual Registry object, it is a stub, and you can't unexport those.

But unexporting the Registry and then calling unbind() doesn't make sense. Doing it the other way round makes a little sense, but not much.

And you also have to unexport your own remote object.

And creating a new remote object in a method that is trying to unexport the existing one doesn't make sense either.

Upvotes: 1

Related Questions