Tomcat, starting a deamon thread

J

jbj

I am trying to start a daemon thread on the startup of tomcat, but so
far all I can find is how to make tomcat launch a servlet (and i could
then have the servlet launch the thread, but once done, it would close
and defeat the purpose). Is there anyway to let tomcat launch the
thread itself?

Lets say the thread is called ThreadCls, and it has a run function.
The only way i can see it is to make a Launcher class, and have that
class actually handle set ThreadCls to daemon and then starting it.

But as i mentioned, Launcher then exits (so tomcat can contiunue
loading), and that kills my deamon. Any idea of what to do? Thanks
 
O

Oscar kind

jbj said:
I am trying to start a daemon thread on the startup of tomcat, but so
far all I can find is how to make tomcat launch a servlet (and i could
then have the servlet launch the thread, but once done, it would close
and defeat the purpose). Is there anyway to let tomcat launch the
thread itself?

What do you mean by "let tomcat launch the thread itself"? Must it be the
container that starts the thread? Or is an application starting the thread
at startup (from the init method) good enough?

If it is the former, you'll probably have to patch Tomcat. Any other
method is likely to be either an unreliably hack or too complicated to
maintain.

Lets say the thread is called ThreadCls, and it has a run function.
The only way i can see it is to make a Launcher class, and have that
class actually handle set ThreadCls to daemon and then starting it.

But as i mentioned, Launcher then exits (so tomcat can contiunue
loading), and that kills my deamon. Any idea of what to do? Thanks

It appears you want the started thread to influence Tomcat's behaviour.
What kind of influence do you want? If it's for security, use Tomcat's
security mechanisms. If not, you'll definitely need to patch Tomcat.
 
W

William Brogden

I am trying to start a daemon thread on the startup of tomcat, but so
far all I can find is how to make tomcat launch a servlet (and i could
then have the servlet launch the thread, but once done, it would close
and defeat the purpose).

Do what? What servlet close???
thread itself?

As I recall you can do that with a ServletContextListener - it will get
a call to contextInitialized when the server starts.

Tomcat 5 server.xml mentions this listener:
org.apache.catalina.mbeans.ServerLifecycleListener
but I don't know any more about it - seems to be a Java Management
Extension

Bill
 
J

jbj

Nope, i just have a dedicated box that I want to handle a few other
tasks. However, it should only do these tasks when tomcat is running,
hence letting tomcat launch the threads as deamons. No security or
anything complicated, just looking for a way to let tomcat start a few
other threads than just it's native ones.

I am currently using the load-on-init methods in the web.xml file to
get them going, but this doesn't let me run them as daemons. Any
ideas?
 
J

Joe

I am trying to start a daemon thread on the startup of tomcat, but so
far all I can find is how to make tomcat launch a servlet (and i could
then have the servlet launch the thread, but once done, it would close
and defeat the purpose). Is there anyway to let tomcat launch the
thread itself?

Lets say the thread is called ThreadCls, and it has a run function.
The only way i can see it is to make a Launcher class, and have that
class actually handle set ThreadCls to daemon and then starting it.

But as i mentioned, Launcher then exits (so tomcat can contiunue
loading), and that kills my deamon. Any idea of what to do? Thanks

Hi, I do exactly the same thing with a ServletContextListener (as
someone else suggested I think) ... just provide methods for
contextInitialized and start your thread from there, then make sure
it's sent a message to stop somehow in the contextDestroyed method.

Then tell tomcat about your listener in your webapps web.xml file:

<listener>
<listener-class>
...your class...
</listener-class>
</listener>

Hope that helps.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top