waiting for external process to finish (using Runtime and Thread classes)

B

brownjenkn

Hi all,

I was hoping to get some advice/strategy on how best to do this. I
want to write a form to update a user's password on DB2 via a servlet.
The code below runs okay, and successfully updates a user password, but
I'd also like to verify it updated okay with a little dummy query at
the end. Having virtually no experience with threading, or the Runtime
class, etc. I think I need some help...

The idea behind the code below is to have the current thread sleep 5sec
which will give the external db2 process time to run and update the
password.


HERE'S MY CODE!

//----------------------------------------------------------
Runtime rt = Runtime.getRuntime();
rt.exec("db2cmd /c db2 connect to {someDatabase} user {someUser} using
{somePassword} new {newPassword} confirm {newPassword}")

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

/*

- code to connect to {someDatabase} and run a query to verify if update
was successful using whatever {newPassword} is

- if query runs okay, redirect to a "success" page, else display an
error message

*/

//----------------------------------------------------------

Question 1- is putting the current thread to sleep a good way to
handle this?

Question 2- should I use Thread.currentThread().sleep(5000) instead?
(ie. do it "non-statically")

Question 3- should I consider:

Process p = Runtime.getRuntime().exec("db2cmd /c db2 connect to
{someDatabase} user {someUser} using {somePassword} new {newPassword}
confirm {newPassword}");

p.waitFor();

instead of dealing with the Thread class? According to the API
waitFor() "causes the current thread to wait, if necessary, until the
process represented by this Process object has terminated."

Question 4- is there a totally better way to do this?

Any advice is much appreciated.

Thanks, Marc
 
M

Matt Humphrey

Hi all,

I was hoping to get some advice/strategy on how best to do this. I
want to write a form to update a user's password on DB2 via a servlet.
The code below runs okay, and successfully updates a user password, but
I'd also like to verify it updated okay with a little dummy query at
the end. Having virtually no experience with threading, or the Runtime
class, etc. I think I need some help...

The idea behind the code below is to have the current thread sleep 5sec
which will give the external db2 process time to run and update the
password.


HERE'S MY CODE!

//----------------------------------------------------------
Runtime rt = Runtime.getRuntime();
rt.exec("db2cmd /c db2 connect to {someDatabase} user {someUser} using
{somePassword} new {newPassword} confirm {newPassword}")

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

/*

- code to connect to {someDatabase} and run a query to verify if update
was successful using whatever {newPassword} is

- if query runs okay, redirect to a "success" page, else display an
error message

*/

//----------------------------------------------------------

Question 1- is putting the current thread to sleep a good way to
handle this?

I think you're better off waiting for the external process to finish,
although you may want to consider a watchdog timer in case it takes too
long.
Question 2- should I use Thread.currentThread().sleep(5000) instead?
(ie. do it "non-statically")

sleep is a static method of Thread. Invoking via currentThread() often
confuses the issue, especially when people start calling sleep on other
threads, e.g. otherThread.sleep(2000) is a valid statement, but actually
makes the current thread sleep.
Question 3- should I consider:

Process p = Runtime.getRuntime().exec("db2cmd /c db2 connect to
{someDatabase} user {someUser} using {somePassword} new {newPassword}
confirm {newPassword}");

p.waitFor();

instead of dealing with the Thread class? According to the API
waitFor() "causes the current thread to wait, if necessary, until the
process represented by this Process object has terminated."

Yes, this is better.
Question 4- is there a totally better way to do this?

Yes. You need to ensure that your exec target either does not produce any
output at all or that you properly consume that output in two separate
threads (one for the exec's standard output and one for standard error.)
See this: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
The reason is that if the exec produces output and you don't consume it, the
exec will block waiting for you to read it. Meanwhile you are waiting for
the process to finish, so voila deadlock.
Any advice is much appreciated.

Why don't you connect to the database directly via JDBC?

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top