Executing a shell script in java as a thread - java

I need to execute a shell script in a java program. I figured out that i can use processbuilder and runtime.exec.. but my webserver times out every 180 sec but my script execution takes more than that..i do not want to use process for this approach.. is there any other way where i can use thread for this execution.
thanks.

I'm assuming that the response from the script is intended for humans to read.
Good interface design, and human nature, suggests that if your script is taking over 180 seconds to run, then it should be run separately from the web server. On linux, I would suggest putting it into 'cron', and letting it run on a regular basis. You would only serve the results of the script via the web server, with a response time in seconds instead of minutes.
If your script depends on parameters from the http request, or other information that is only available from within the web server's environment, you have the following choices.
If you can figure out the likely combinations of parameters, run the
script automatically for each combination of parameters,
again only serving the results through the web.
If the majority of the time is spent in a single command, and the
results of that command don't change much between runs, move that
command into a separate script that runs automatically, and use the
results of that separate script to build the web response.
Break the response up into segments, only showing a portion of the
data for each request, allowing the user to page through the
response. The script would be rewritten to only request the
necessary data for the current page, reducing the amount of time
needed to obtain that data.
Rewrite the script in a compilable language, which might gain you enough time to make running it for every request reasonable. However, if the problem is a database query, this won't do you any good. You'd have to go with option (3), whether you rewrote it in a compilable language or not.
Without additional information, like an example of the script, or a description of where you're getting the results from, that's the best I can do.

A process can run several threads, but they still are parts of the process.
So, all threads inside a java program are the threads of the java process, and a thread cannot run another program's threads.
A shell script is ran by a program : the shell program ! (/bin/bash or /bin/sh)
Anyway a shell script will mostly ran other programs inside several other processes.
No, you cannot run a shell inside a thread of java.

In general, if you have code that is separate from your Java program, such as code that is in a separate script, then there is no justification for why your code would execute an outside script when that code could be instead integrated into the program. It is insecure at best. Your basically allowing arbitrary code to be executed by your program since the outside script is editable. What you are doing sounds to me almost like it should be confined either a unit test or a build task.
As a unit test task and you could use a threaded JUnit runner to run your outside script during the test phase of your project.
Also, separately from your program, you could also execute it using a Gradle task and by using the parallellforks option that Gradle has.

Related

Jar integration with jmeter

I wrote a Java class, I made it into a runnable jar, I want to call a method in that class in a beanshell or JSR223!
I wrote a beanshell sampler, in that I imported the class and called that method, the method calls another method which has multithreading, it uses ExecutorService.
What is happening is, the beanshell is working fine, the class is imported, the method is called, the method called another method which has threads, the problem comes when the thread is started, when the thread is started, the beanshell script is not moving further, the testcase in jmeter is not stopping at all, Is it because of threads in the class in that jar?
Given you have a runnable .jar it might be better idea to run it using OS Process Sampler. So you will be able to decide whether you want to wait till the .jar completes its work or not by using underlying operating system functionality like start command in MS Windows Family or & operator or nohup command in Linux.
See How to Run External Commands and Programs Locally and Remotely from JMeter article for more details.
If you would like to continue with scripting make sure to use JSR223 Test Elements and Groovy language as currently it is the best option in terms of performance.
With regards to your "not moving further" it is hard to tell what's going wrong without seeing your Java/Beanshell code, try checking jmeter.log file for suspicious entries.

Is it better to launch a Java app once and sleep or repeat launching and killing?

