You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ashish Kulkarni <as...@gmail.com> on 2007/11/05 16:40:09 UTC

[OT] Write a thread to check database in web application

Hi
I have to write a thread in web application which will check some values in
database, and then perform some function depending on the values.

There wont be any user input and this thread should be called after like 10
minutes, also i want to have a jsp page from where i can maintain this
thread, like stop, change the time it should run etc.

Are there any specific J2EE api i can use, or should i just use a time
thread, and store the handle to this thread in servlet context so i can
access and modify it..

Any suggections

Ashish

Re: [OT] Write a thread to check database in web application

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
On Mon, November 5, 2007 11:17 am, Ashish Kulkarni wrote:
> Hi
> I dont need more then one thread, or a timer job i would say, this timer
> should sleep and then activate like after 10 minutes, check the database,
> if
> there is nothing to do go back to sleep.
> I will look into quartz, is it ok to use Java Timer and TimerTask to do
> it.

Although I can't say I've ever done it that way, I don't see why it would
be any worse than a straight Thread approach... it might even be better
because the JVM might have a little more control over its management and
can possibly cooperate with the container a little better, but I don't
know that for sure (I also wouldn't be one bit surprised if Timers in Java
are just a thin abstraction around a Thread anyway).

> I will make sure that the class with does check database function will not
> go in loop or be there for ever

You'll also *probabyly* want to get your DB connection the same way as any
app code, which I presume is a JNDI lookup.  At least that way you get
some of that benefit in term of management, but there is a prime example
of making sure that thread code doesn't do anything unsavory... it'd be a
real shame to somehow keep that connection from being returned to the pool
and then find the thread continues to fine and eat a connection each time
:) (I've had this debate with some folks and there's also the thought that
NOT using the same conn pool as the container is actually better because
then the pool can't be exhausted... then again, at that point you run the
risk of chewing up the listener threads in the RDBMS, so six of one, half
dozen of another I figure).

Frank

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Author of "Practical Ajax Projects With Java Technology"
 (2006, Apress, ISBN 1-59059-695-1)
and "JavaScript, DOM Scripting and Ajax Projects"
 (2007, Apress, ISBN 1-59059-816-4)
Java Web Parts - http://javawebparts.sourceforge.net
 Supplying the wheel, so you don't have to reinvent it!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Ashish Kulkarni <as...@gmail.com>.
Hi
I dont need more then one thread, or a timer job i would say, this timer
should sleep and then activate like after 10 minutes, check the database, if
there is nothing to do go back to sleep.
I will look into quartz, is it ok to use Java Timer and TimerTask to do it.
For example this will be my taslk
 class MyTimerTask extends TimerTask
{
 public void run()
{
  //Class to check for database
}
}

Timer tt = new Timer();
tt.scheduleAtFixedRate(myTimerTask, 100000, 100000);

In ServletContext i will save instance of myTimerTask and then provide a jsp
to stop or start this Task


I will make sure that the class with does check database function will not
go in loop or be there for ever

Any suggestions





On 11/5/07, Frank W. Zammetti <fz...@omnytex.com> wrote:
>
> On Mon, November 5, 2007 10:40 am, Ashish Kulkarni wrote:
> > Hi
> > I have to write a thread in web application which will check some values
> > in
> > database, and then perform some function depending on the values.
> >
> > There wont be any user input and this thread should be called after like
> > 10
> > minutes, also i want to have a jsp page from where i can maintain this
> > thread, like stop, change the time it should run etc.
> >
> > Are there any specific J2EE api i can use, or should i just use a time
> > thread, and store the handle to this thread in servlet context so i can
> > access and modify it..
>
> Spawning threads in a servlet container is generally considered Bad
> Voodoo(tm).  If memory serves, it's even outlawed by the servlet spec.
>
> That being said, we've all done it, we'll all probably do it again, so
> mheh with the recommendations :)
>
> *That* being said, the key thing is to be extra careful doing it.  Make
> sure the thread code is as bullet-proof as possible, most especially when
> it comes to resource usage.  The reason spawning such threads is a bad
> idea in the first place is because they are not under control of the
> container and cannot be managed, nor can the resources it uses.
> Therefore, you'll need to do that yourself and be sure you play nice
> within the container.
>
> Also, be sure to mark it a daemon thread, otherwise you'll find it can and
> will hold up shutdown of the container.  Bumping its priority as low as
> possible is probably also a good idea.
>
> Or you can do as Chris said and use Quartz, which will deal with most of
> these concerns for you.  I say most because you can still write bad Quartz
> jobs that bork things as badly as if you didn't use Quartz at all, but
> it'll help a little bit.
>
> > Ashish
>
> Frank
>
> --
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
> AIM/Yahoo: fzammetti
> MSN: fzammetti@hotmail.com
> Author of "Practical Ajax Projects With Java Technology"
> (2006, Apress, ISBN 1-59059-695-1)
> and "JavaScript, DOM Scripting and Ajax Projects"
> (2007, Apress, ISBN 1-59059-816-4)
> Java Web Parts - http://javawebparts.sourceforge.net
> Supplying the wheel, so you don't have to reinvent it!
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: [OT] Write a thread to check database in web application

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
On Mon, November 5, 2007 10:40 am, Ashish Kulkarni wrote:
> Hi
> I have to write a thread in web application which will check some values
> in
> database, and then perform some function depending on the values.
>
> There wont be any user input and this thread should be called after like
> 10
> minutes, also i want to have a jsp page from where i can maintain this
> thread, like stop, change the time it should run etc.
>
> Are there any specific J2EE api i can use, or should i just use a time
> thread, and store the handle to this thread in servlet context so i can
> access and modify it..

