The Grails project is packaged and started as a Windows service

The purpose of this document is to:

  1. The war package packaged by grails project runs under the system as a Windows service
  2. After the deployment is completed, the default settings of startup and self startup are supported
  3. Supports 32-bit and 64 bit JDK
  4. Implement one click installation service
  5. Support one click uninstall service
  6. Support one click restart service
  7. Support one click stop service
  8. Support yml external configuration file to modify environment variables

System environment:

Text:

1: Create service installation script

Scheme 1: (this script uses independent JDK and does not need to configure JDK environment variables for the system)

1. Create a new txt file with the file name "service installation"

2. Replace txt suffix changed to bat

3. Paste the following script

@echo off
rem This script is responsible for the installation grails-service-0.1.war For system services, scripts depend on Procrun
rem Be sure to check before running the script procrun.exe Put the file in the same directory as the script

rem set up jdk Directory, recommended JDK The installation free package is placed in the same directory as this installation script
SET JAVA_HOME=".\JDK8-32\jre"
: install_service
if "%JAVA_HOME%" == "" (
    echo can't find JRE,Not copied JDK-32 File package to the directory where the current file is located, please copy.....
    goto failed
) else (
    "%JAVA_HOME%\bin\java" -version 2>&1 | find /i "build 1.8.0" > nul 2>&1
    if %ERRORLEVEL% NEQ 0 (
        echo Must use java8 Environment, please check the used JDK edition.
        goto failed
    )
)

if exist "%JAVA_HOME%\jre" (
    set "JRE_HOME=%JAVA_HOME%\jre"
) else (
    set "JRE_HOME=%JAVA_HOME%"
)

rem Try to use the server jvm
set "JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%JVM%" goto foundJvm
echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
set JVM=auto

:foundJvm
cd /d %~dp0
set "SERVICE_PATH=%cd%"
echo use JAVA_HOME Variable value: %JAVA_HOME%
prunsrv.exe install grails-service ^
    --Description "grails-server Test the service, and the service will restart automatically" ^
    --Startup auto ^
    --StartMode jvm ^
    --StopMode jvm ^
    --Jvm "%JVM%" ^
    --Classpath "%SERVICE_PATH%\grails-service-0.1.war" ^
    --StartClass "org.springframework.boot.loader.WarLauncher" ^
    --StopClass "java.lang.System"^
    --LogPath  "./"^
    --StopMethod "exit"^
if %ERRORLEVEL% NEQ 0 goto failed

sc failure "grails-service"  actions= restart/5000/restart/5000/restart/5000 reset= 86400
if %ERRORLEVEL% NEQ 0 (
    echo Failed to set the automatic restart of the service. Please make sure to run the script as an administrator
    goto :failed
)

:start_service
echo Attempt to start service...
prunsrv.exe start grails-service

if %ERRORLEVEL% NEQ 0 (
    echo Service startup failed. Please check the terminal output or related log location problem
) else (
  goto :EOF
)

:failed
echo Installation failed. Please check the error information output on the terminal and relevant logs, fix the problem and run again.
pause

Scheme 2: (this script uses the system default environment variable. You need to configure the JDK environment variable in advance, and you can choose one from the scheme)

1. Create a new txt file with the file name "service installation"

2. Replace txt suffix changed to bat

3. Paste the following script

@echo off
rem This script is responsible for the installation grails-service-0.1.war For system services, scripts depend on Procrun
rem Be sure to check before running the script procrun.exe Put the file in the same directory as the script

rem lookup windows In the system environment variable JDK
if "%JAVA_HOME%" == "" (
    for %%I in ("HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\1.8",
        "HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment\1.8") do (
        reg query %%I /v "JavaHome" > nul 2>&1

        if %ERRORLEVEL% EQU 0 (
            for /f "tokens=1,2*" %%i in ('reg query %%I ^| find /i "JavaHome"') do (
                SET JAVA_HOME=%%k
            )
            goto install_service
        )
    )
)

: install_service
if "%JAVA_HOME%" == "" (
    echo can't find JDK or JRE,Please check the system JDK Is the environment variable installed in the correct configuration
    goto failed
) else (
    "%JAVA_HOME%\bin\java" -version 2>&1 | find /i "build 1.8.0" > nul 2>&1
    if %ERRORLEVEL% NEQ 0 (
        echo Must use java8 Environment, please check the used JDK edition.
        goto failed
    )
)

if exist "%JAVA_HOME%\jre" (
    set "JRE_HOME=%JAVA_HOME%\jre"
) else (
    set "JRE_HOME=%JAVA_HOME%"
)

rem Try to use the server jvm
set "JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%JVM%" goto foundJvm
echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
set JVM=auto

:foundJvm
cd /d %~dp0
set "SERVICE_PATH=%cd%"
echo use JAVA_HOME Variable value: %JAVA_HOME%
prunsrv.exe install grails-service ^
    --Description "grails-server Test the service, and the service will restart automatically" ^
    --Startup auto ^
    --StartMode jvm ^
    --StopMode jvm ^
    --Jvm "%JVM%" ^
    --Classpath "%SERVICE_PATH%\grails-service-0.1.war" ^
    --StartClass "org.springframework.boot.loader.WarLauncher" ^
    --StopClass "java.lang.System"^
    --LogPath  "./"^
    --StopMethod "exit"^
if %ERRORLEVEL% NEQ 0 goto failed

sc failure "grails-service"  actions= restart/5000/restart/5000/restart/5000 reset= 86400
if %ERRORLEVEL% NEQ 0 (
    echo Failed to set the automatic restart of the service. Please make sure to run the script as an administrator
    goto :failed
)

