How to use multiprocessing with Java - NOT multithreading - java

I've been looking for an api for simple java-based multiprocessing and couldn't find anything.
I have legacy code which I need to integrate into my java application. Since this legacy (native) code crashes sometimes, the whole jvm crashes with it. So what I want to do, is to run this code and its adapter in a different process (not thread!).
There's the ProcessBuilder in the newer java jdks, it lets you start a new process and gives you a In/Outputstream; so solving my problem is possible by hand. In order to do so, you have to find the path to your jvm and start it up with the for your process. Then you have to use the stream to communicate.
Is there something which takes over that job? Or do I really have to do it by hand?

AFAIK, people usually do this by hand.
The problem is that there's no truly portable way of doing it ... what with the difficulty (impossibility) of identifying where the currently running JVM was launched from, and the fact that JVM command-line options are vendor, version and (potentially) platform specific.
The simple solution is just to put the JVM path and options for launching the child JVM into some configuration file.

You can use the -Dprocess.name=$1 and let your main class take in some command line args. You can invoke by calling something like this:
java -cp $CLASSPATH $VM_ARGS $MAIN_CLASS
and your VM_ARGS can be defined something as
VM_ARGS=" -Dprocess.name=$1"

FWIW, I wrote a replacement class to take care of a lot of the I/O stream redirection nastiness, at david.tribble.com/src/java/tribble/util/RuntimeExec.java

You are looking for the technology called Java RMI (Remolt Method Invocation).
This allows one JVM to call a method in another JVM. This can be on the same machine or over a network.

Related

Is there any Java tool to communicate with an external CLI?

