Any way to “reboot” the JVM?

后端 未结 8 1002
难免孤独
难免孤独 2020-12-05 17:46

Is there any way to reboot the JVM? As in don\'t actually exit, but close and reload all classes, and run main from the top?

相关标签:
8条回答
  • 2020-12-05 17:51

    It's easy in JavaX: You can use the standard functions nohupJavax() or restart().

    0 讨论(0)
  • 2020-12-05 18:01

    Your best bet is probably to run the java interpreter within a loop, and just exit. For example:

    #!/bin/sh
    while true
    do
        java MainClass
    done
    

    If you want the ability to reboot or shutdown entirely, you could test the exit status:

    #!/bin/sh
    STATUS=0
    while [ $STATUS -eq 0 ]
    do
        java MainClass
        STATUS=$?
    done
    

    Within the java program, you can use System.exit(0) to indicate that you want to "reboot," and System.exit(1) to indicate that you want to stop and stay stopped.

    0 讨论(0)
  • 2020-12-05 18:04

    IBM's JVM has a feature called "resettable" which allows you to effectively do what you are asking.

    http://publib.boulder.ibm.com/infocenter/cicsts/v3r1/index.jsp?topic=/com.ibm.cics.ts31.doc/dfhpj/topics/dfhpje9.htm

    Other than the IBM JVM, I don't think it is possible.

    0 讨论(0)
  • 2020-12-05 18:05

    AFAIK there is no such way.

    Notice that if there were a way to do that, it would highly depend on the current loaded code to properly release all held resources in order to provide a graceful restart (think about files, socket/tcp/http/database connections, threads, etc).

    Some applications, like Jboss AS, capture Ctrl+C on the console and provide a graceful shutdown, closing all resources, but this is application-specific code and not a JVM feature.

    0 讨论(0)
  • 2020-12-05 18:07

    If you're working in an application server, they typically come with built-in hot deployment mechanisms that'll reload all classes in your application (web app, enterprise app) when you redeploy it.

    Otherwise, you'll have to look into commercial solutions. Java Rebel (http://www.zeroturnaround.com/javarebel/) is one such option.

    0 讨论(0)
  • 2020-12-05 18:07

    I do something similar using JMX, I will 'unload' a module using JMX and then 'reload' it. Behind the scenes I am sure they are using a different class loader.

    0 讨论(0)
提交回复
热议问题