asp.net control tomcat Startup Shutdown Implementation Method _ Practical Tips

Source: Internet
Author: User
first, the scene
Recently, a project customer asked to be able to configure their own relevant permissions. For historical reasons this project uses the company's previous permission system. This permission system is very powerful, but there is a disadvantage, that is, each additional permission menu to restart to take effect, or it will wait 1 days after the expiration of the cache will not be effective. Because of the progress of the project, we could not overturn the authority system again.
The possible way is to increase the permission System restart button on the admin interface. This allows the customer to easily restart Tomcat after modifying the permissions.
second, technical analysis
Because the administrator system is based on the. NET BS approach. So we have to be able to control Tomcat's shutdown via web page and load it into the admin system.
First, analyze how Tomcat starts. There are two batch files in the Tomcat6.0 bin directory. Startup.bat and Shutdown.bat control the start and shutdown of Tomcat, respectively. We have to control Tomcat's startup shutdown to invoke these two batch files.
How to tune it? Where's the tune? It is impossible to adjust the browser directly. You must deploy a Web page on the Web server to invoke both commands on the service side. This is best if deployed inside the permission system, but this creates a problem, we can turn tomcat off, but it doesn't start. The final solution is to deploy the webservice of the. NET implementation on the same machine, through which the permission system is started and shut down. WebService
Third, the concrete realization
Create a Web service project in VS2008.
Create two WebMethod.
1. Start Tomcat method
Copy Code code as follows:

<summary>
Start Permission System
</summary>
<returns>true: Success; false: Failed </returns>
[WebMethod]
public bool STARTPM ()
{
Try
{
Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo (AppDomain.CurrentDomain.BaseDirectory + "Start.bat");
Get System Environment variables
IDictionary dics = Environment.getenvironmentvariables (environmentvariabletarget.machine);
foreach (String key in Dics. Keys)
{//Add system environment variables to new process environment variables
if (sinfo. Environmentvariables.containskey (key)) continue;
Sinfo. Environmentvariables.add (Key, Dics[key]. ToString ());
}
Does not need to be created from the system shell and created directly from the file
Sinfo. UseShellExecute = false;
Process.Start (Sinfo);
}
catch (Exception ex)
{
TODO: Write Log
return false;
}
return true;
}

. Turn off the Tomcat method
Copy Code code as follows:

<summary>
Turn off permission system
</summary>
<returns>true: Success; false: Failed </returns>
[WebMethod]
public bool stoppm ()
{
Try
{
Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo (AppDomain.CurrentDomain.BaseDirectory + "Stop.bat");
Get System Environment variables
IDictionary dics = Environment.getenvironmentvariables (environmentvariabletarget.machine);
foreach (String key in Dics. Keys)
{//Add system environment variables to new process environment variables
if (sinfo. Environmentvariables.containskey (key)) continue;
Sinfo. Environmentvariables.add (Key, Dics[key]. ToString ());
}
Does not need to be created from the system shell and created directly from the file
Sinfo. UseShellExecute = false;
Process.Start (Sinfo);
}
catch (Exception ex)
{
TODO: Write Log
return false;
}
return true;
}

To facilitate later configuration I have created two bat files. Do the Startup.bat and Shutdown.bat calls to Tomcat separately in these two files. Specifically as follows:
1.start.bat
Copy Code code as follows:
K:\apache-tomcat-6.0.30\bin\startup.bat

2.stop.bat
Copy Code code as follows:
K:\apache-tomcat-6.0.30\bin\shutdown.bat

Iv. Summary
The error of Catalina_home, Java_home environment variables that cannot be found throughout the development process is because the parent process, which is not in the environment variable of the Web service, does not have both, so when the child process is created (the child process's environment variable inherits from the parent process). You have to get the environment variable from the system environment variable to add it back into the newly initiated process. It must also be clear that the working directory of the process does not equal the process's startup directory. Like what
Copy Code code as follows:
ProcessStartInfo sinfo = new ProcessStartInfo (AppDomain.CurrentDomain.BaseDirectory + "Stop.bat");

If you remove AppDomain.CurrentDomain.BaseDirectory, you will not be able to find the Stop.bat error because the working directory and the startup directory are inconsistent.
Related Article

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.