You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Nathan Thatcher <n8...@gmail.com> on 2008/11/18 09:40:01 UTC

Tomcat vs Daemons

I am porting an application from Windows to Linux which makes heavy use of
servlets. I have a fairly intensive background process (currently a windows
service) that requires no user interaction. I plan to rewrite the code in
Java and I am wondering if there is any notable performance difference
between running the Java code under Tomcat as opposed to running the code
directly as a daemon. Since the application requires Tomcat, I know I can
code it up as a servlet and kick off the processing in the init() function
and not worry about executing the code directly as a daemon. Since this is a
performance sensitive process I want to make sure that I choose the optimal
route.

Does anybody know how java code running as a servlet compares
performance-wise to the same code running as a daemon? Is one way generally
better than the other?

Thanks,

Nate Thatcher

Re: Tomcat vs Daemons

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

Nathan,

removeps-groups@yahoo.com wrote:
> In my code, I have a ServletContextListener (configured in web.xml or
> similar file, don't remember exactly), so when the servlet context is
> created it calls contextInitialized(ServletContextEvent).  In this
> function I start the daemon threads.  There is a function
> contextDestroyed which you can use to stop the threads (but note
> Thread.stop is deprecated).

Yup, that'll work. If you subclass Thread (or, better yet, Runnable),
you can add a method that tells the process to stop itself: basically,
you set a flag that your long-running loop (or whatever) periodically
checks to see if it's been set. If it's been set, you simply return from
the "run" method and the thread stops. This is much better than calling
Thread.stop().

> It sounds like a good idea to start these background threads in a new
> process.  However, I don't know if you can do this in a hosted web
> server environment like the kind you find at mochahost.com and
> dailyrazor.com.  Will need to test it out.

Yeah, you'll need to see what they allow you to do. cron is, of course,
the best choice if it's available. Otherwise, you may have to run your
process from within your web application. :(

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

iEYEARECAAYFAkkkIcEACgkQ9CaO5/Lv0PChigCaAhi4PtzdVngSBdAZ4aRiq8IS
c/IAoIaNVjkBxa8Z9MUpwQxEEsGysHcc
=GttZ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat vs Daemons

Posted by re...@yahoo.com.
In my code, I have a ServletContextListener (configured in web.xml or similar file, don't remember exactly), so when the servlet context is created it calls contextInitialized(ServletContextEvent).  In this function I start the daemon threads.  There is a function contextDestroyed which you can use to stop the threads (but note Thread.stop is deprecated).

It sounds like a good idea to start these background threads in a new process.  However, I don't know if you can do this in a hosted web server environment like the kind you find at mochahost.com and dailyrazor.com.  Will need to test it out.


--- On Tue, 11/18/08, Christopher Schultz <ch...@christopherschultz.net> wrote:

> From: Christopher Schultz <ch...@christopherschultz.net>
> Subject: Re: Tomcat vs Daemons
> To: "Tomcat Users List" <us...@tomcat.apache.org>
> Date: Tuesday, November 18, 2008, 1:35 PM
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Nathan,
> 
> Nathan Thatcher wrote:
> > I am porting an application from Windows to Linux
> which makes heavy use of
> > servlets. I have a fairly intensive background process
> (currently a windows
> > service) that requires no user interaction. I plan to
> rewrite the code in
> > Java and I am wondering if there is any notable
> performance difference
> > between running the Java code under Tomcat as opposed
> to running the code
> > directly as a daemon.
> 
> Not really, but I don't see a compelling reason to run
> something under a
> servlet container if it doesn't actually need to be
> running inside a
> servlet container. Is also simplifies testing, deployment,
> etc if you
> leave it separate.
> 
> > Since the application requires Tomcat, I know I can
> > code it up as a servlet and kick off the processing in
> the init() function
> > and not worry about executing the code directly as a
> daemon.
> 
> While this certainly will work (I'm assuming you're
> kicking-off a thread
> to do this work), it's a pretty big hack ;)
> 
> > Since this is a
> > performance sensitive process I want to make sure that
> I choose the optimal
> > route.
> 
> So, you want to make sure that the process itself gets
> enough CPU time?
> I think you want to be able to control this process
> separately from your
> webapps. *NIX has finer-grained levels of process priority
> than
> Microsoft Windows does (at least, those that are exposed to
> the user),
> and you can use the program "nice" to run a job
> with higher or lower
> priority (note there is no "mean" command... you
> just use a negative
> "niceness" value).
> 
> > Does anybody know how java code running as a servlet
> compares
> > performance-wise to the same code running as a daemon?
> Is one way generally
> > better than the other?
> 
> Once your code starts up, there should be no performance
> difference
> since you're not adding any overhead from the container
> (that is, you
> don't have to use any of their classes or objects or
> anything). The only
> problem is that it's running in the same JVM as the
> actual servlets in
> your application.
> 
> I'd keep it as a separate application if I were you.
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla -
> http://enigmail.mozdev.org
> 
> iEYEARECAAYFAkkjNTsACgkQ9CaO5/Lv0PBEogCfVE3yTS6SV/BSpK810P2XYtW/
> HX8AoMKeCUSOK2tlDLHdVWLzGRtCUZaI
> =Q41d
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail:
> users-help@tomcat.apache.org

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat vs Daemons

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

Nathan,

Nathan Thatcher wrote:
> I am porting an application from Windows to Linux which makes heavy use of
> servlets. I have a fairly intensive background process (currently a windows
> service) that requires no user interaction. I plan to rewrite the code in
> Java and I am wondering if there is any notable performance difference
> between running the Java code under Tomcat as opposed to running the code
> directly as a daemon.

Not really, but I don't see a compelling reason to run something under a
servlet container if it doesn't actually need to be running inside a
servlet container. Is also simplifies testing, deployment, etc if you
leave it separate.

> Since the application requires Tomcat, I know I can
> code it up as a servlet and kick off the processing in the init() function
> and not worry about executing the code directly as a daemon.

While this certainly will work (I'm assuming you're kicking-off a thread
to do this work), it's a pretty big hack ;)

> Since this is a
> performance sensitive process I want to make sure that I choose the optimal
> route.

So, you want to make sure that the process itself gets enough CPU time?
I think you want to be able to control this process separately from your
webapps. *NIX has finer-grained levels of process priority than
Microsoft Windows does (at least, those that are exposed to the user),
and you can use the program "nice" to run a job with higher or lower
priority (note there is no "mean" command... you just use a negative
"niceness" value).

> Does anybody know how java code running as a servlet compares
> performance-wise to the same code running as a daemon? Is one way generally
> better than the other?

Once your code starts up, there should be no performance difference
since you're not adding any overhead from the container (that is, you
don't have to use any of their classes or objects or anything). The only
problem is that it's running in the same JVM as the actual servlets in
your application.

I'd keep it as a separate application if I were you.

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

iEYEARECAAYFAkkjNTsACgkQ9CaO5/Lv0PBEogCfVE3yTS6SV/BSpK810P2XYtW/
HX8AoMKeCUSOK2tlDLHdVWLzGRtCUZaI
=Q41d
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat vs Daemons

Posted by Kees Jan Koster <kj...@kjkoster.org>.
Dear Nate,

> I am porting an application from Windows to Linux which makes heavy  
> use of
> servlets. I have a fairly intensive background process (currently a  
> windows
> service) that requires no user interaction. I plan to rewrite the  
> code in
> Java and I am wondering if there is any notable performance difference
> between running the Java code under Tomcat as opposed to running the  
> code
> directly as a daemon.

No there is not. Tomcat just handles the HTTP serving part and the  
processing is done in Java, just as it would it it were a Tomcat-less  
JVM.

> Since the application requires Tomcat, I know I can
> code it up as a servlet and kick off the processing in the init()  
> function
> and not worry about executing the code directly as a daemon. Since  
> this is a
> performance sensitive process I want to make sure that I choose the  
> optimal
> route.

Performance is determined by many factors.

> Does anybody know how java code running as a servlet compares
> performance-wise to the same code running as a daemon? Is one way  
> generally
> better than the other?


No difference. You'd need something that handler HTTP access to your  
servlet, so you'd wind up with something like Tomcat anyway.

I think you have a strange idea of what a daemon is. After all, most  
of us run Tomcat as a daemon.

--
Kees Jan

http://java-monitor.com/forum/
kjkoster@kjkoster.org
06-51838192

The secret of success lies in the stability of the goal. -- Benjamin  
Disraeli


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org