Spawning threads in a servlet container is generally considered Bad
Voodoo(tm).  If memory serves, it's even outlawed by the servlet spec.

That being said, we've all done it, we'll all probably do it again, so
mheh with the recommendations :)

*That* being said, the key thing is to be extra careful doing it.  Make
sure the thread code is as bullet-proof as possible, most especially when
it comes to resource usage.  The reason spawning such threads is a bad
idea in the first place is because they are not under control of the
container and cannot be managed, nor can the resources it uses. 
Therefore, you'll need to do that yourself and be sure you play nice
within the container.

Also, be sure to mark it a daemon thread, otherwise you'll find it can and
will hold up shutdown of the container.  Bumping its priority as low as
possible is probably also a good idea.

Or you can do as Chris said and use Quartz, which will deal with most of
these concerns for you.  I say most because you can still write bad Quartz
jobs that bork things as badly as if you didn't use Quartz at all, but
it'll help a little bit.

> Ashish

Frank

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Author of "Practical Ajax Projects With Java Technology"
 (2006, Apress, ISBN 1-59059-695-1)
and "JavaScript, DOM Scripting and Ajax Projects"
 (2007, Apress, ISBN 1-59059-816-4)
Java Web Parts - http://javawebparts.sourceforge.net
 Supplying the wheel, so you don't have to reinvent it!


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Leon Rosenberg <ro...@googlemail.com>.
On 11/5/07, Christopher Schultz <ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Antonio,
>
> Antonio Petrelli wrote:
> > Though I think that Quartz is a great product, it is discouraged by the Java
> > EE specifications to create threads in a webapp.
>
> Hey, every time someone asks on the Tomcat list about how to do this, I
> tell 'em to use cron ;)
>
> I hate using Java for periodic tasks. :(

Geez, I love it! Scriptlets (small scripts written in java with a main
method and some lines of code) are fun to write and you can express
everything you need much better than in p€rl...

:-)
Leon

>
> - -chris
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFHL0+19CaO5/Lv0PARArjgAJ9jg9D29a5QXLN0Mbv38qi4cWWHEwCfeCRx
> bMAn+JSiTsB23WP3Hi7L3Ho=
> =LFxn
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Antonio,

Antonio Petrelli wrote:
> Though I think that Quartz is a great product, it is discouraged by the Java
> EE specifications to create threads in a webapp.

Hey, every time someone asks on the Tomcat list about how to do this, I
tell 'em to use cron ;)

I hate using Java for periodic tasks. :(

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHL0+19CaO5/Lv0PARArjgAJ9jg9D29a5QXLN0Mbv38qi4cWWHEwCfeCRx
bMAn+JSiTsB23WP3Hi7L3Ho=
=LFxn
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Gabriel Belingueres <be...@gmail.com>.
Hi,

If you are deploying on a full blown EJB container, you can use the
TimerService too.

I personally used Spring to run periodic TimerTasks and it works well.
You don't need to use Quartz if your scheduling needs are simple (like
run each 10 minutes).

