Summary of Java service background startup problems
Problem background:
For plug-in deployment, since the synchronization task and VB request business interface are written in java language, they need to be written when actually deploying java services to warehouse 4.35 server The bat script starts the jar package service and sets it to run in the background.
- . bat script:
start D:/cq_ck/FimSys/jre32/jre32/bin/java -jar FIMSys.jar
Note: the reason why jre is directly specified here is that the 32-bit jdk1.0 is installed on the 4.35 server 8, the following error will appear: "the. EXE file is not a valid windows32-bit application", so install the 32-bit jdk1.exe locally 8, copy the jre to the 4.35 environment, and specify the jre running environment to start the jar package.
- . The vbs script is as follows:
set ws=WScript.CreateObject("WScript.Shell") ws.Run"D:\cq_ck\FimSys\start.bat /start",0
Set start The vbs script is placed on the program startup page so that the service can be started automatically when the service is restarted.
The service is successful when the following command box appears
However, in the actual production, there was a problem that VB could not adjust the java interface. After the problem was located, the following reasons were found: since the account used to log in to the 4.35 server is a company domain account, the account will be automatically cancelled after the specified time, so the service started above will also hang up.
Therefore, the java service needs to be run in the background as a windows service. Even if the current domain account is cancelled, the service operation will not be affected.
According to the above ideas, there are several ways to make java services run in the background of Windows services:
- Will bat file as windows service
- Make jar package into windows service
- Deploy war to Tomcat as a windows Service
Mode 1: transfer bat file as windows service
. bat files can be registered as windows services in the following two ways:
- Put it directly bat file is registered as a windows service
- Convert the bat file to exe and register as a windows service
The implementation of these two methods is basically the same, but the second method needs to be added bat file is registered as exe process, the following is the second way to record the process.
1.1 transfer Convert bat file to exe package
Tool required: Bat_To_Exe toolkit, open the software after downloading and select the one to be converted Bat file, compiled exe package
Obtained after conversion exe package
1.1 instsrv.exe tool registration service
Note: administrator permission is required here
Execute the following code
instsrv start D:\cq_ck\FimSys\start.bat
1.2 registry editor add exe path information
cmd --> regedit --> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\fimStart.exe
Documents and contents to be added:
Item: Parameters character string: Application D:\cq_ck\FimSys character string: AppDirectory D:\cq_ck\FimSys\fimStart.exe character string: AppParameters
1.3 enter the service and set the startup mode
1.4 shut down and uninstall services
Execute the following command
instsrv fimStart remove
Summary of problems:
An error will be reported on the server: "service startup failed: error code 1053, the service did not respond to the startup or control request in time"
In view of the above problems, by consulting the data, the default startup timeout of windows services is 30s, but the startup time of some services may exceed 30s. Therefore, it is necessary to modify the registry to solve this problem. The registry address is:
HKEY_ LOCAL_ Machine / system / currentcontrolset / control / servicespipettimeout. Servicespipettimeout may not exist. If it does not exist, you need to add it manually. The type is DWORD in milliseconds.
The server needs to be restarted due to this setting, but the 4.35 server cannot be restarted due to its particularity. The test is not continued here, but it can provide an idea for future work.
Mode 2: make jar package into windows service
Because mode one will bat file is not registered as a windows service. The second method is used here, using winsw Exe registers the jar package as a windows service, as follows:
2.1 download winsw-x86 Exe application
GitHub address: https://github.com/winsw/winsw/releases
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-6lfojuqi-1628769608213)( https://gitee.com/wang-sy-0924/cloud-pic-go/raw/master/img/83JV (~(B)]IGPG(7Q)C%Y}9E. png)
2.2 writing XML files
<service> <id>FIMSysTest</id> <name>FIMSysTest</name> <description>This service runs FIMSysTest system.</description> <executable>E:/cq_ck/FimSys/jre32/jre32/bin/java</executable> <arguments>-jar FIMSysTest.jar</arguments> <log mode="roll" /> <onfailure action="restart" /> </service>
2.3 registration services
Modify winsw-x86 Exe name is FIMSys.exe Exe and add fimsys jar,FIMSys.xml,FIMSys. Exe is placed in the same level directory, and use the following command to register the service
FIMSysTest install
Summary of problems:
At that time, the jar package can be made into a Windows service, but when deploying the 4.35 server, an error occurs when executing FIMSys install: "the. exe file is not a valid windows32-bit application."
This problem is the same as that jdk cannot be installed manually. The version number of exe downloaded is checked and no problem is found, but it can be successfully executed locally. There is a solution on the Internet, which may be due to a virus and needs to be deleted Exe registry, etc. due to the particularity of this server, the above operations are not performed.
Method 3: deploy war to Tomcat and set up windows service
Because the server environment is relatively old, windows 2003 cannot manually install jdk1 8. Installation free 32-bit JDK and installation free tomcat8 are used here 5. Because jdk1.0 is installed in the server 4. You need to set the path environment variable and set the java version to version 1.8.
In addition, the java service needs to be deployed as a war package. When the war package is deployed, the default access needs to add the war package name. However, in the VB service, the requested address has been written dead without the service name. At this time, it is necessary to modify the server XML, add the following configuration:
-
appBase=""
-
<Context path="" docBase="D:\cq_ck\apache-tomcat-8.5.69-windows-x86\apache-tomcat-8.5.69\webapps\FIMSys" debug="0" reloadable="true" crossContext="true"/>
docBase: set the war package path, and FIMSys is the war package name
<!--<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">--> <Host name="localhost" appBase="" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="D:\cq_ck\apache-tomcat-8.5.69-windows-x86\apache-tomcat-8.5.69\webapps\FIMSys" debug="0" reloadable="true" crossContext="true"/> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
The background runs successfully, and it's done!!!