Tomcat Background Server Application Analysis (HIGH)

xiaoxiao2021-03-06  53

Background service program application analysis

I. Background introduction: Background Server Loading, mainly applying the service.exe program to load the program into the WinNT launch service. Start service loading form, in Tomcat and JRun already have applications, below Tomcat as an example, simply introduce the implementation principle and process:

Service.exe: The original name is JavaService.exe, now renamed: Service.exe now This idea simulates most Java applications to create boot boot service in the Windows system. (Of course, other language compiled programs can also be loaded, now taking the Tomcat server as an example, Tomcat as an open source software, convenient to learn from its design ideas)

1. Copy Service.exe to the Java application directory you want to start; 2, create a .bat file, file content format is as follows: Service.exe -install "Mediaaservice" C: /JDK1.3.0_02/jre/bin/ Classic / jvm.dll -djava.class.path = c: /app/authsvr/media/auth.jar -dcatalina.home = C: / App / AuthSVR / Media / -start C: / App / AuthSVR / Media / Auth .jar -Params star -stop c: /app/authsvr/media/auth.jar -Params Stop -out C: /app/authsvr/media/stdout.log -ERR C: /APP/AUTHSVR/MEDia/stderr.log

C: /jdk1.3.0_02/bin/java.exe -jar -duser.dir = "c: / program files / apache tomcat 4.0" "C: / Program files / apache tomcat 4.0 / bin / bootstrap.jar" Start C : /jdk1.3.0_02/bin/java.exe -jar -duser.dir = "C: / Program Files / Apache Tomcat 4.0" "C: / Program Files / Apache Tomcat 4.0 / bin / bootstrap.jar" stop // The following is the Tomcat example, Tomcat.exe is a program similar to Service.exe, using C to write a shared applet, which can be downloaded or copied in many places. // Change to Tomcat.exe or Service.exe is only more likely to understand its functionality when it is used. // At the command prompt, you can check the corresponding available parameters thereof.

@ Tomcat.exe -install Tomcat @ {jdk_home} / jre / bin / {hotspot | server | classic} /jvm.dll @ -djava.class.path = {tomcat_home} /bin/bootstrap.jar; {Tomcat_Home} / bin /servlet.jar; (JDK_HOME }/lib/tools.jar @ -dcatalina.home = {tomcat_home} @ -start org.apache.catalina.startup.bootstrapservice @ -Params Start @ -stop org.apache.catalina.startup. BootstrapService @ -params stop @ -out {TOMCAT_HOME} /logs/stdout.log @ -err {TOMCAT_HOME} /logs/stderr.log3, parameters: To install a service: -install service_name jvm_library (jvm_option) * -start start_class [ -method start_method] [-params (start_parameter) ] [-stop start_class [-method stop_method] [-params (stop_parameter) ]] [-out out_log_file] [-err err_log_file] [-current current_dir] [-path extra_path]

4, analyze Tomcat automatic loading service mode

@ {JDK_HOME} / jre / bin / {hotspot | server | classic} /jvm.dll // jre, jvm @ -djava.class.path = {tomcat_home} /bin/bootstrap.jar; {Tomcat_Home} / bin / servlet .jar; {JDK_HOME} /lib/tools.jar //*.jar@ -dcatalina.home = {Tomcat_Home} // Path

Start: c: /jdk1.3.0_02/bin/java.exe -jar -duser.dir = "c: / program files / apache tomcat 4.0" "C: / Program files / apache tomcat 4.0 / bin / bootstrap.jar" START The corresponding load statement is: @ -start org.apache.catalina.startup.bootstrapservice @ -Params Start

Stop: c: /jdk1.3.0_02/bin/java.exe -jar -duser.dir = "c: / program files / apache tomcat 4.0" "C: / Program files / apache tomcat 4.0 / bin / bootstrap.jar" STOP The corresponding load statement is:

@ -stop org.apache.catalina.startup.bootstrapservice @ -params Stop The following is the output to the corresponding standard event and error event to the corresponding record file: @ --out {tomcat_home} /logs/stdout.log @ - Err {tomcat_home} /logs/stderr.log5, the next task is to start the thread that is activated, and make the corresponding parameters of the corresponding Start or Stop, corresponding to org.bootstrapservice.java, or It is an other method for providing equivalents. BootstrapService.java and other related files are attached to the compression package together.

Second, implementation steps: 1, Tomcat startup implementation, mainly to apply the daemon interface, so it also establishes a com.luulu.app.daemon.daemon interface

Package com.lulu.app.daemon;

// Reference Classes of Package Com.lu.app.daemon: // DaemonContext

Public interface daemon {

Public Abstract Void Init (DaemonContext daemonText).

Public Abstract void start () throws Exception

Public Abstract void stop () throws exception;

Public abstract void destroy ();

2, then implement the daemon interface in the boot program bootstrapservice.java (IMPLEments DAEMON)

Take the Program application as an example, you need to transform pgmautoupdate.java to implement the daemon interface.

/ / -------------------------------------------------------------------------------------------- -------- Service Methods

/ ** * load the app service. * / Public void init (daemonContext context) throws exception {

String arguments [] = NULL;

/ * Read the arguments from the daemon context * / if (context! = Null) {arguments = context.getarguments (); if (arguments! = Null) {for (int i = 0; i

If (! boolnoreadkey) {rk = new readkey (); rk.Start ();}

"CREATE APP Server");

}

/ ** * Start the app service. * / Public void start () throws exception {system.out.println ("Test Start IS OK:"); // for (int i = 0 ;; i ) for (;; ) {TRY {if (! Stopflag) {// system.out.println ("Test Times:" i); pgmupdate = new pgMUpdate (); pgMUpdate.start (); pgMUpdate.join ();} else system. EXIT (1);} catch (exception ex) {system.out.println (ex.getMessage ();}

Try {thread.sleep (1000);} catch (interruptedException ex) {system.out.println (ex.getMessage ());}}}

/ ** * stop the app service. * / Public void stop () throws exception {system.out.println ("Test Stop is Ok:"); stopflag = true;

/ ** * destroy the app service. * / Public void destroy () {// fixme

}

3. Package the compilation, package the command: Rem@echo off

REM produces JAR file JAR CVFM ../bin/service.jar manifest.mf -c ../classes .Rem JAR file package end

The manifest.mf file is as follows:

Manifest-version: 1.0created-by: sun jarmain-class: com.lulu.app.program.bootstrapserviceClass-path: Specification-Title: App Servicespecification-version: 1.0created-by:

4, use service.exe, load it into the NT service startup.

The service.bat content is as follows:

Service.exe -install appservice c: /jdk1.3.1_03/jre/bin/HOTSPOT/JVM.DLL -XMX512M -XMS128M -DJAVA.CLASS.PATH = D: /APP/Application/Program/bin/program.jar; / LIB / Tools.jar -start com.lulu.app.program.bootstrapservice -Params Start C: /jdk1.3.1_03-stop com.lulu.app.program.bootstrapservice -Params Stop -out D: / biwm / appLication /program/logs/stdout.log -err d: /biwm/app/Application/program/logs/stderr.log

Parameter format and usage, refer to the previous background.

转载请注明原文地址:https://www.9cbs.com/read-115438.html

New Post(0)