I need a way to communicate with an external tool which has a Command Line Interface from my Java application. Is there any handy tool/lib to make it less painful? Believe me, I've googled enough. Thank you!
You can use Runtime.exec() for executing external commands of the operating system. Be aware, though, of the gotchas of using it - make sure to read this article first.
If that's too simplistic for your needs, take a look at ProcessBuilder. It's available since Java 1.5, from the release notes:
The new ProcessBuilder class provides a more convenient way to invoke subprocesses than does Runtime.exec. In particular, ProcessBuilder makes it easy to start a subprocess with a modified process environment (that is, one based on the parent's process environment, but with a few changes).
I've ended up using the process handler (more specifically, the com.intellij.execution.process.OSProcessHandler) from Intellij IDEA platform API + writing my own small command-based framework where each command knowns how to execute itself (write) and parse the response from a process (read).

Bullet-proof groovy script embedding

I'm working on a server app that may be extended by user-supplied Groovy scripts. It's evident that I want to make sure these scripts run in a very tight sandbox where they cannot disrupt the core application code or consume too much resources to overload the server.
I have studied various possibilities and the final solution may be a combination of these:
Run the script within a very restricted security manager. The script is run within a no permission SecurityManager. Additional permissions have to be declared (like Android).
Launch a new JVM. Create a ScriptProcess wrapper around Runtime.exec and spawning a new JVM with a security manager, limited heap, etc. Because we launch a full-blown process, we might get more control on monitor bad behaving ones? The cost in resource would be dire though... An alternative would be to use Ant here, but would it be scalable?
Java Monitor API In Java 6 there is a package with monitoring capacity. We could monitor threads and maybe detect infinite loops and memory consumption. Anyone used this?
These are what I have in mind today. What would be the best way to make sure these scripts behave correctly and still keep a certain scalability and performance?
An additional possibility is using Groovy 1.8 compilation customizers on the GroovyShell that runs the embedded scripts. You can pre-import classes and methods, restrict use of the Groovy AST, and pre-apply an AST transformation, such as #ThreadInterrupt, #TimedInterrupt, or #ConditionalInterrupt. Details at:
http://www.jroller.com/melix/entry/customizing_groovy_compilation_process
You should have a look at the project groovy-sandbox from kohsuke.
Have also a look to his blog post here on this topic and what is solution is addressing: sandboxing, but performance drawback.
Also have a look at the java-sandbox project and the accompanying blog post http://blog.datenwerke.net/2013/06/sandboxing-groovy-with-java-sandbox.html.

How to replace the current Java process in Windows using JNA/JNI?

I want to replace the current Java process by a new one just like the Unix exec does. There has been already a similar question here, but I'd prefer a solution consuming as few memory as possible (the accepted answer suggest to use ClassLoaders, which could lead to memory leaks; a similar simple solution would be to use another process just to start the proper one). It can be surely done in a platform-dependent way using JNI, and I think I can do it for Unix (and a solution for Unix seem to already exist), but I know nearly nothing about the corresponding Windows API. What Windows function should I call? Has anybody done it already?
With Windows there are many subsystems to choose from that run on the base OS, so it helps to have some sense of what you are aiming for. For example, if you can use the C run-time library then you can just use the _exec() family of functions which are very similar to their unix cousins. Perhaps you can modify jniexec to work with windows using these.
The Win32 API doesn't include the concept of 'exec'. THe POSIX API does. The low-level WinNT API has the building blocks, but it's quite complex to use them, and, at least in the past, required recourse to undocumented functionality.

Save and load a (Java) program's state

Suppose you're short on time and you're looking for a program with certain features, and you find one, except it lacks one feature - it cannot save and load its state. Is it possible to achieve this on OS level, or with another program, that can take the whole thing, write it to a file, and then at a later time, load it back into memory? How?
Specifically for me, this is about a Java program, but any more information on this topic is welcome.
One (heavy and easy) solution could be to use VirtualPC and install the program on a virtual OS.
Check these library's:-
Brakes
ACTC(Asynchronous Transfer of Control Threading) [Article]
Apache JavaFlow
You want to do something like the Hibernate function of Windows right?
This will be extremely difficult to implement in Java as you will also have to write the state of the JavaVM. If you had open files when you closed the program end so on.
I think the best you can do is writing the objects you need to recover to disk using Java serialization.
Have a look at the CRIU project at https://criu.org/Main_Page
It offers exactly this possibility within linux systems. Docker integrates it and offers a docker checkpoint command, which if you run your program in a container, will allow you to do this on any OS.

How to know start and kill processes within Java code (or C or Python) on *nix

I need to write a process controller module on Linux that handles tasks, which are each made up of multiple executables. The input to the controller is an XML file that contains the path to each executable and list of command line parameters to be passed to each. I need to implement the following functionality:
Start each executable as an independent process
Be able to kill any of the child processes created, independent of the others
In order to do (2), I think I need to capture the pid when I create a process, to issue a system kill command. I tried to get access to pid in Java using ProcessBuilder but saw no easy way to do it.
All my other logic (putting info about the tasks in DB, etc) is done in Java so I'd like to stick with that, but if there are solutions you can suggest in C, C++, or Python I'd appreciate those, too.
For a Java solution, you should take a look at the apache commons exec library. They've done a lot of work to make it platform independant and they have a great tutorial.
In python, you can use the included subprocess library.
You really really need to look up "shell scripting" on google. Specially if your employer/instructor wants you to work on linux and deal with processes etc.
Maybe start here:
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
I'm not sure, but if you start executables from Java, you may start them in seperate threads, and then you can map them however you want - by name, by line number or something - and stop that enclosing thread regularly as java-thread, which doesn't seem to be an elegant solution (not closing files, etc.), but could work to some extend (as long as the linux-program doesn't start a process, which is freeing itself from its parent).
Specific commands for closing each process, send via stdin to the programs, might be another option. How to handle stdin and stdout and other pitfalls are mentioned here in some length:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?
Visible programs can even be controlled by java.awt.Robot (keyboard, mouse).
As a last idea I would consider using a new command "kill pidof program" which working on a name-basis, so you can't distinguish two instances of the same progam.
I don't know the apache-lib, mentioned by Steen, but there is normally very useful stuff, I would recommend to look there too - maybe in the first place.

Categories

Resources