I have a Java application that needs to run several times. Every time it runs, it checks if there's data to process and if so, it processes the data.
I'm trying to figure out what's the best approach (performance, resource consumption, etc.) to do this:
1.- Launch it once, and if there's nothing to process make it sleep (All Java).
2.- Using a bash script to launch the Java app, and when it finishes, sleep (the script) and then relaunch the java app.
I was wondering if it is best to keep the Java app alive (sleeping) or relaunching every time.
It's hard to answer your question without the specific context. On the face of it, your questions sounds like it could be a premature optimization.
Generally, I suggest you do what's easier for you to do (and to maintain), unless you have good reasons not to. Here are some possible good reasons, pick the ones appropriate to your situation:
For sleeping in Java:
The check of whether there's new data is easier in Java
Starting the Java program takes time or other resources, for example if on startup, your program needs to load a bunch of data
Starting the Java process from bash is complex for some reason - maybe it requires you to fiddle with a bunch of environment variables, files or something else.
For re-launching the Java program from bash:
The check of whether there's new data is easier in bash
Getting the Java process to sleep is complex - maybe your Java process is a complex multi-threaded beast, and stopping, and then re-starting the various threads is complicated.
You need the memory in between Java jobs - killing the Java process entirely would free all of its memory.
I would not keep it alive.
Instead of it you can use some Job which runs at defined intervals you can use jenkins or you can use Windows scheduler and configure it to run every 5 minutes (as you wish).
Run a batch file with Windows task scheduler
And from your batch file you can do following:
javac JavaFileName.java // To Compile
java JavaFileName // to execute file
See here how to execute java file from cmd :
How do I run a Java program from the command line on Windows?
I personally would determine it, by the place where the application is working.
if it would be my personal computer, I would use second option with bash script (as resources on my local machine might change a lot, due to extensive use of some other programs and it can happen that at some point I might be running out of memory for example)
if it goes to cloud (amazon, google, whatever) I know exactly what kind of processes are running there (it should not change so dynamically comparing to my local PC) and long running java with some scheduler would be fine for me

Performance in running perl program from java spring controller

I am creating a web application where user enters a query and i have a program in perl which tag the words in query. I am writing a spring controller to handle the Web request and then call the perl program from java class. Is this the optimized way. Is there any performance issue or time delay in calling perl program from java?
Well, it depends on how you are calling the perl program. When running a perl script, the perl interpreter starts, then compiles the script, then runs it. This means everytime you run program you spawn a new process, read the text file(s) containing the program, interpret them, and then get the results.
This is not optimum.
To optimize the process, write a wrapper in perl for you perl script that runs it continuously in a loop, waiting for input from your java process via a socket, named pipe, or file descriptor. This way, the process does not have to start over and over again. To support greater concurrency, run a pool of these.

How to open a matlab session from java and execute matlab scripts

I have a matlab script that takes an input mat file and produces an output file.
Currently I have a shell script that is able to execute this script.
I am executing this script from Java using ProcessBuilder class and all is working well.
I need to perform the matlab script execution very often from java and each time script executes the mcr process is created, script executes and mcr terminates.
i like to have the MCR process open and have a matlab session open towards java , so that same process is kept alive so i can execute scripts multiple times in the same session.
I find matlabcontrol seems to be suitablefor this.
How to configure the MatlabProxyFactory with the MCR location and LD_LIBRARY_PATH
which i am setting in my shell script and execute my script in a same session?
I also came across a tool MATLAB JA Builder but i cannot use this at the moment as its not Free.
Thanks!
If you are using matlabcontrol I don't think you need the shell script, just tell it the name of the m-file you want to be run. If you want to keep the shell script in conjunction with a ProcessBuilder, you have to start the process once and keep a reference to its input stream. When you want a command executed, you write it that stream. Also, you have to keep a thread alive to empty the output and error streams of Matlab, otherwise they will get full and Matlab will hang. If you want to go with this, I recommend looking at the source of this project (which I am a contributor to). The class you are interested in is RCaller. It does more or less the same, except it invokes R and not matlab.

How can I save a process resource from proc_open in order to check the status later on?

I'm running a sh script that runs a java process through php on ubuntu server. I'm using proc_open for running the process. usually the Workflow goes like :
request a page ->
script runs (until it's finished) ->
result page.
In my case the script runs in parallel so the server won't wait until the script is finished (it takes hours sometimes so it can't) , so I need to save that resource somehow to follow it later (status of the process or just stopping it).
The resource type is "process", I used this function get_resource_type for getting it.
Serialize won't work at this case - resource is an exceptional for it (you can look at http://il2.php.net/manual/en/function.serialize.php inside the Parameters box).
My target is a good process handling. does someone know how can i use the resource or other way you would do for process handling.
You can't store resource types for later use in PHP. What you need to do is implement some form of asynchronous communication - maybe a file, where one writes status information and the other one reads, a shared memory, a named pipe, ...
I would look into the pcntl extension. Hint: Forking is not possible from within a web-server environment for security reasons.
In my case the script runs in parallel so the server won't wait until the script is finished (it takes hours sometimes so it can't) ..
That shouldn't be a problem on its own. You can easily have a long running php-process, as long as it's not initiated from a web server. If you need to initiate the process from a web application, I would suggest that you insert an entry in a database table, and then have a cronjob run a script, which checks this queue and do the processing.

Categories

Resources