2007/11/5, Antonio Petrelli <an...@gmail.com>:
> 2007/11/5, Christopher Schultz <ch...@christopherschultz.net>:
> >
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > Ashish,
> >
> > Ashish Kulkarni wrote:
> > > There wont be any user input and this thread should be called after like
> > 10
> > > minutes, also i want to have a jsp page from where i can maintain this
> > > thread, like stop, change the time it should run etc.
> > >
> > > Are there any specific J2EE api i can use, or should i just use a time
> > > thread, and store the handle to this thread in servlet context so i can
> > > access and modify it..
> >
> > Lots of folks use Quartz for this kind of thing. It's a cron-like Java
> > API.
>
>
>
> Though I think that Quartz is a great product, it is discouraged by the Java
> EE specifications to create threads in a webapp.
> The curious thing is that there is no a specification on how to create
> scheduled processes in Java EE, and every container has its own
> implementation, for example Websphere has Work Manager.
> So keep in mind that, if you are using Quartz, you are somewhat breaking the
> rules, though there is a missing rule :-)
>
> Ciao
> Antonio
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Antonio Petrelli <an...@gmail.com>.
2007/11/5, Christopher Schultz <ch...@christopherschultz.net>:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Ashish,
>
> Ashish Kulkarni wrote:
> > There wont be any user input and this thread should be called after like
> 10
> > minutes, also i want to have a jsp page from where i can maintain this
> > thread, like stop, change the time it should run etc.
> >
> > Are there any specific J2EE api i can use, or should i just use a time
> > thread, and store the handle to this thread in servlet context so i can
> > access and modify it..
>
> Lots of folks use Quartz for this kind of thing. It's a cron-like Java
> API.



Though I think that Quartz is a great product, it is discouraged by the Java
EE specifications to create threads in a webapp.
The curious thing is that there is no a specification on how to create
scheduled processes in Java EE, and every container has its own
implementation, for example Websphere has Work Manager.
So keep in mind that, if you are using Quartz, you are somewhat breaking the
rules, though there is a missing rule :-)

Ciao
Antonio

Re: [OT] Write a thread to check database in web application

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ashish,

Ashish Kulkarni wrote:
> There wont be any user input and this thread should be called after like 10
> minutes, also i want to have a jsp page from where i can maintain this
> thread, like stop, change the time it should run etc.
> 
> Are there any specific J2EE api i can use, or should i just use a time
> thread, and store the handle to this thread in servlet context so i can
> access and modify it..

Lots of folks use Quartz for this kind of thing. It's a cron-like Java API.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHLzuW9CaO5/Lv0PARAvhJAJwJd7S+Na4wkxGn1UYHvNcemQ1lcQCcDEos
VV6I06VumxlOsvYWymxisDM=
=xZD2
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Gary Affonso <gl...@greywether.com>.
Ashish Kulkarni wrote:
> Hi
> I have to write a thread in web application which will check some values in
> database, and then perform some function depending on the values.
> 
> There wont be any user input and this thread should be called after like 10
> minutes, also i want to have a jsp page from where i can maintain this
> thread, like stop, change the time it should run etc.
> 
> Are there any specific J2EE api i can use, or should i just use a time
> thread, and store the handle to this thread in servlet context so i can
> access and modify it..
> 
> Any suggections

If you're using Spring be sure to checkout its scheduling services.  It 
supports both Quartz and the JDK 1.3 Timer as underlying mechanisms.

Springs provides some very nice abstractions and features on top of 
those raw scheduling libraries.  And, of course, you get all all of the 
other nifty spring stuff (like being able to DI dependencies into your 
scheduled classes).

- Gary

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] Write a thread to check database in web application

Posted by Leon Rosenberg <ro...@googlemail.com>.
<2 cents>
servlet context sounds good.
Start from ServletContextListener upon server start.
</2 cents>

regards
Leon

On 11/5/07, Ashish Kulkarni <as...@gmail.com> wrote:
> Hi
> I have to write a thread in web application which will check some values in
> database, and then perform some function depending on the values.
>
> There wont be any user input and this thread should be called after like 10
> minutes, also i want to have a jsp page from where i can maintain this
> thread, like stop, change the time it should run etc.
>
> Are there any specific J2EE api i can use, or should i just use a time
> thread, and store the handle to this thread in servlet context so i can
> access and modify it..
>
> Any suggections
>
> Ashish
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org