是否可以从不同的JVM中调用java应用程序中的方法? [英] Is it possible to call method within a java application from a different JVM?

查看:137
本文介绍了是否可以从不同的JVM中调用java应用程序中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次使用apache守护进程为windows开发java服务时,我使用了我喜欢的 JVM 模式。您指定您的类并启动\stop(静态)方法。但是对于Linux,Jsvc看起来并没有相同的选择。我真的想知道为什么?!

When I first developed a java service for windows using apache daemon, I used the JVM mode which I liked a lot. You specify your class and start\stop (static) methods. But with Linux, Jsvc doesn't look like it has the same option. I would really like to know why ?!

无论如何我要使用Linux的init系统,我试图找到一种类似的方法来实现相同的行为无论如何要启动应用程序,但要停止它,我将不得不在类中调用一个方法。

Anyway If I'm going to use Linux's init system, I'm trying to find a similar way to accomplish the same behavior which is to start the app in anyway but to stop it, I'll have to call a method in a class.

我的问题是,在jar启动后,我如何使用jvm库或其他任何东西来调用我的应用程序中的方法(它将尝试停止我的应用程序优雅)。

My question is, after the jar is started, how can I use the jvm libraries or anything else, to call a method in my application (which will attempt to stop my application gracefully).

另一个问题,如果应用程序启动并且该应用程序具有静态方法,如果我使用 java 命令行来运行 main 如果是应用程序类,则为一个方法, main 方法,即 static 将调用另一个静态方法在我想要发出终止信号的类中的方法,是否会在相同的 JVM中调用

Another side question, if an application is started and that application has static methods, If I use the java command line to run a main method in one if that's application class, and the main method, which is static would call another static method in the class in which I would like to signal the termination signal, would that call by in the same JVM ?

推荐答案

为什么不添加 ShutdownHook 到你的申请?

Why not rather add a ShutdownHook to your application?


关闭钩子只是一个初始化但未启动的线程。当
虚拟机开始其关闭序列时,它将以某些未指定的顺序启动所有
注册的关闭挂钩,并让它们同时运行
。当所有钩子都完成后,如果启用了终止退出,它将运行所有
未经过调整的终结器。
最后,虚拟机将停止运行。请注意,守护程序线程将在关闭序列期间继续运行
,如果通过调用exit方法启动关闭,则非守护程序
线程也将运行。

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method.

这将允许您的jar在关闭之前正常终止:

This will allow your jar to terminate gracefully before being shutdown:

public class ShutdownHookDemo {
    public void start() {
        System.out.println("Demo");
        ShutdownHook shutdownHook = new ShutdownHook();
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    public static void main(String[] args) {
        ShutdownHookDemo demo = new ShutdownHookDemo();
        demo.start();
        try {
            System.in.read();
        }
        catch(Exception e) {
        }
    }
}

class ShutdownHook extends Thread {
    public void run() {
        System.out.println("Shutting down");
        //terminate all other stuff for the application before it exits
    }

}

重要的是要注意

关闭钩子在以下情况下运行:


  • 程序正常存在。例如,调用System.exit(),或者退出最后一个非守护程序线程。

  • 虚拟机终止。例如CTRL-C。这对应于Unix系统上的kill -SIGTERM pid或

  • kill -15 pid。

关闭挂钩在以下情况下不会运行:


  • 虚拟机中止

  • SIGKILL信号被发送到Unix系统上的虚拟机进程。例如kill -SIGKILL pid或kill -9 pid

  • TerminateProcess调用被发送到Windows系统上的进程。

或者如果你必须使用它来调用类中的方法:

Alternatively if you must you can use this to call a method in a class:

public class ReflectionDemo {

  public void print(String str, int value) {
    System.out.println(str);
    System.out.println(value);
  }

  public static int getNumber() { return 42; }

  public static void main(String[] args) throws Exception {
    Class<?> clazz = ReflectionDemo.class;//class name goes here
    // static call
    Method getNumber = clazz.getMethod("getNumber");
    int i = (Integer) getNumber.invoke(null /* static */);
    // instance call
    Constructor<?> ctor = clazz.getConstructor();
    Object instance = ctor.newInstance();
    Method print = clazz.getMethod("print", String.class, Integer.TYPE);
    print.invoke(instance, "Hello, World!", i);
  }
}

并动态加载一个类:

ClassLoader loader = URLClassLoader.newInstance(
    new URL[] { yourURL },
    getClass().getClassLoader()
);
Class<?> clazz = Class.forName("mypackage.MyClass", true, loader);
Class<? extends Runnable> runClass = clazz.asSubclass(Runnable.class);

参考文献:

  • http://onjava.com/onjava/2003/03/26/shutdownhook.html
  • http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
  • How to shutdown java application correctly from C# one
  • How does one access a method from an external jar at runtime?
  • How to load a jar file at runtime

这篇关于是否可以从不同的JVM中调用java应用程序中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