Cleanly stop a running JRuby scriptlet - java

I am running a Ruby script from Java using JRuby like so:
public class ScriptletRunnable implements Runnable {
#Override
public void run() {
ScriptingContainer scriptingContainer = new ScriptingContainer();
scriptingContainer.runScriptlet(/*My script*/); // this is a blocking call, never returns
}
}
I pass this runnable to a new thread and start the thread. When I want to stop the script from running, I can't seem to find any way to tell the ScriptingContainer to stop executing the script. Even if I make it a member and call terminate() on it, the script just doesn't stop. If you run the script from the command line you can cleanly shut it down with Ctrl+C. There must be a way to achieve this with JRuby.

Related

How to tell when an instance of Command Prompt is closed, in java?

I've had a bit of an issue, and I'm pretty new at Java. For context, I'm making a GUI in Javafx, with a Batch backend. The GUI only has to call the Batch script(core.bat) once, and know when it has ended after it has been called. Currently, I call the Batch script using the following code:
Runtime runtime = Runtime.getRuntime();
try {
Process p1 = runtime.exec("cmd /c start core.bat");
} catch(IOException ioException) {}
The issue comes into play when I need to know when the Batch script has ended. I would give examples of what I've tried so far, but I've tried so many things over the past three hours and overwritten my code so many times that I just don't know anymore.
Any solution will do. I just need some way to determine when the Batch script has ended, after which it will close itself. Due to this, knowing when the Batch script ends is not the only possible method.
Knowing when the instance of command prompt running the script ends is also an option.
Any solutions are helpful, thank you.
Take a look at Process#waitFor. It waits until the process has finished.
You might need to start a new Thread if you don't want to wait for it but only get notified/execute code when the program is finished(blocking seems not like a good idea in JavaFX):
Runtime runtime = Runtime.getRuntime();
try {
Process p1 = runtime.exec("core.bat");
Thread t=new Thread(()->{
try{
p1.waitFor();
//your code
}catch(InterruptedException e){
Thread.currentThread.interrupt();//not actually needed, but I think it is a good practise and...SonarLint :)
}
});
t.setDaemon(true);
t.start();
} catch(IOException ioException) {}
Also, you executed the cmd command start, that starts a new process that you cannot control that easily. Just execute core.bat or cmd /c core.bat
setDaemon(true); marks your Thread as a daemon Thread, that does not affect the end of the Program(ends if all Threads that are no daemon Threads finished).

Create a stoppable java program (daemon)

Starting a java command line application is easy, you only have to write the following line in a command prompt (in the directory where the app is located).
java myApp.java
However, to stop the application in the right way, so that you ensure that all unmanaged resources are cleaned (and anything that must be done before stop, will be done) requires custom code.
The app will run in a debian system with no GUI as a daemon (it will run in background).
Here below I write the skeleton of the code.
public class MainClass {
public static void main(String[] args) throws Exception {
boolean stop = false;
while(!stop){
doSomething();
}
stop();
}
private static void doSomething(){
//Main code of app here
}
private static void stop(){
beforeStop();
System.exit(0);
}
private static void beforeStop(){
clean();
//Code to do anything you have to do before stop
}
private static void clean(){
//Code to clean unmanaged resources
}
}
As you can see, the app will run 24/24 and won't stop until you don't stop it.
Killing the process (as some people suggest) is not a good solution, because (for example) some unmanaged resources might not be cleaned properly.
I need a code which makes possible to alter the boolean variable "stop" from OUTSIDE.
The best solution is the one which makes possible to stop the app with a command similar to the start command, see pseudo code below (executed in a command prompt, in the directory where myApp.java is located).
myApp.java stop=true
But if it's not possible, the second option would be to have an other java command line app, which stops myApp.java, so that I could stop myApp.java with the following code
java stopMyApp.java
Is someone able to suggest a useful code example?
You can use a text file with one word. Your program reads it every x seconds and depending on that word it will autostop.
You can change the file text content by hand or with another program you can run whenever you want.
Even better you can use WatchService API (Java 7) or VFS API from Apache Commons to be notified when the file changes.
If you use a DB you can use it instead of a plain file.

How to Debug Thread.start() in java

I am debugging a Java Application in NetBeans IDE 8.0.2.
When a Thread.start() method is called I am not able to reach the run() method of that thread (though I put the breakpoints in that method).
However, sometimes it is hitting the method but sometimes not.
How can I reach the run() method while debugging?
public class JavaApplication7 {
public static void main(String[] args) {
Mailthread1 mt1 = new Mailthread1();
Thread pt = new Thread(mt1);
pt.setName("Mailthread");
pt.start();
}
}
And the Thread class is :
class Mailthread1 implements Runnable {
public void run() {
System.out.println("Cant hit this line");
}
}
in the JDWP, there are 3 types of breakpoint: class, method and line.
If your IDE fails to intercept the line breakpoint in the println(), then you can try a method breakpoint on the run() reclaration.
If that fails, there is something out of sync between the byte code and the source. You can try adding lines, breakpointing another line above and below.
Other than that, change IDE and/or change JVM. This should work.
You don't have a t.join() nor a sleep in the main branch of your code so the new thread starts in theory but your main method also keeps running and exits. The application terminates before it even has a chance to do something in your other thread and the breakpoint is not reachable.

