Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Running Tomcat From Java Code

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to run tomcat from my java code. It works fine and start the tomcat perfectly but immediately it will shutdown the current or started thread.

Problem might be thread dies after execution and will stops JVM execution.

Following is the code snippet. Required bootstrap.jar of tomcat for running Code.

import org.apache.catalina.startup.Bootstrap;

public class TomcatLauncher implements Runnable {


public Bootstrap bootStrap;
public Boolean readyToExit = false;
static Thread t ;
private Thread shutdownThread;
static
{
TomcatLauncher tl = new TomcatLauncher();
t = new Thread(tl);
t.setDaemon(true);
}

public static void main(String[] args)
{
t.start();

}
public void run() {
//registerShutdownHook();
startTomcat();
showStartPage();
}
private void startTomcat()
{
try
{
log("Starting Tomcat...");
bootStrap = new Bootstrap();
bootStrap.init();
bootStrap.start();
log("...Tomcat started.");
// Thread.sleep(1555555);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
private void log(String msg) {
// FIXME you will probably want to make this write to a file
System.out.println(msg);
}
}

Please help ...........
Thanks

Arpit
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're making the TomcatLauncher thread a daemon, which means it will exit (as will the JVM) if there are no other non-daemon threads running. Is that the thread that runs Tomcat?
 
Arpit Tayal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes my intention of making thread as daemon thread that it can not be shutdown by any user thread. Daemon thread is running tomcat still it shutdown the Tomcat. May be due to JVM shutdown . Can we fork a new JVM or any other idea
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpit Tayal wrote:Yes my intention of making thread as daemon thread that it can not be shutdown by any user thread. Daemon thread is running tomcat still it shutdown the Tomcat. May be due to JVM shutdown . Can we fork a new JVM or any other idea




I think you are confusing the definition of "daemon" thread -- perhaps with the unix definition of daemon process. In Java, a daemon thread doesn't mean it can't be shut down by another thread. A daemon thread just means that the thread is not a user thread, and the JVM automatically terminates when there are no user threads.

Henry
 
Arpit Tayal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the solution. The problem is that Daemon thread automatically Shutdown when JVM shutdown. So for the Solution i made a applet and from applet i starts the daemon thread.So it will not shutdown until i have applet running
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpit Tayal wrote:The problem is that Daemon thread automatically Shutdown when JVM shutdown. So for the Solution i made a applet and from applet i starts the daemon thread.So it will not shutdown until i have applet running


You have an *applet* that starts a servlet container? Ugh.

If the problem is the daemon nature of a thread, then why do you make it a daemon?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic