JDK 6:有没有办法运行一个新的java进程来执行指定类的main方法
我正在尝试开发一个应用程序,在退出之前必须运行一个新的守护进程来执行类的主要方法。
我要求主应用程序退出后守护进程必须仍在执行中。
它是在 Oracle DB 上运行的 Java 存储过程,因此我无法使用 Runtime.exec,因为我无法从操作系统 Shell 中找到 java 类,因为它是在数据库结构而不是文件系统文件中定义的。
特别是,期望的行为应该是,在远程数据库会话期间,我应该能够
调用第一个运行守护进程的java方法并退出,使守护进程处于执行状态
,然后(让守护进程启动并进行会话控制,因为最后一次调用终止)因此
调用与守护进程通信的方法(最终在通信结束时退出)
这可能吗?
谢谢
更新
我的确切需求是创建一个大文本文件并将其加载(达到最佳性能)到数据库中,假设主机没有来自连接到 Oracle 的 Java JDK6 客户端应用程序的文件传输服务使用 JDBC-11G oci 驱动程序的 11gR1 DB。
我已经通过调用一个过程来开发一个可行的解决方案,该过程将作为输入给出的 LOB(大型数据库对象)存储到文件中,但这种方法使用了太多我想避免的中间结构。
因此,我考虑在数据库上创建一个 ServerSocket,首先调用,然后连接到它,并通过直接、快速的通信建立数据传输。
我遇到的问题是因为创建 ServerSocket 的 java 过程无法退出并让正在执行的线程/进程监听该 Socket 和客户端,以确保 ServerSocket 已创建,无法运行单独的线程来处理剩下的工作。
希望能说清楚
I'm trying to develop an application that just before quit has to run a new daemon process to execute the main method of a class.
I require that after the main application quits the daemon process must still be in execution.
It is a Java Stored Procedure running on Oracle DB so I can't use Runtime.exec because I can't locate the java class from the Operating System Shell because it's defined in database structures instead of file system files.
In particular the desired behavior should be that during a remote database session I should be able to
call the first java method that runs the daemon process and quits leaving the daemon process in execution state
and then (having the daemon process up and the session control, because the last call terminated) consequentially
call a method that communicates with the daemon process (that finally quits at the end of the communication)
Is this possible?
Thanks
Update
My exact need is to create and load (reaching the best performances) a big text file into the database supposing that the host doesn't have file transfer services from a Java JDK6 client application connecting to Oracle 11gR1 DB using JDBC-11G oci driver.
I already developed a working solution by calling a procedure that stores into a file the LOB(large database object) given as input, but such a method uses too many intermediate structures that I want to avoid.
So I thought about creating a ServerSocket on the DB with a first call and later connect to it and establish the data transfer with a direct and fast communication.
The problem I encountered comes out because the java procedure that creates the ServerSocket can't quit and leave an executing Thread/Process listening on that Socket and the client, to be sure that the ServerSocket has been created, can't run a separate Thread to handle the rest of the job.
Hope to be clear
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这是可能的,我会感到惊讶。实际上,您可以用无限数量的守护进程使数据库服务器计算机饱和。
如果这种事情是可能的,那么该技术很可能是 Oracle 特定的。
也许您可以使用数据库触发器或其他此类事件驱动的数据库功能来实现您想要的效果。
我建议解释一下您想要解决的确切问题,为什么需要守护进程?我的直觉是,尝试管理守护进程的生命将会变得非常复杂。您可能很需要处理诸如阻止启动两个实例、守护进程意外终止、需要维护时关闭守护进程等问题。这类事情可能会变得非常混乱。
例如,如果您想每小时运行一些 Java 代码,那么几乎可以肯定有更简单的方法可以实现该效果。操作系统和数据库往往有很好的方法来在所需的时间启动工作。因此,在需要时调用存储过程可能是您的环境中已经存在的功能。因此,您所需要做的就是将所需的代码放入存储过程中。无需您手工制定计划、启动和管理。这种方法的一个非常重要的优点是,您最终会使用环境中其他人已经理解的技术。
编写您正在考虑的代码非常有趣,但在商业环境中通常是浪费精力。
I'd be surprised if this was possible. In effect you'd be able to saturate the DB Server machine with an indefinite number of daemon processes.
If such a thing is possible the technique is likely to be Oracle-specific.
Perhaps you could achieve your desired effect using database triggers, or other such event driven Database capabilities.
I'd recommend explaining the exact problem you are trying to solve, why do you need a daemon? My instict is that trying to manage your daemon's life is going to get horribly complex. You may well need to deal with problems such as preventing two instances being launched, unexpected termination of the daemon, taking daemon down when maintenance is needed. This sort of stuff can get really messy.
If, for example, you want to run some Java code every hour then almost certanly there are simpler ways to achieve that effect. Operating systems and databases tend to have nice methods for initiating work at desired times. So having a stored procedure called when you need it is probably a capability already present in your environment. Hence all you need to do is put your desired code in the stored procedure. No need for you to hand craft the shceduling, initiation and management. One quite significant advantage of this approach is that you end up using a tehcnique that other folks in your environment already understand.
Writing the kind of code you're considering is very intersting and great fun, but in commercial environments is often a waste of effort.
为您的其他 Main 类创建另一个 jar,并在主应用程序中使用 Runtime.getRuntime().exec() 方法调用该 jar,该方法应该运行运行您的其他 Main 类的外部程序(另一个 JVM)。
Make another jar for your other Main class and within your main application call the jar using the Runtime.getRuntime().exec() method which should run an external program (another JVM) running your other Main class.
在 Java 中启动子进程的方式是
Runtime.exec()
(或更方便的包装器ProcessBuilder
)。如果这不起作用,那么您就是 SOL,除非您可以使用本机代码来实现等效功能(在此处提出另一个问题以了解如何在 C++ 级别启动子进程),但这至少是与使用标准方法一样容易出错。如果像 Oracle 这样的应用程序服务器允许您访问启动子进程或加载本机代码的功能,我会感到惊讶;两者都会造成巨大的危害,因此禁止使用不受信任的代码。查看您的编辑,您最好的方法是重新考虑如何解决您的实际问题,例如,通过使用 NIO 以更有效的方式管理套接字(并尝试不在磁盘上创建额外的文件;您只需必须输入额外复杂的代码来清理它们......)
The way you start subprocesses in Java is
Runtime.exec()
(or its more convenient wrapper,ProcessBuilder
). If that doesn't work, you're SOL unless you can use native code to implement equivalent functionality (ask another question here to learn how to start subprocesses at the C++ level) but that would be at least as error-prone as using the standard methods.I'd be startled if an application server like Oracle allowed you access to either the functionality of starting subprocesses or of loading native code; both can cause tremendous mischief so untrusted code is barred from them. Looking over your edit, your best approach is going to be to rethink how you tackle your real problem, e.g., by using NIO to manage the sockets in a more efficient fashion (and try to not create extra files on disk; you'll just have to put in extra elaborate code to clean them up…)