2

I would like to do the following:

Write a file to disk. Then run a shell command. The shell command reads/manipulates this file sends a request over the network writes the response to another file and returns and exit value.

In order to run the shell command i have read about ProcessBuilder and Runtime.exec().

How exactly would one implement the above functionality. The webapp (struts) responsible will be hosted in Jbossas 7.1.

Do i need some like JCA since i.o and EJBs are not recommended (although only one server will be used in my case). Can i simply use a POJO (Struts action) i am not sure how to deal with multiple threads, although Struts actions are "thread safe".

Thanks in advance,

If i were to use a pool of threads in order to handle multiple requests in parallel would this be a reason to use a JCA.

asked Sep 17, 2012 at 11:32
3
  • How difficult would it be to recreate the functionality that creates the network request in Java? If you can do you then need the files to be created on disk or do they just aid in creating the network request? Commented Sep 25, 2012 at 22:40
  • Difficult because it has to do with billing/tax forms that need to be perserved Commented Sep 26, 2012 at 7:55
  • Wouldn't the fact that it's billing/tax forms be a compelling reason to rewrite it as I would consider a solution built on integrating a HTTP request with Java and the shell calls a bit on the fragile side. Also, what would happen when 20 calls come in at the same time. Can the shell script be run concurrently? Could there be a risk of user input being injected to the shell script maliciously? Commented Sep 26, 2012 at 9:02

2 Answers 2

2
+25

You should take a look at the Apache Camel framework: http: //camel.apache.org/index.html. It is a very useful and easy to learn/use integration framework. It has all the components that you need:

I used it in a Java EE environment in one project (JBoss AS 7.1 + JSF2/Richfaces) and like it very much.

Arjan Tijms
38.2k12 gold badges112 silver badges144 bronze badges
answered Sep 26, 2012 at 13:40
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry i didn't notice your question. I think i can share some parts of this code with you if you are still interested.
Yes please that would be great
1

I confirm there is no need to bother with JCA and EJB as far as two-phase-commit transaction propagation in such a context does not sound relevant and definitely hard to implement.

I recommend you to create request and response (first empty) files in Java thanks to File.createTempFile to avoid collision on file names between threads and invoke your shell script with ProcessBuilder.

Technically speaking, the most difficult point for such an implementation is error handling to free resources: temporary files, stuck processes:

  • use try/finally to remove file after usage
  • as File.deleteOnExitis not relevant for a long running server, you will have to implement a regular job to clean temporary files left after unexpected failures
  • a timer watch dog should kill script processes than have not answered in delay, some may be stuck for any reason

By the way, to avoid troubles with temporary files and improve performance, if your code runs on a Unix system, I recommend you to use stdin/stdout to pass request and response between processes and design your script as less file access as possible.

But your system may raise some limits: the resources required to create a new process and the processing time of a script can lead to thousands of processes living in your operating system, in addition to JBoss threads waiting for process returns (so less threads available to process short web requests). To avoid crashes, the watch dog should limit the number of created processes in any moment - and that threshold must be defined thanks to a benchmark.

answered Sep 24, 2012 at 20:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.