Implementation of Asp.net control Tomcat startup and Shutdown

Source: Internet
Author: User

I. Scenarios
Recently, a project customer needs to be able to configure relevant permissions on their own. For historical reasons, this project uses the company's previous permission system. This permission system is very powerful, but there is a drawback, that is, every time you add a permission menu, it must be restarted to take effect, otherwise it will take one day to take effect after the cache expires. Because of the project progress, we cannot overturn this permission system.
The feasible method is to add the permission system restart button on the administrator interface. In this way, you can easily restart Tomcat after modifying permissions.
Ii. Technical Analysis
The Administrator System is based on. net BS. Therefore, we must be able to control the shutdown and startup of Tomcat through the web page and load it into the administrator system.
First, analyze the Tomcat startup method. There are two batch files in the bin directory of Tomcat6.0. Startup. bat and shutdown. bat control Tomcat startup and shutdown respectively. To control Tomcat startup and shutdown, you must call these two batch files.
How to adjust it? Where is the call? It is impossible to directly tune the browser. You must deploy a web page on the web server and call these two commands on the server. It would be the best if deployed in the permission system, but this will cause a problem. We can disable Tomcat, but it cannot be started. The final solution is to deploy a. net-implemented webservice on the same machine. This webservice enables and disables the permission system.
Iii. Implementation
Create a web service Project in VS2008.
Create two webmethods.
1. Start Tomcat Copy codeThe Code is as follows: // <summary>
/// Start the Permission System
/// </Summary>
/// <Returns> true: Successful; false: Failed </returns>
[WebMethod]
Public bool StartPM ()
{
Try
{
// Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo (AppDomain. CurrentDomain. BaseDirectory + "start. bat ");
// Obtain system environment variables
IDictionary dics = Environment. GetEnvironmentVariables (EnvironmentVariableTarget. Machine );
Foreach (string key in dics. Keys)
{// Add the system environment variable to the new process environment variable
If (sinfo. EnvironmentVariables. ContainsKey (key) continue;
Sinfo. EnvironmentVariables. Add (key, dics [key]. ToString ());
}
// You do not need to create a shell from a system, but directly create a shell from a file.
Sinfo. UseShellExecute = false;
Process. Start (sinfo );
}
Catch (Exception ex)
{
// TODO: Write logs
Return false;
}
Return true;
}

. Disable Tomcat MethodCopy codeThe Code is as follows: // <summary>
/// Disable the Permission System
/// </Summary>
/// <Returns> true: Successful; false: Failed </returns>
[WebMethod]
Public bool StopPM ()
{
Try
{
// Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo (AppDomain. CurrentDomain. BaseDirectory + "stop. bat ");
// Obtain system environment variables
IDictionary dics = Environment. GetEnvironmentVariables (EnvironmentVariableTarget. Machine );
Foreach (string key in dics. Keys)
{// Add the system environment variable to the new process environment variable
If (sinfo. EnvironmentVariables. ContainsKey (key) continue;
Sinfo. EnvironmentVariables. Add (key, dics [key]. ToString ());
}
// You do not need to create a shell from a system, but directly create a shell from a file.
Sinfo. UseShellExecute = false;
Process. Start (sinfo );
}
Catch (Exception ex)
{
// TODO: Write logs
Return false;
}
Return true;
}

To facilitate future configuration, I have created two bat files. In these two files, call Tomcat's startup. bat and shutdown. bat respectively. The details are as follows:
1. start. batCopy codeThe Code is as follows: K: \ apache-tomcat-6.0.30 \ bin \ startup. bat

2. stop. batCopy codeThe Code is as follows: K: \ apache-tomcat-6.0.30 \ bin \ shutdown. bat

Iv. Summary
During the development process, the CATALINA_HOME and JAVA_HOME environment variables cannot be found because the parent process, that is, the environment variables of web service, do not contain these two variables, therefore, neither of the two processes is created (the environment variables of the child process inherit from the parent process. You have to get the environment variable from the system environment variable and add it to the newly started process. At the same time, it must be clear that the working directory of the process is not equal to the startup directory of the process. For exampleCopy codeThe Code is as follows: ProcessStartInfo sinfo = new ProcessStartInfo (AppDomain. CurrentDomain. BaseDirectory + "stop. bat ");

If AppDomain. CurrentDomain. BaseDirectory is removed, the error "stop. bat" cannot be found because the working directory and startup directory are inconsistent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.