Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   waiting for external process to finish (using Runtime and Thread classes) (http://www.velocityreviews.com/forums/t142963-waiting-for-external-process-to-finish-using-runtime-and-thread-classes.html)

brownjenkn@aol.com 04-22-2005 06:53 PM

waiting for external process to finish (using Runtime and Thread classes)
 
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


Matt Humphrey 04-22-2005 07:55 PM

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

<brownjenkn@aol.com> wrote in message
news:1114196035.410411.262540@g14g2000cwa.googlegr oups.com...
> 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/j...229-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 matth@ivizNOSPAM.com http://www.iviz.com/




All times are GMT. The time now is 07:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.