Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JVM does not terminate when using RMI

I just read the Trail on RMI from sun at http://java.sun.com/docs/books/tutorial/rmi/implementing.html

When I run the example, the JVM does not terminate although main has finished. Is RMI spawning a Thread somewhere internally?

What is the behaviour of multiple Threads spawned in main, after main exits? Is it a clean way to let the Threads exit whenever they want or should you do a join on each Thread you spawn? I did not find any documentation on this question.

Thank you very much for your help!!

public class ComputeEngine implements Compute {

    public ComputeEngine() {
        super();
    }

    public <T> T executeTask(Task<T> t) {
        return t.execute();
    }


    public static void main(String[] args) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Compute";
            Compute engine = new ComputeEngine();
            Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind(name, stub);
            System.out.println("ComputeEngine bound");
        } catch (Exception e) {
            System.err.println("ComputeEngine exception:");
            e.printStackTrace();
        }
    }
}
like image 379
Flo Avatar asked Jul 05 '26 13:07

Flo


1 Answers

A thread is created for listening the socket and reply to requests to your object. One way to stop the JVM is to unbind the server:

Registry.unbind() 

and unexport the objects:

UnicastRemoteObject.unexportObject()). 
like image 165
Kartoch Avatar answered Jul 08 '26 03:07

Kartoch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!