How to prevent ctrl+c killing spawned processes in Java

[NB. This is related to How do I launch a completely independent process from a Java program? but different]
I want to be able to spawn external processes (shell scripts) from a "manager" Java process that should keep running when the JVM is killed - but it seems that when I kill the parent Java program the child is killed too (note, the behaviour is different if the JVM exits naturally). The simplest test program I have is:
public class Runit {
public static void main(String args[]) throws IOException, InterruptedException {
Runtime.getRuntime().exec(args[0]);
// doesn't work this way either
// ProcessBuilder pb = new ProcessBuilder(args[0]);
// pb.start();
while (true) {
System.out.println("Kill me");
Thread.sleep(2000);
}
}
}
and external script:
#!/bin/sh
while [ 1 ] ; do
ls
sleep 1
done
run as
java -classpath jar-with-dependencies.jar temp.exec.Runit runit.sh
If the manager simply exits (i.e. take out the "while" loop in the Java program) then the spawned process keeps running, but when I Ctrl+c the Java program the external program is killed too which is not what I want.
I'm using OpenJDK 1.6 on Ubuntu.
Edit1: Changing the exec to
Runtime.getRuntime().exec("/usr/bin/nohup " + args[0]);
doesn't help.
Edit2: Adding a shutdown hook as described in How to gracefully handle the SIGKILL signal in Java doesn't stop the Ctrl+c being propagated to the child.
Vladimir gave the hint we needed! (Sorry, beat Lukasz to it)
Add another script spawn_protect.sh
#!/bin/sh
LOG=$1
shift
nohup $* > $LOG 2>&1 &
And change the manager to:
public class Runit {
public static void main(String args[]) throws IOException, InterruptedException {
Runtime.getRuntime().exec(args);
while (true) {
System.out.println("Kill me");
Thread.sleep(5000);
}
}
}
Then run as:
java -classpath jar-with-dependencies.jar temp.exec.Runit spawn_protect.sh /tmp/runit.log runit.sh
Now runit.sh is really detached from the JVM process!
In Linux, if you start another process, it is your child and you are his parent. If parent gets killed, all children get killed, and their children too (what a terrible atrocity).
What you need, is to start a process that won't be killed when you exit your program. So, you need to give birth to not your own child. The methods to do that are described for example here: Linux: Prevent a background process from being stopped after closing SSH client for example use screen utility.
You've got to make it a daemon. Don't be afraid it's not a horror movie. Simply you'll need to detach your processes from controlling terminal session. I've always do it in a oposite way: shell script that launches Java.
Here is an explanation:
http://en.wikipedia.org/wiki/Daemon_(computing)
You can also you "jvm shutdown hooks", but they will not work in some situations.

How to start a java program?

I have written a java program and I am running it through command line like "java MyProgram"
Now I want to have a GUI that have a start, pause and stop button. How to start that java program by clicking on start button. How to pause it and how to stop it?
I assume your program is something like:
public class MyProgram {
public void doSomething() {
// ... does something ...
}
public static void main(String[] args) {
new MyProgram().doSomething();
}
}
I recommend reading the Swing Tutorial, but a simple GUI to launch your program could be something like:
public class MyProgramLauncher {
public static void main(String[] args) {
final MyProgram myProgram = new MyProgram();
JFrame frame = new JFrame("My Program");
JComponent cp = frame.getContentPane();
cp.add(new JButton(new AbstractAction("Start") {
public void actionPerformed(ActionEvent e) {
myProgram.doSomething();
}
}));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
If your class has a pause() function, you could add a similar JButton to call that function, but you'd have to write/implement the function.
You would, however, have to launch this with java MyProgramLauncher, which isn't very exciting. Still, that will get you a basic GUI in which you can experiment with starting, pausing, etc.
To turn your program into something you can double-click on, you'll need to create a JAR file. This is basically a special ZIP file that includes all the classes in your application, plus a manifest.xml file that describes those classes and (for launchable JAR files) identifies the "main class" whose main() method should be called when the JAR file is launched.
To turn that JAR file into a more or less self-contained deployable application is a bigger pain and there are a whole lot of options. The Deployment Tutorial might be a place to start.
Basically you need a native launcher, but I cannot figure out what do you mean exactly by "pause"... Sending the process to sleep?
I think that should be very easy to implement with a shellscript using the xdialog command in a Unix like system.
You'll need to implement a state machine:
State: "Stopped"
Start: execute java YourProgram and store the PID in a variable. Change state to "Started"
Pause: do nothing/disabled
Stop: do nothing/disabled
State: "Started"
Start: do nothing/disabled
Pause: send the STOP signal (like ctr+z) to the process. Change state to "Paused"
Stop: send the INT signal (like ctr+c) to the process. Change state to "Stopped"
State "Paused"
Start: do nothing/disabled
Pause: send the CONT signal (like doing fg) to the process. Change start to "Started"
Stop: send the INT signal (like ctr+c) to the process. Change state to "Stopped"
With this, you can loop in the script and react to the buttons. Look the reference for kill and dialog or xdialog for more details on the implementation.
First you need to write your "Forms"...
This will be a helpful resource to a beginner.
Basics

Categories

Resources