• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issues with Runtime.exec Process.getInputStream and Process.waitFor().

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have written a code to check whether HSQl, JBoss, Radius (AAA sever) and MySQL is running or not.The code is written in an inifinte while loop to continuously monitor.Now I tested that if this service(let's say hsql) is running then "/bin/bash -c ps -ef | grep 'hsql' | wc -l" will return 3 when this is passed as an argument in Runtime.exec() method.Now I have come to know following facts

1.The inputstream or errstream of the forked process must not be overflowed.Otherwise it 'll cause deadlock.

Keeping that in mind I have written this code



Output I'm expecting as follows

HSQL is running/not running 1
JBoss is running/not running 1
MySQL is running/not running 1
-------------------1----------------

HSQL is running/not running 2
JBoss is running/not running 2
MySQL is running/not running 2
-------------------2----------------

and so on...

Howevere I'm not getting like that.Some times HSQL's status message is not printed or sometimes JBoss status message is not printed.Even sometimes I got the output like
------------------1----------------------

after this status messages are printed.All I'm trying to say is that seems to be some race condition or some synchronization problem.

Thanks & Regards,
Arka




 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arka Sharma wrote:All I'm trying to say is that seems to be some race condition or some synchronization problem.



I wouldn't call it a problem exactly. Sometimes you want to execute a program and let it run independently while your Java program continues to run; it depends on what that program does.

About synchronous / asynchronous operation, yes, you are right. Quoting from the the JDK API's ProcessBuilder page:

There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object.



If you need the program to run synchronously, it is possible. I've done this in the past but can't remember exactly how I did it; I'll have to get back to you on this. If you're not opposed to using API's for this, the Apache Commons Exec project might be useful to you, but its documentation assumes you know how Java executes other programs in detail. Executing other programs with Java can be tricky.

CNH
 
reply
    Bookmark Topic Watch Topic
  • New Topic