:start_service
echo Attempt to start service...
prunsrv.exe start grails-service

if %ERRORLEVEL% NEQ 0 (
    echo Service startup failed. Please check the terminal output or related log location problem
) else (
  goto :EOF
)

:failed
echo Installation failed. Please check the error information output on the terminal and relevant logs, fix the problem and run again.
pause

2: Create service stop script

1. Create a new txt file with the file name "service stop"

2. Replace txt suffix changed to bat

3. Paste the following script

@net stop grails-service

3: Create service uninstall script

1. Create a new txt file with the file name "service uninstall"

2. Replace txt suffix changed to bat

3. Paste the following script

@echo off

cd /d %~dp0
prunsrv.exe delete grails-service

4: Create service restart script

1. Create a new txt file with the file name "service restart"

2. Replace txt suffix changed to bat

3. Paste the following script

@net stop grails-service
@net start grails-service

5: Download

prunsrv.exe 

Download address:

https://downloads.apache.org/commons/daemon/binaries/windows/commons-daemon-1.2.4-bin-windows.zip

Unzip after download

Vi. create service installation folder and copy relevant files

1. Grails services folder (it's better not to use Chinese names. You can start your own name. As for whether there will be any mistakes, I haven't tried, and I don't know whether there will be any wonderful mistakes)

2. Copy prunsrv Exe, service installation Bat, service stop Bat, service uninstall Bat, service restart bat,grails-service-0.1.war six files. If the independent JDK is used, the JDK installation free package is placed in this directory. If the system environment variable is used, the step of copying the JDK is ignored.

3. Run the service installation as an administrator bat, if you don't report an error, congratulations on your success. You can make do with it.

So far, the war package packaged by grails can achieve the first seven purposes. If the above script needs to be modified, the following contents need to be modified:

1. If you do not use the JDK provided by the system, you need to use the JDK path in line 6 of scheme 1

2. Modify the Grails service name in all scripts to your own script name

3. Install grails-service-0.1 after the script -- Classpath War replace with your own packaged war name

7: I'm not a mediocre person. After the system is deployed, I can't flexibly modify the environment variable configuration. I don't allow such disgusting things. I modify them every time I pack them, and then pack them after modification. I don't do that. Don't say much and get to the point.

1. Create environmentconfig groovy 

 2. New spring Factories file

Document content:

org.springframework.boot.env.EnvironmentPostProcessor=grails.service.EnvironmentConfig

Environmentconfig Groovy} name and path should be the same as the environmentconfig created above The groovy package path and name are consistent

3. Modify the service pin number in the yml file for testing (other contents are default after creating grails project, and the last two lines are newly added for testing)

---
grails:
    profile: web
    codegen:
        defaultPackage: grails.service
    spring:
        transactionManagement:
            proxies: false
    gorm:
        reactor:
            # Whether to translate GORM events into Reactor events
            # Disabled by default for performance reasons
            events: false
info:
    app:
        name: '@info.app.name@'
        version: '@info.app.version@'
        grailsVersion: '@info.app.grailsVersion@'
spring:
    main:
        banner-mode: "off"
    groovy:
        template:
            check-template-location: false

# Spring Actuator Endpoints are Disabled by Default
endpoints:
    enabled: false
    jmx:
        enabled: true

---
grails:
    mime:
        disable:
            accept:
                header:
                    userAgents:
                        - Gecko
                        - WebKit
                        - Presto
                        - Trident
        types:
            all: '*/*'
            atom: application/atom+xml
            css: text/css
            csv: text/csv
            form: application/x-www-form-urlencoded
            html:
              - text/html
              - application/xhtml+xml
            js: text/javascript
            json:
              - application/json
              - text/json
            multipartForm: multipart/form-data
            pdf: application/pdf
            rss: application/rss+xml
            text: text/plain
            hal:
              - application/hal+json
              - application/hal+xml
            xml:
              - text/xml
              - application/xml
    urlmapping:
        cache:
            maxsize: 1000
    controllers:
        defaultScope: singleton
    converters:
        encoding: UTF-8
    views:
        default:
            codec: html
        gsp:
            encoding: UTF-8
            htmlcodec: xml
            codecs:
                expression: html
                scriptlets: html
                taglib: none
                staticparts: none
endpoints:
    jmx:
        unique-names: true

---
hibernate:
    cache:
        queries: false
        use_second_level_cache: false
        use_query_cache: false
dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.h2.Driver
    username: sa
    password: ''

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    test:
        dataSource:
            dbCreate: update
            url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    production:
        dataSource:
            dbCreate: none
            url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
            properties:
                jmxEnabled: true
                initialSize: 5
                maxActive: 50
                minIdle: 5
                maxIdle: 25
                maxWait: 10000
                maxAge: 600000
                timeBetweenEvictionRunsMillis: 5000
                minEvictableIdleTimeMillis: 60000
                validationQuery: SELECT 1
                validationQueryTimeout: 3
                validationInterval: 15000
                testOnBorrow: true
                testWhileIdle: true
                testOnReturn: false
                jdbcInterceptors: ConnectionState
                defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

server:
    port: 8090

3. Stop the service and copy the application YML file and packaged grails-service-0.1 War to the Grails service folder, and then restart the service. If not expected, the service port number has changed to 8090.

8: After writing for three hours, I finally finished it. If you have any questions, you can leave a message to me. I may see it immediately or a year later.

Keywords: Java Windows Back-end Groovy

Added by xydra on Fri, 14 Jan 2022 05:37:36 +0200