Runtime Exec within Timer Thread

D

dagreat

Hello,

I am trying to report system print queue statuses at specified
intervals. I am using java.util.Timer to implement a repeating task
that does this. My code executes properly when run without using
java.util.Timer. However, it does not work when using Timer.

--------------------------------------------------
Runtime.getRuntime().addShutdownHook(new ShutdownHook());


Timer timer = new Timer(false);
// get time interval randomly. The interval will be somewhere
between
// 9 and 11 minutes.
GregorianCalendar now = new GregorianCalendar();
Random randomGen = new Random(now.getTimeInMillis());
int intervalVariance = randomGen.nextInt(maxIntervalVariance);
//long interval = minInterval + intervalVariance;
long interval = 3000;
// Create The task
StatusReportTask task = new StatusReportTask(util);
timer.schedule(task,now.getTime(),interval);

--------------------------------------------------

The code seems to be breaking when making a system call to get a list
of print queues on the system...
String[] cmd = { "/bin/sh", "-c", "lpstat -p | awk '/^printer/ {print
$2}'" };
----------------------------------------------------
Vector queues = new Vector();
Runtime rt = Runtime.getRuntime();
rt.traceInstructions(true);
String[] cmd = { "/bin/sh", "-c", "lpstat -p | awk '/^printer/
{print $2}'" };
try {
Process p = rt.exec(cmd);

String line;
// read STDOUT from command
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if (line != null && line.trim().length() > 0 )
queues.add(line.trim());
}
input.close();
int waitRet = p.waitFor();
int exitValue = p.exitValue();
System.out.println("exitValue:"+exitValue);

} catch (Exception e) {
throw new Exception(e.getMessage()+ ": Could not get print queue
list.");
}
String[] strs = new String[queues.size()];
return (String[])queues.toArray(strs);
}
--------------------------------------------------

What seems to be happening is that I lose the process after executing
it. It never seems to return (The exit value does not get printed- as
it should) and an exception is never caught. It seems that no code is
executed after I call and waitFor the process... except my shutDownHook
is printing some output. and also... the app should not terminate
because Timer() should keep it running until explicitly killed.
However, it is terminating following the p.exec() call.

Any ideas?

thanks
 
R

Roedy Green

// 9 and 11 minutes.
GregorianCalendar now = new GregorianCalendar();
Random randomGen = new Random(now.getTimeInMillis());
int intervalVariance = randomGen.nextInt(maxIntervalVariance);
//long interval = minInterval + intervalVariance;
long interval = 3000;

There is no need to fool around with GregorianCalendar.
new Random() seeds itself from the clock by default.

If you need "now" in future, you can get it more easily with
System.currentTimeMillis()
 
D

dagreat

here ya go... thanks for the help.
------------------------------------------------------------------
public void run() {
try {
StatusMap statusMap = new StatusMap();

Set statuses = statusMap.keySet();
Iterator statusIt = statuses.iterator();
while(statusIt.hasNext()) {
String paramName = (String)statusIt.next();
// The status reporter knows how to gather the data.
StatusReporter reporter =
(StatusReporter)statusMap.get(paramName);
reporter.getStatusFromSystem(appUtil);

// send report where it needs to go
reporter.sendReport(url);
}
} catch (Exception e)
{
// log exception
appUtil.log(e);
}
}
 
D

dagreat

thanks for the help. I changed the code as you suggested, but I'm
still having the same problem... the application seems to be exiting
without finishing the code, throwing an exception or anything. I even
added some code to catch the more general Throwable, but I still get
nothing.
---------------------------------------------
code here.....

p.waitFor();
exitValue = p.exitValue();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Interupted thread");
} catch (Exception e) {
System.out.println("Exeption");
exception = e;
} catch (Throwable t) {
System.out.println("Throwable");
}
 
D

dagreat

Just an update... I added...

while (true) {}

....after scheduling the timer task, and it no longer dies. I thought
that it was supposed to keep running without brute force, though. Could
this problem be specific to the Linux RTE?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top